-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.ts
More file actions
51 lines (49 loc) · 1.56 KB
/
start.ts
File metadata and controls
51 lines (49 loc) · 1.56 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
import { HELP, type ICliArgs, parseCliArgs } from './args.ts'
import { runRepl } from './run.ts'
const main = async (): Promise<void> => {
let args: ICliArgs
try {
args = parseCliArgs(process.argv.slice(2))
} catch (err) {
console.error((err as Error).message)
console.error('\nRun `dd-agent --help` for usage.')
process.exit(2)
}
if (args.help) {
process.stdout.write(HELP)
return
}
if (args.envFile) {
try {
process.loadEnvFile(args.envFile)
} catch (err) {
console.error(`Failed to load env file ${args.envFile}: ${(err as Error).message}`)
process.exit(2)
}
}
// Apply --flag overrides AFTER --env-file so flags always win, even when an
// env-file already set the same key. Mutating process.env is local to this
// CLI process; it never leaks out via inheritance because Node only forks
// child env from this point onward (we don't spawn anything user-visible).
for (const [k, v] of Object.entries(args.overrides)) {
process.env[k] = v
}
try {
await runRepl()
} catch (err) {
const msg = err instanceof Error ? err.message : String(err)
if (msg.startsWith('Missing required env var')) {
// Friendlier surface for the most common misconfiguration: the user
// started the CLI without --env-file and without exporting the vars.
console.error(
`${msg}\n\nRun \`dd-agent --help\` to see required env vars, or pass --env-file=<path>.`,
)
process.exit(2)
}
throw err
}
}
main().catch((err: unknown) => {
console.error(err)
process.exit(1)
})