-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathdev.ts
More file actions
288 lines (249 loc) · 8.93 KB
/
dev.ts
File metadata and controls
288 lines (249 loc) · 8.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import { ResolvedConfig } from "@trigger.dev/core/v3/build";
import { Command, Option as CommandOption } from "commander";
import { z } from "zod";
import { CommonCommandOptions, commonOptions, wrapCommandAction } from "../cli/common.js";
import { watchConfig } from "../config.js";
import { DevSessionInstance, startDevSession } from "../dev/devSession.js";
import { createLockFile } from "../dev/lock.js";
import { chalkError } from "../utilities/cliOutput.js";
import { resolveLocalEnvVars } from "../utilities/localEnvVars.js";
import { printDevBanner, printStandloneInitialBanner } from "../utilities/initialBanner.js";
import { logger } from "../utilities/logger.js";
import { runtimeChecks } from "../utilities/runtimeCheck.js";
import { getProjectClient, LoginResultOk } from "../utilities/session.js";
import { login } from "./login.js";
import { updateTriggerPackages } from "./update.js";
import {
readConfigHasSeenMCPInstallPrompt,
writeConfigHasSeenMCPInstallPrompt,
} from "../utilities/configFiles.js";
import { confirm, isCancel, log } from "@clack/prompts";
import { installMcpServer } from "./install-mcp.js";
import { tryCatch } from "@trigger.dev/core/utils";
import { VERSION } from "@trigger.dev/core";
import { initiateRulesInstallWizard } from "./install-rules.js";
const DevCommandOptions = CommonCommandOptions.extend({
debugOtel: z.boolean().default(false),
config: z.string().optional(),
projectRef: z.string().optional(),
skipUpdateCheck: z.boolean().default(false),
envFile: z.string().optional(),
keepTmpFiles: z.boolean().default(false),
maxConcurrentRuns: z.coerce.number().optional(),
mcp: z.boolean().default(false),
mcpPort: z.coerce.number().optional().default(3333),
analyze: z.boolean().default(false),
disableWarnings: z.boolean().default(false),
skipMCPInstall: z.boolean().default(false),
skipRulesInstall: z.boolean().default(false),
rulesInstallManifestPath: z.string().optional(),
rulesInstallBranch: z.string().optional(),
});
export type DevCommandOptions = z.infer<typeof DevCommandOptions>;
export function configureDevCommand(program: Command) {
return commonOptions(
program
.command("dev")
.description("Run your Trigger.dev tasks locally")
.option("-c, --config <config file>", "The name of the config file")
.option(
"-p, --project-ref <project ref>",
"The project ref. Required if there is no config file."
)
.option(
"--env-file <env file>",
"Path to the .env file to use for the dev session. Defaults to .env in the project directory."
)
.option(
"--max-concurrent-runs <max concurrent runs>",
"The maximum number of concurrent runs to allow in the dev session"
)
.option("--debug-otel", "Enable OpenTelemetry debugging")
.option("--skip-update-check", "Skip checking for @trigger.dev package updates")
.option(
"--keep-tmp-files",
"Keep temporary files after the dev session ends, helpful for debugging"
)
.option("--mcp", "Start the MCP server")
.option("--mcp-port", "The port to run the MCP server on", "3333")
.addOption(
new CommandOption("--analyze", "Analyze the build output and import timings").hideHelp()
)
.addOption(
new CommandOption(
"--skip-mcp-install",
"Skip the Trigger.dev MCP server install wizard"
).hideHelp()
)
.addOption(
new CommandOption(
"--skip-rules-install",
"Skip the Trigger.dev Agent rules install wizard"
).hideHelp()
)
.addOption(
new CommandOption(
"--rules-install-manifest-path <path>",
"The path to the rules install manifest"
).hideHelp()
)
.addOption(
new CommandOption(
"--rules-install-branch <branch>",
"The branch to install the rules from"
).hideHelp()
)
.addOption(new CommandOption("--disable-warnings", "Suppress warnings output").hideHelp())
).action(async (options) => {
wrapCommandAction("dev", DevCommandOptions, options, async (opts) => {
await devCommand(opts);
});
});
}
export async function devCommand(options: DevCommandOptions) {
runtimeChecks();
// Only show these install prompts if the user is in a terminal (not in a Coding Agent)
if (process.stdout.isTTY) {
const skipMCPInstall = typeof options.skipMCPInstall === "boolean" && options.skipMCPInstall;
if (!skipMCPInstall) {
const hasSeenMCPInstallPrompt = readConfigHasSeenMCPInstallPrompt();
if (!hasSeenMCPInstallPrompt) {
const installChoice = await confirm({
message: "Would you like to install the Trigger.dev MCP server?",
initialValue: true,
});
writeConfigHasSeenMCPInstallPrompt(true);
const skipInstall = isCancel(installChoice) || !installChoice;
if (!skipInstall) {
log.step("Welcome to the Trigger.dev MCP server install wizard 🧙");
const [installError] = await tryCatch(
installMcpServer({
yolo: false,
tag: VERSION as string,
logLevel: options.logLevel,
})
);
if (installError) {
log.error(`Failed to install MCP server: ${installError.message}`);
}
}
}
}
const skipRulesInstall =
typeof options.skipRulesInstall === "boolean" && options.skipRulesInstall;
if (!skipRulesInstall) {
await initiateRulesInstallWizard({
manifestPath: options.rulesInstallManifestPath,
branch: options.rulesInstallBranch,
});
}
}
const authorization = await login({
embedded: true,
silent: true,
defaultApiUrl: options.apiUrl,
profile: options.profile,
});
if (!authorization.ok) {
if (authorization.error === "fetch failed") {
logger.log(
`${chalkError(
"X Error:"
)} Connecting to the server failed. Please check your internet connection or contact eric@trigger.dev for help.`
);
} else {
logger.log(
`${chalkError("X Error:")} You must login first. Use the \`login\` CLI command.\n\n${
authorization.error
}`
);
}
process.exitCode = 1;
return;
}
let watcher;
try {
const devInstance = await startDev({ ...options, cwd: process.cwd(), login: authorization });
watcher = devInstance.watcher;
await devInstance.waitUntilExit();
} finally {
await watcher?.stop();
}
}
type StartDevOptions = DevCommandOptions & {
login: LoginResultOk;
cwd: string;
};
async function startDev(options: StartDevOptions) {
logger.debug("Starting dev CLI", { options });
let watcher: Awaited<ReturnType<typeof watchConfig>> | undefined;
try {
if (options.logLevel) {
logger.loggerLevel = options.logLevel;
}
await printStandloneInitialBanner(true);
let displayedUpdateMessage = false;
if (!options.skipUpdateCheck) {
displayedUpdateMessage = await updateTriggerPackages(options.cwd, { ...options }, true, true);
}
const removeLockFile = await createLockFile(options.cwd);
let devInstance: DevSessionInstance | undefined;
printDevBanner(displayedUpdateMessage);
const envVars = resolveLocalEnvVars(options.envFile);
if (envVars.TRIGGER_PROJECT_REF) {
logger.debug("Using project ref from env", { ref: envVars.TRIGGER_PROJECT_REF });
}
watcher = await watchConfig({
cwd: options.cwd,
async onUpdate(config) {
logger.debug("Updated config, rerendering", { config });
if (devInstance) {
devInstance.stop();
}
devInstance = await bootDevSession(config);
},
overrides: {
project: options.projectRef ?? envVars.TRIGGER_PROJECT_REF,
},
configFile: options.config,
});
logger.debug("Initial config", watcher.config);
// eslint-disable-next-line no-inner-declarations
async function bootDevSession(configParam: ResolvedConfig) {
const projectClient = await getProjectClient({
accessToken: options.login.auth.accessToken,
apiUrl: options.login.auth.apiUrl,
projectRef: configParam.project,
env: "dev",
profile: options.profile,
});
if (!projectClient) {
process.exit(1);
}
return startDevSession({
name: projectClient.name,
rawArgs: options,
rawConfig: configParam,
client: projectClient.client,
initialMode: "local",
dashboardUrl: options.login.dashboardUrl,
showInteractiveDevSession: true,
keepTmpFiles: options.keepTmpFiles,
});
}
devInstance = await bootDevSession(watcher.config);
const waitUntilExit = async () => {};
return {
watcher,
stop: async () => {
devInstance?.stop();
await watcher?.stop();
removeLockFile();
},
waitUntilExit,
};
} catch (error) {
await watcher?.stop();
throw error;
}
}