forked from triggerdotdev/trigger.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
96 lines (81 loc) · 2.92 KB
/
common.ts
File metadata and controls
96 lines (81 loc) · 2.92 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
import { outro } from "@clack/prompts";
import { Command } from "commander";
import { z } from "zod";
import { fromZodError } from "zod-validation-error";
import { BundleError } from "../build/bundle.js";
import { CLOUD_API_URL } from "../consts.js";
import { chalkError } from "../utilities/cliOutput.js";
import { readAuthConfigCurrentProfileName } from "../utilities/configFiles.js";
import { logger } from "../utilities/logger.js";
import { trace } from "@opentelemetry/api";
export const CommonCommandOptions = z.object({
apiUrl: z.string().optional(),
logLevel: z.enum(["debug", "info", "log", "warn", "error", "none"]).default("log"),
skipTelemetry: z.boolean().default(false),
profile: z.string().default(readAuthConfigCurrentProfileName()),
ignoreEngines: z.boolean().default(false),
});
export type CommonCommandOptions = z.infer<typeof CommonCommandOptions>;
export function commonOptions(command: Command) {
return command
.option("--profile <profile>", "The login profile to use", readAuthConfigCurrentProfileName())
.option("-a, --api-url <value>", "Override the API URL", CLOUD_API_URL)
.option(
"-l, --log-level <level>",
"The CLI log level to use (debug, info, log, warn, error, none). This does not effect the log level of your trigger.dev tasks.",
"log"
)
.option("--skip-telemetry", "Opt-out of sending telemetry");
}
export class SkipLoggingError extends Error { }
export class SkipCommandError extends Error { }
export class OutroCommandError extends SkipCommandError { }
export async function handleTelemetry(action: () => Promise<void>) {
try {
await action();
} catch (e) {
process.exitCode = 1;
}
}
export async function wrapCommandAction<T extends z.AnyZodObject, TResult>(
name: string,
schema: T,
options: unknown,
action: (opts: z.output<T>) => Promise<TResult>
): Promise<TResult | undefined> {
try {
const parsedOptions = schema.safeParse(options);
if (!parsedOptions.success) {
throw new Error(fromZodError(parsedOptions.error).toString());
}
logger.loggerLevel = parsedOptions.data.logLevel;
logger.debug(`Running "${name}" with the following options`, {
options: options,
});
const result = await action(parsedOptions.data);
return result;
} catch (e) {
if (e instanceof SkipLoggingError) {
// do nothing
} else if (e instanceof OutroCommandError) {
outro(e.message ?? "Operation cancelled");
process.exit(1);
} else if (e instanceof SkipCommandError) {
// do nothing
} else if (e instanceof BundleError) {
process.exit(1);
} else {
logger.log(`${chalkError("X Error:")} ${e instanceof Error ? e.message : String(e)}`);
}
throw e;
}
}
export const tracer = trace.getTracer("trigger.dev/cli");
export function installExitHandler() {
process.on("SIGINT", () => {
process.exit(0);
});
process.on("SIGTERM", () => {
process.exit(0);
});
}