Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·84 lines (77 loc) · 2.64 KB
/
Copy pathcli.js
File metadata and controls
executable file
·84 lines (77 loc) · 2.64 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
#!/usr/bin/env node
'use strict'
void (async () => {
const Module = require('node:module')
const os = require('node:os')
const path = require('node:path')
const rootPath = path.join(__dirname, '..')
Module.enableCompileCache?.(path.join(rootPath, '.cache'))
const { default: constants } = require(
path.join(rootPath, 'dist/constants.js'),
)
const { spawn } = require(
path.join(rootPath, 'external/@socketsecurity/registry/lib/spawn.js'),
)
process.exitCode = 1
const spawnPromise = spawn(
constants.execPath,
[
...constants.nodeNoWarningsFlags,
...constants.nodeDebugFlags,
...constants.nodeHardenFlags,
...constants.nodeMemoryFlags,
...(constants.ENV.INLINED_SOCKET_CLI_SENTRY_BUILD
? ['--require', constants.instrumentWithSentryPath]
: []),
constants.distCliPath,
...process.argv.slice(2),
],
{
env: {
...process.env,
...constants.processEnv,
},
stdio: 'inherit',
},
)
// The child shares our process group and handles the signal itself; wait briefly for it
// to exit (so its final output isn't printed after the prompt returns) and mirror its
// exit below. SIGKILL and leave if it outlasts the grace, or on a second signal.
const SHUTDOWN_GRACE_MS = 3_000
const hardAbort = signalName => {
const child = spawnPromise.process
if (child.exitCode === null && child.signalCode === null) {
child.kill('SIGKILL')
}
// eslint-disable-next-line n/no-process-exit
process.exit(signalName === 'SIGTERM' ? 143 : 130)
}
let sawSignal = false
const onSignal = signalName => {
if (sawSignal) {
hardAbort(signalName)
return
}
sawSignal = true
setTimeout(() => hardAbort(signalName), SHUTDOWN_GRACE_MS).unref?.()
}
const onSigint = () => onSignal('SIGINT')
const onSigterm = () => onSignal('SIGTERM')
process.on('SIGINT', onSigint)
process.on('SIGTERM', onSigterm)
// See https://nodejs.org/api/child_process.html#event-exit.
spawnPromise.process.on('exit', (code, signalName) => {
if (signalName) {
// Mirror a signal death as the conventional 128 + signum exit code. Exit explicitly
// rather than re-raising the signal: with our handlers installed the re-raise would
// race `await spawnPromise` resolving and could leave the default exitCode of 1.
const signum = os.constants.signals[signalName] ?? 0
// eslint-disable-next-line n/no-process-exit
process.exit(128 + signum)
} else if (typeof code === 'number') {
// eslint-disable-next-line n/no-process-exit
process.exit(code)
}
})
await spawnPromise
})()