Skip to content

Commit 7b3ffe0

Browse files
authored
v3: config file is now ts by default and uses named exports (#962)
* A bunch of changes to the config file: - No more default export, the config is exported via the `config` named export - `handleError` is moved to a separate named export - Added the ability to include additional packages (e.g. wrangler) that aren’t included by default (think CLI usage) - More reliably install packages in dev - The config is now built first instead of directly imported, meaning we can now support typescript config files (`trigger.config.ts`) * Update to trigger.config.ts in `trigger.dev init`
1 parent 737ff99 commit 7b3ffe0

File tree

25 files changed

+481
-194
lines changed

25 files changed

+481
-194
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"type": "node-terminal",
3434
"request": "launch",
3535
"name": "Debug V3 Dev CLI",
36-
"command": "pnpm exec trigger.dev dev",
36+
"command": "pnpm exec trigger.dev dev --log-level debug",
3737
"cwd": "${workspaceFolder}/references/v3-catalog",
3838
"sourceMaps": true
3939
},

packages/cli-v3/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"gradient-string": "^2.0.2",
111111
"import-meta-resolve": "^4.0.0",
112112
"ink": "^4.4.1",
113+
"jsonc-parser": "^3.2.1",
113114
"jsonlines": "^0.1.1",
114115
"liquidjs": "^10.9.2",
115116
"mock-fs": "^5.2.0",

