forked from jameshartig/child-process-debug
-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.js
141 lines (134 loc) · 4.32 KB
/
debug.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var child_process = require('child_process'),
EventEmitter = require('events').EventEmitter,
myDebugBreak = global.v8DebugBreak,
myDebugPort = global.v8DebugPort;
function setChildInfo(port, info) {
if (global.debugChildProcesses === undefined) {
global.debugChildProcesses = {};
}
global.debugChildProcesses[port] = info;
}
function getChildInfo(port) {
if (global.debugChildProcesses === undefined) {
return undefined;
}
return global.debugChildProcesses[port];
}
function _getDebugPort(argv) {
var debugBreak = false,
debugIndex = -1,
port, i;
for (i = argv.length - 1; i >= 0; i--) {
if (typeof argv[i] !== 'string') {
continue;
}
if (argv[i] === '--inspect-brk') {
debugBreak = true;
continue;
}
if (debugIndex === -1 && argv[i].indexOf('--inspect') !== -1) {
port = parseInt(argv[i].substr(8), 10);
if (!port) {
port = 5858;
}
debugIndex = i;
}
}
//todo: stop using an array for this and use an object...
return [port, debugIndex, debugBreak];
}
//initially store the values we were run with in case someone changes them later or we increment them
if (myDebugPort === undefined) {
myDebugPort = _getDebugPort(process.execArgv)[0];
if (myDebugPort) {
//store this globally so we always have a record of the initial debug port for this process
global.v8DebugPort = myDebugPort;
}
}
if (myDebugBreak === undefined) {
myDebugBreak = _getDebugPort(process.execArgv)[2];
global.v8DebugBreak = myDebugBreak;
}
function incrementDebugPort(info) {
//start at end so we pick up the LAST debug statement
var portAndIndex = _getDebugPort(process.execArgv),
nextPort;
if (portAndIndex[0]) {
nextPort = portAndIndex[0] + 1;
process.execArgv.splice(portAndIndex[1], 1, '--inspect=' + nextPort);
if (info !== undefined) {
setChildInfo(nextPort, info);
}
}
return nextPort;
}
//shove the --inspect at the front of arguments
function wrapSpawnFork(method /*, file , args, options*/) {
var argsIndex = 2,
file = arguments[1],
args, options, debugPort, child,
argsPortBrk;
if (typeof file !== 'string') {
file = process.execPath;
argsIndex = 1;
}
if (Array.isArray(arguments[argsIndex])) {
args = arguments[argsIndex];
options = arguments[argsIndex + 1];
} else {
args = [];
options = arguments[argsIndex];
}
argsPortBrk = _getDebugPort(args);
debugPort = incrementDebugPort({args: args});
if (debugPort) {
//only add --debug=port when they didn't already add one
//if (!argsPortBrk[0]) {
args.unshift('--inspect=' + debugPort);
argsPortBrk[1] = 0;
//}
/* if (!argsPortBrk[2] && myDebugBreak) {
args.splice(argsPortBrk[1] + 1, 0, '--inspect-brk');
} */
}
//in case they add more params in the future, concat new args on
child = child_process[method].apply(child_process, [file, args, options].concat(Array.prototype.slice.call(arguments, 4)));
if (child instanceof EventEmitter) {
child.debugPort = debugPort || argsPortBrk[0];
}
return child;
}
exports.port = function(argv) {
if (argv === undefined) {
return myDebugPort;
}
return _getDebugPort(argv)[0];
};
exports.debugBreak = function(argv) {
if (argv === undefined) {
return myDebugBreak;
}
return _getDebugPort(argv)[2];
};
exports.spawn = function(file, args, options) {
return wrapSpawnFork('spawn', file, args, options);
};
exports.exitWithParent = function(child) {
if (typeof child.kill !== 'function') {
throw new TypeError('Invalid child sent to exitWithParent');
}
process.on('exit', child.kill.bind(child));
process.on('SIGTERM', child.kill.bind(child));
process.on('SIGINT', child.kill.bind(child));
};
exports.fork = function(modulePath, args, options) {
if (Array.isArray(modulePath)) {
throw new TypeError('Missing modulePath for fork');
}
return wrapSpawnFork('fork', modulePath, args, options);
};
exports.nextPort = incrementDebugPort;
/*
exports.setInfo = setChildInfo;
exports.getInfo = getChildInfo;
*/