1 | var spawn = require('child_process').spawn;
|
2 |
|
3 | var FLAGS = [
|
4 | 'debug',
|
5 | '--debug',
|
6 | '--debug-brk',
|
7 | '--inspect',
|
8 | '--expose-gc',
|
9 | '--gc-global',
|
10 | '--es_staging',
|
11 | '--no-deprecation',
|
12 | '--prof',
|
13 | '--log-timer-events',
|
14 | '--throw-deprecation',
|
15 | '--trace-deprecation',
|
16 | '--use_strict',
|
17 | '--allow-natives-syntax',
|
18 | '--perf-basic-prof'
|
19 | ];
|
20 |
|
21 | var FLAG_PREFIXES = [
|
22 | '--harmony',
|
23 | '--trace',
|
24 | '--icu-data-dir',
|
25 | '--max-old-space-size',
|
26 | '--preserve-symlinks'
|
27 | ];
|
28 |
|
29 | var DEFAULT_FORCED_KILL_DELAY = 30000;
|
30 |
|
31 | module.exports = function (cliPath, opts) {
|
32 | var useShutdownMessage = opts && opts.useShutdownMessage;
|
33 | var forcedKillDelay = opts && opts.forcedKillDelay || DEFAULT_FORCED_KILL_DELAY;
|
34 | var args = [cliPath];
|
35 |
|
36 | process.argv.slice(2).forEach(function (arg) {
|
37 | var flag = arg.split('=')[0];
|
38 |
|
39 | if (FLAGS.indexOf(flag) > -1) {
|
40 | args.unshift(arg);
|
41 | return;
|
42 | }
|
43 |
|
44 | for (var i = 0; i < FLAG_PREFIXES.length; i++) {
|
45 | if (arg.indexOf(FLAG_PREFIXES[i]) === 0) {
|
46 | args.unshift(arg);
|
47 | return;
|
48 | }
|
49 | }
|
50 |
|
51 | args.push(arg);
|
52 | });
|
53 |
|
54 | var cliProc = spawn(process.execPath, args, { stdio: [process.stdin, process.stdout, process.stderr, useShutdownMessage ? 'ipc' : null] });
|
55 |
|
56 | cliProc.on('exit', function (code, signal) {
|
57 | if (useShutdownMessage && process.disconnect)
|
58 | process.disconnect();
|
59 |
|
60 | process.on('exit', function () {
|
61 | if (signal)
|
62 | process.kill(process.pid, signal);
|
63 | else
|
64 | process.exit(code);
|
65 | });
|
66 | });
|
67 |
|
68 | process.on('SIGINT', function () {
|
69 | function forceKill () {
|
70 | cliProc.kill('SIGTERM');
|
71 | }
|
72 |
|
73 | if (useShutdownMessage)
|
74 | cliProc.send('shutdown');
|
75 | else
|
76 | cliProc.kill('SIGINT');
|
77 |
|
78 | setTimeout(forceKill, forcedKillDelay).unref();
|
79 | });
|
80 |
|
81 | if (useShutdownMessage) {
|
82 | process.on('message', function (message) {
|
83 | if (message === 'shutdown')
|
84 | cliProc.send('shutdown');
|
85 | });
|
86 | }
|
87 | };
|