packages/cli-v3/src/commands/deploy.ts

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { intro, log, outro, spinner } from "@clack/prompts";
22
import { depot } from "@depot/cli";
33
import { context, trace } from "@opentelemetry/api";
4-
import { ResolvedConfig, detectDependencyVersion, flattenAttributes, recordSpanException } from "@trigger.dev/core/v3";
4+
import {
5+
ResolvedConfig,
6+
detectDependencyVersion,
7+
flattenAttributes,
8+
recordSpanException,
9+
} from "@trigger.dev/core/v3";
510
import chalk from "chalk";
611
import { Command, Option as CommandOption } from "commander";
712
import { Metafile, build } from "esbuild";
@@ -29,7 +34,7 @@ import {
2934
import { readConfig } from "../utilities/configFiles.js";
3035
import { createTempDir, readJSONFile, writeJSONFile } from "../utilities/fileSystem";
3136
import { printStandloneInitialBanner } from "../utilities/initialBanner.js";
32-
import { detectPackageNameFromImportPath } from "../utilities/installPackages";
37+
import { detectPackageNameFromImportPath, parsePackageName } from "../utilities/installPackages";
3338
import { logger } from "../utilities/logger.js";
3439
import { createTaskFileImports, gatherTaskFiles } from "../utilities/taskFiles";
3540
import { login } from "./login";
@@ -63,11 +68,7 @@ export function configureDeployCommand(program: Command) {
6368
"prod"
6469
)
6570
.option("-T, --skip-typecheck", "Whether to skip the pre-build typecheck")
66-
.option(
67-
"-c, --config <config file>",
68-
"The name of the config file, found at [path]",
69-
"trigger.config.mjs"
70-
)
71+
.option("-c, --config <config file>", "The name of the config file, found at [path]")
7172
.option(
7273
"-p, --project-ref <project ref>",
7374
"The project ref. Required if there is no config file."
@@ -134,7 +135,11 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
134135

135136
intro("Deploying project");
136137

137-
const authorization = await login({ embedded: true, defaultApiUrl: options.apiUrl, profile: options.profile });
138+
const authorization = await login({
139+
embedded: true,
140+
defaultApiUrl: options.apiUrl,
141+
profile: options.profile,
142+
});
138143

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

230235
deploymentSpinner.start(`Deploying version ${version}`);
231-
const registryHost = deploymentResponse.data.registryHost ?? options.registry ?? "registry.trigger.dev";
236+
const registryHost =
237+
deploymentResponse.data.registryHost ?? options.registry ?? "registry.trigger.dev";
232238

233239
const buildImage = async () => {
234240
if (options.selfHosted) {
@@ -345,7 +351,8 @@ async function _deployCommand(dir: string, options: DeployCommandOptions) {
345351
);
346352
} else {
347353
outro(
348-
`Version ${version} deployed with ${taskCount} detected task${taskCount === 1 ? "" : "s"
354+
`Version ${version} deployed with ${taskCount} detected task${
355+
taskCount === 1 ? "" : "s"
349356
} ${deploymentLink}`
350357
);
351358
}
@@ -515,14 +522,14 @@ type BuildAndPushImageOptions = {
515522

516523
type BuildAndPushImageResults =
517524
| {
518-
ok: true;
519-
image: string;
520-
digest?: string;
521-
}
525+
ok: true;
526+
image: string;
527+
digest?: string;
528+
}
522529
| {
523-
ok: false;
524-
error: string;
525-
};
530+
ok: false;
531+
error: string;
532+
};
526533

527534
async function buildAndPushImage(
528535
options: BuildAndPushImageOptions
@@ -774,12 +781,12 @@ async function compileProject(
774781

775782
workerContents = workerContents.replace(
776783
"__IMPORTED_PROJECT_CONFIG__",
777-
`import importedConfig from "${configPath}";`
784+
`import * as importedConfigExports from "${configPath}"; const importedConfig = importedConfigExports.config; const handleError = importedConfigExports.handleError;`
778785
);
779786
} else {
780787
workerContents = workerContents.replace(
781788
"__IMPORTED_PROJECT_CONFIG__",
782-
`const importedConfig = undefined;`
789+
`const importedConfig = undefined; const handleError = undefined;`
783790
);
784791
}
785792

@@ -919,8 +926,7 @@ async function compileProject(
919926

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

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

11421148
// Returns the dependencies that are required by the output that are found in output and the CLI package dependencies
11431149
// Returns the dependency names and the version to use (taken from the CLI deps package.json)
1144-
function gatherRequiredDependencies(
1150+
async function gatherRequiredDependencies(
11451151
imports: Metafile["outputs"][string]["imports"],
1146-
externalPackageJson?: { dependencies: Record<string, string> }
1152+
config: ResolvedConfig
11471153
) {
1154+
const externalPackageJson = await readJSONFile(join(config.projectDir, "package.json"));
1155+
11481156
const dependencies: Record<string, string> = {};
11491157

11501158
for (const file of imports) {
@@ -1165,15 +1173,44 @@ function gatherRequiredDependencies(
11651173
continue;
11661174
}
11671175

1168-
const internalDependencyVersion = (packageJson.dependencies as Record<string, string>)[
1169-
packageName
1170-
] ?? detectDependencyVersion(packageName);
1176+
const internalDependencyVersion =
1177+
(packageJson.dependencies as Record<string, string>)[packageName] ??
1178+
detectDependencyVersion(packageName);
11711179

11721180
if (internalDependencyVersion) {
11731181
dependencies[packageName] = internalDependencyVersion;
11741182
}
11751183
}
11761184

1185+
if (config.additionalPackages) {
1186+
for (const packageName of config.additionalPackages) {
1187+
if (dependencies[packageName]) {
1188+
continue;
1189+
}
1190+
1191+
const packageParts = parsePackageName(packageName);
1192+
1193+
if (packageParts.version) {
1194+
dependencies[packageParts.name] = packageParts.version;
1195+
continue;
1196+
} else {
1197+
const externalDependencyVersion = {
1198+
...externalPackageJson?.devDependencies,
1199+
...externalPackageJson?.dependencies,
1200+
}[packageName];
1201+
1202+
if (externalDependencyVersion) {
1203+
dependencies[packageParts.name] = externalDependencyVersion;
1204+
continue;
1205+
} else {
1206+
logger.warn(
1207+
`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`
1208+
);
1209+
}
1210+
}
1211+
}
1212+
}
1213+
11771214
// Make sure we sort the dependencies by key to ensure consistent hashing
11781215
return Object.fromEntries(Object.entries(dependencies).sort(([a], [b]) => a.localeCompare(b)));
11791216
}

0 commit comments

Comments
 (0)