forked from inikulin/bin-v8-flags-filter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
113 lines (92 loc) · 2.85 KB
/
index.js
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
var spawn = require('child_process').spawn;
var FLAGS = [
'debug',
'--debug',
'--debug-brk',
'--inspect',
'--inspect-brk',
'--expose-gc',
'--gc-global',
'--es_staging',
'--no-deprecation',
'--prof',
'--log-timer-events',
'--throw-deprecation',
'--trace-deprecation',
'--use_strict',
'--allow-natives-syntax',
'--perf-basic-prof',
'--experimental-repl-await',
'--experimental-loader',
'--no-warnings'
];
var FLAG_PREFIXES = [
'--harmony',
'--trace',
'--icu-data-dir',
'--max-old-space-size',
'--preserve-symlinks'
];
var DEFAULT_FORCED_KILL_DELAY = 30000;
function getChildArgs (cliPath, ignore) {
var args = [cliPath];
process.argv.slice(2).forEach(function (arg) {
var flag = arg.split('=')[0];
if (ignore.indexOf(flag) > -1) {
args.push(arg);
return;
}
if (FLAGS.indexOf(flag) > -1) {
args.unshift(arg);
return;
}
for (var i = 0; i < FLAG_PREFIXES.length; i++) {
if (arg.indexOf(FLAG_PREFIXES[i]) === 0) {
args.unshift(arg);
return;
}
}
args.push(arg);
});
return args;
}
function setupSignalHandler (signal, childProcess, useShutdownMessage, forcedKillDelay) {
function forceKill () {
childProcess.kill('SIGTERM');
}
function handler () {
if (useShutdownMessage)
childProcess.send('shutdown');
else
childProcess.kill(signal);
setTimeout(forceKill, forcedKillDelay).unref();
}
process.on(signal, handler);
}
module.exports = function (cliPath, opts) {
var useShutdownMessage = opts && opts.useShutdownMessage;
var forcedKillDelay = opts && opts.forcedKillDelay || DEFAULT_FORCED_KILL_DELAY;
var ignore = opts && opts.ignore || [];
var args = getChildArgs(cliPath, ignore);
if (opts.forcedArgs && opts.forcedArgs.length > 0)
args.unshift(...opts.forcedArgs);
var cliProc = spawn(process.execPath, args, { stdio: [process.stdin, process.stdout, process.stderr, useShutdownMessage ? 'ipc' : null] });
cliProc.on('exit', function (code, signal) {
if (useShutdownMessage && process.disconnect)
process.disconnect();
process.on('exit', function () {
if (signal)
process.kill(process.pid, signal);
else
process.exit(code);
});
});
setupSignalHandler('SIGINT', cliProc, useShutdownMessage, forcedKillDelay);
setupSignalHandler('SIGBREAK', cliProc, useShutdownMessage, forcedKillDelay);
if (useShutdownMessage) {
process.on('message', function (message) {
if (message === 'shutdown')
cliProc.send('shutdown');
});
}
};