forked from TypeStrong/ts-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin.ts
More file actions
89 lines (78 loc) · 2.39 KB
/
bin.ts
File metadata and controls
89 lines (78 loc) · 2.39 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
#!/usr/bin/env node
import { spawn } from 'child_process'
import { join } from 'path'
import v8flags = require('v8flags')
const argv = process.argv.slice(2)
const signals: NodeJS.Signals[] = ['SIGINT', 'SIGTERM', 'SIGWINCH']
v8flags(function (err, v8flags) {
if (err) {
console.error(err.stack)
process.exit(1)
return
}
const nodeArgs: string[] = []
const scriptArgs: string[] = []
const knownFlags = v8flags.concat([
'debug',
'inspect',
'--debug',
'--debug-brk',
'--inspect',
'--inspect-brk',
'--nolazy',
'--no-deprecation',
'--log-timer-events',
'--throw-deprecation',
'--trace-deprecation',
'--allow-natives-syntax',
'--perf-basic-prof',
'--preserve-symlinks',
'--expose-gc',
'--expose-http2',
'--trace-warnings'
])
for (let i = 0; i < argv.length; i++) {
const arg = argv[i]
const flag = arg.split('=', 1)[0]
if (knownFlags.indexOf(flag) > -1) {
nodeArgs.push(arg)
} else if (/^-/.test(flag)) {
scriptArgs.push(arg)
} else {
// Break when we encounter a "script".
scriptArgs.push(...argv.slice(i))
break
}
}
const proc = spawn(
process.execPath,
nodeArgs.concat(join(__dirname, '_bin.js'), scriptArgs),
{
// We need to run in detached mode so to avoid
// automatic propagation of signals to the child process.
// This is necessary because by default, keyboard interrupts
// are propagated to the process tree, but `kill` is not.
//
// See: https://nodejs.org/api/child_process.html#child_process_options_detached
//
// This fix is not being required on Windows; besides, detached mode
// runs differently on Windows than on other platforms, and it would break
// the behavior of this application. See https://github.com/TypeStrong/ts-node/issues/480
// for more details.
detached: process.platform !== 'win32',
stdio: 'inherit'
}
)
// Ignore signals, and instead forward them to the child process.
signals.forEach(signal => process.on(signal, () => proc.kill(signal)))
// On spawned close, exit this process with the same code.
proc.on('close', (code: number, signal: string) => {
if (signal) {
process.kill(process.pid, signal)
} else {
process.exit(code)
}
})
// If this process exits, kill the child first.
process.on('exit', () => proc.kill())
})