Skip to content

v3: config file is now ts by default and uses named exports #962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"type": "node-terminal",
"request": "launch",
"name": "Debug V3 Dev CLI",
"command": "pnpm exec trigger.dev dev",
"command": "pnpm exec trigger.dev dev --log-level debug",
"cwd": "${workspaceFolder}/references/v3-catalog",
"sourceMaps": true
},
Expand Down
1 change: 1 addition & 0 deletions packages/cli-v3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"gradient-string": "^2.0.2",
"import-meta-resolve": "^4.0.0",
"ink": "^4.4.1",
"jsonc-parser": "^3.2.1",
"jsonlines": "^0.1.1",
"liquidjs": "^10.9.2",
"mock-fs": "^5.2.0",
Expand Down
89 changes: 63 additions & 26 deletions packages/cli-v3/src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { intro, log, outro, spinner } from "@clack/prompts";
import { depot } from "@depot/cli";
import { context, trace } from "@opentelemetry/api";
import { ResolvedConfig, detectDependencyVersion, flattenAttributes, recordSpanException } from "@trigger.dev/core/v3";
import {
ResolvedConfig,
detectDependencyVersion,
flattenAttributes,
recordSpanException,
} from "@trigger.dev/core/v3";
import chalk from "chalk";
import { Command, Option as CommandOption } from "commander";
import { Metafile, build } from "esbuild";
Expand Down Expand Up @@ -29,7 +34,7 @@ import {
import { readConfig } from "../utilities/configFiles.js";
import { createTempDir, readJSONFile, writeJSONFile } from "../utilities/fileSystem";
import { printStandloneInitialBanner } from "../utilities/initialBanner.js";
import { detectPackageNameFromImportPath } from "../utilities/installPackages";
import { detectPackageNameFromImportPath, parsePackageName } from "../utilities/installPackages";
import { logger } from "../utilities/logger.js";
import { createTaskFileImports, gatherTaskFiles } from "../utilities/taskFiles";
import { login } from "./login";
Expand Down Expand Up @@ -63,11 +68,7 @@ export function configureDeployCommand(program: Command) {
"prod"
)
.option("-T, --skip-typecheck", "Whether to skip the pre-build typecheck")
.option(
"-c, --config <config file>",
"The name of the config file, found at [path]",
"trigger.config.mjs"
)
.option("-c, --config <config file>", "The name of the config file, found at [path]")
.option(
"-p, --project-ref <project ref>",
"The project ref. Required if there is no config file."
Expand Down Expand Up @@ -134,7 +135,11 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {

intro("Deploying project");

const authorization = await login({ embedded: true, defaultApiUrl: options.apiUrl, profile: options.profile });
const authorization = await login({
embedded: true,
defaultApiUrl: options.apiUrl,
profile: options.profile,
});

if (!authorization.ok) {
if (authorization.error === "fetch failed") {
Expand Down Expand Up @@ -228,7 +233,8 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
const deploymentSpinner = spinner();

deploymentSpinner.start(`Deploying version ${version}`);
const registryHost = deploymentResponse.data.registryHost ?? options.registry ?? "registry.trigger.dev";
const registryHost =
deploymentResponse.data.registryHost ?? options.registry ?? "registry.trigger.dev";

const buildImage = async () => {
if (options.selfHosted) {
Expand Down Expand Up @@ -345,7 +351,8 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
);
} else {
outro(
`Version ${version} deployed with ${taskCount} detected task${taskCount === 1 ? "" : "s"
`Version ${version} deployed with ${taskCount} detected task${
taskCount === 1 ? "" : "s"
} ${deploymentLink}`
);
}
Expand Down Expand Up @@ -515,14 +522,14 @@ type BuildAndPushImageOptions = {

type BuildAndPushImageResults =
| {
ok: true;
image: string;
digest?: string;
}
ok: true;
image: string;
digest?: string;
}
| {
ok: false;
error: string;
};
ok: false;
error: string;
};

async function buildAndPushImage(
options: BuildAndPushImageOptions
Expand Down Expand Up @@ -774,12 +781,12 @@ async function compileProject(

workerContents = workerContents.replace(
"__IMPORTED_PROJECT_CONFIG__",
`import importedConfig from "${configPath}";`
`import * as importedConfigExports from "${configPath}"; const importedConfig = importedConfigExports.config; const handleError = importedConfigExports.handleError;`
);
} else {
workerContents = workerContents.replace(
"__IMPORTED_PROJECT_CONFIG__",
`const importedConfig = undefined;`
`const importedConfig = undefined; const handleError = undefined;`
);
}

Expand Down Expand Up @@ -919,8 +926,7 @@ async function compileProject(

// Get all the required dependencies from the metaOutputs and save them to /tmp/dir/package.json
const allImports = [...metaOutput.imports, ...entryPointMetaOutput.imports];
const projectPackageJson = await readJSONFile(join(config.projectDir, "package.json"));
const dependencies = gatherRequiredDependencies(allImports, projectPackageJson);
const dependencies = await gatherRequiredDependencies(allImports, config);

const packageJsonContents = {
name: "trigger-worker",
Expand Down Expand Up @@ -1141,10 +1147,12 @@ async function typecheckProject(config: ResolvedConfig, options: DeployCommandOp

// Returns the dependencies that are required by the output that are found in output and the CLI package dependencies
// Returns the dependency names and the version to use (taken from the CLI deps package.json)
function gatherRequiredDependencies(
async function gatherRequiredDependencies(
imports: Metafile["outputs"][string]["imports"],
externalPackageJson?: { dependencies: Record<string, string> }
config: ResolvedConfig
) {
const externalPackageJson = await readJSONFile(join(config.projectDir, "package.json"));

const dependencies: Record<string, string> = {};

for (const file of imports) {
Expand All @@ -1165,15 +1173,44 @@ function gatherRequiredDependencies(
continue;
}

const internalDependencyVersion = (packageJson.dependencies as Record<string, string>)[
packageName
] ?? detectDependencyVersion(packageName);
const internalDependencyVersion =
(packageJson.dependencies as Record<string, string>)[packageName] ??
detectDependencyVersion(packageName);

if (internalDependencyVersion) {
dependencies[packageName] = internalDependencyVersion;
}
}

if (config.additionalPackages) {
for (const packageName of config.additionalPackages) {
if (dependencies[packageName]) {
continue;
}

const packageParts = parsePackageName(packageName);

if (packageParts.version) {
dependencies[packageParts.name] = packageParts.version;
continue;
} else {
const externalDependencyVersion = {
...externalPackageJson?.devDependencies,
...externalPackageJson?.dependencies,
}[packageName];

if (externalDependencyVersion) {
dependencies[packageParts.name] = externalDependencyVersion;
continue;
} else {
logger.warn(
`Could not find version for package ${packageName}, add a version specifier to the package name (e.g. ${packageParts.name}@latest) or add it to your project's package.json`
);
}
}
}
}

// Make sure we sort the dependencies by key to ensure consistent hashing
return Object.fromEntries(Object.entries(dependencies).sort(([a], [b]) => a.localeCompare(b)));
}
Expand Down
Loading