Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use --inspect instead of --debug #4

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
# child-process-debug #
# child_process_inspect #

Convenience methods for debugging child processes in Node.JS. Child processes will be started with `--debug` if the
parent was started with `--debug` and the children will each get their own increasing port number based off the
parent's port number. The default port is 5858. If you add `--debug-brk` that will also get passed to the children.
Convenience methods for debugging child processes in Node.JS. Child processes will be started with `--inspect` if the
parent was started with `--inspect` and the children will each get their own increasing port number based off the
parent's port number. The default port is 5858. If you add `--inspect-brk` that will also get passed to the children.

## Example ##
```JS
var childProcessDebug = require('child-process-debug');
var childProcessDebug = require('child_process_inspect');

for (var i = 0; i < 4; i++) {
//if this script wasn't run with --debug this will spawn node example.js [0-3]
//if this script was run with --debug, this will spawn node --debug=[5859-5862] example.js [0-3]
//if this script wasn't run with --inspect this will spawn node example.js [0-3]
//if this script was run with --inspect, this will spawn node --inspect=[5859-5862] example.js [0-3]
childProcessDebug.spawn(['example.js', i]);
}
```

## Methods ##

### spawn([command][, args][, options]) ###
This takes the exact same arguments as `child_process.spawn` and if the parent had debugging turned on (via --debug),
This takes the exact same arguments as `child_process.spawn` and if the parent had debugging turned on (via --inspect),
it'll turn on debugging for the spawned child. `command` is also optional (unlike `child_process.spawn`) and defaults
to `process.execPath`.

The ChildProcess returned from spawn will have a property called `debugPort` indicating the debug port chosen for that
child or undefined.

### fork(modulePath [, args][, options]) ###
This takes the exact same arguments as `child_process.fork` and if the parent had debugging turned on (via --debug),
This takes the exact same arguments as `child_process.fork` and if the parent had debugging turned on (via --inspect),
it'll turn on debugging for the spawned child. Return is the same as `spawn` above.

### nextPort() ###
Returns the next debug port that comes after the current process's debug port. If the current process doesn't have
debug turned on then this will return undefined. This is useful if you're not using `spawn` and want to specify the
`--debug=port` argument yourself.
`--inspect=port` argument yourself.

### exitWithParent(child) ###
Kill's the spawned child when the parent dies. This will not work if the parent is killed with SIGKILL.
Expand All @@ -41,6 +41,6 @@ Kill's the spawned child when the parent dies. This will not work if the parent
Returns the current process's debug port or undefined if debug is not turned on.

### debugBreak() ###
Returns true if the current process has the flag `--debug-brk`.
Returns true if the current process has the flag `--inspect-brk`.

By [James Hartig](https://github.com/fastest963/)
By [ccckblaze based on James Hartig's work](https://github.com/fastest963/)
23 changes: 14 additions & 9 deletions debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@ function _getDebugPort(argv) {
if (typeof argv[i] !== 'string') {
continue;
}
if (argv[i] === '--debug-brk') {
if (argv[i] === '--inspect-brk') {
debugBreak = true;
continue;
}
if (debugIndex === -1 && argv[i].indexOf('--debug') !== -1) {
port = parseInt(argv[i].substr(8), 10);
if (!port) {
if (debugIndex === -1){
if(argv[i].indexOf('--inspect') !== -1) {
port = parseInt(argv[i].substr(10), 10);
if (!port) {
port = 5858;
}
}
else{
port = 5858;
}
debugIndex = i;
Expand Down Expand Up @@ -59,15 +64,15 @@ function incrementDebugPort(info) {
nextPort;
if (portAndIndex[0]) {
nextPort = portAndIndex[0] + 1;
process.execArgv.splice(portAndIndex[1], 1, '--debug=' + nextPort);
process.execArgv.splice(portAndIndex[1], 1, '--inspect=' + nextPort);
if (info !== undefined) {
setChildInfo(nextPort, info);
}
}
return nextPort;
}

//shove the --debug at the front of arguments
//shove the --inspect at the front of arguments
function wrapSpawnFork(method /*, file , args, options*/) {
var argsIndex = 2,
file = arguments[1],
Expand All @@ -87,13 +92,13 @@ function wrapSpawnFork(method /*, file , args, options*/) {
argsPortBrk = _getDebugPort(args);
debugPort = incrementDebugPort({args: args});
if (debugPort) {
//only add --debug=port when they didn't already add one
//only add --inspect=port when they didn't already add one
if (!argsPortBrk[0]) {
args.unshift('--debug=' + debugPort);
args.unshift('--inspect=' + debugPort);
argsPortBrk[1] = 0;
}
if (!argsPortBrk[2] && myDebugBreak) {
args.splice(argsPortBrk[1] + 1, 0, '--debug-brk');
args.splice(argsPortBrk[1] + 1, 0, '--inspect-brk');
}
}
//in case they add more params in the future, concat new args on
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "child-process-debug",
"version": "0.0.7",
"name": "child_process_inspect",
"version": "0.0.9",
"description": "Convenience methods for debugging child-process",
"main": "debug.js",
"directories": {
Expand All @@ -15,16 +15,18 @@
},
"repository": {
"type": "git",
"url": "git://github.com/fastest963/child-process-debug.git"
"url": "git://github.com/ccckblaze/child_process_inspect"
},
"keywords": [
"child_process",
"child",
"process",
"debug",
"debugger"
"debugger",
"inspect",
"inspect-brk"
],
"author": "James Hartig <fastest963@gmail.com>",
"author": "ccckblaze <ccckblaze@sina.com>",
"license": "MIT",
"readmeFilename": "README.md"
}