-
Notifications
You must be signed in to change notification settings - Fork 44
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
Dev #30
base: master
Are you sure you want to change the base?
Dev #30
Changes from all commits
fcb6a74
5177e24
dc93829
1b69450
31728f6
507552f
6d26a35
a044ea6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
'use strict'; | ||
var spawn = require('child_process').spawn; | ||
|
||
var sh, shFlag, children, args, wait, cmds, verbose, i ,len; | ||
var sh, shFlag, children, args, wait, first, cmds, verbose, i ,len; | ||
// parsing argv | ||
cmds = []; | ||
args = process.argv.slice(2); | ||
|
@@ -14,15 +14,20 @@ for (i = 0, len = args.length; i < len; i++) { | |
case '--wait': | ||
wait = true; | ||
break; | ||
case '-f': | ||
case '--first': | ||
first = true; | ||
break; | ||
case '-v': | ||
case '--verbose': | ||
verbose = true; | ||
break; | ||
case '-h': | ||
case '--help': | ||
console.log('-h, --help output usage information'); | ||
console.log('-v, --verbose verbose logging') | ||
console.log('-w, --wait will not close sibling processes on error') | ||
console.log('-v, --verbose verbose logging'); | ||
console.log('-w, --wait will not close sibling processes on error'); | ||
console.log('-f, --first close all sibling processes after first exits (succes/error)'); | ||
process.exit(); | ||
break; | ||
} | ||
|
@@ -31,55 +36,65 @@ for (i = 0, len = args.length; i < len; i++) { | |
} | ||
} | ||
|
||
if (wait && first) { | ||
console.error('--wait and --first cannot be used together'); | ||
process.exit(1); | ||
} | ||
|
||
// called on close of a child process | ||
function childClose (code) { | ||
var i, len; | ||
code = code ? (code.code || code) : code; | ||
if (verbose) { | ||
if (code > 0) { | ||
console.error('`' + this.cmd + '` failed with exit code ' + code); | ||
console.error('parallelshell: `' + this.cmd + '` failed with exit code ' + code); | ||
} else { | ||
console.log('`' + this.cmd + '` ended successfully'); | ||
console.log('parallelshell: `' + this.cmd + '` ended successfully'); | ||
} | ||
} | ||
if (code > 0 && !wait) close(code); | ||
status(); | ||
if (first || code > 0 && !wait) close(code); | ||
} | ||
|
||
function status () { | ||
if (verbose) { | ||
var i, len; | ||
console.log('\n'); | ||
console.log('### Status ###'); | ||
console.log('parallelshell: Status'); | ||
for (i = 0, len = children.length; i < len; i++) { | ||
if (children[i].exitCode === null) { | ||
console.log('`' + children[i].cmd + '` is still running'); | ||
console.log('parallelshell: `' + children[i].cmd + '` is still running'); | ||
} else if (children[i].exitCode > 0) { | ||
console.log('`' + children[i].cmd + '` errored'); | ||
console.log('parallelshell: `' + children[i].cmd + '` errored'); | ||
} else { | ||
console.log('`' + children[i].cmd + '` finished'); | ||
console.log('parallelshell: `' + children[i].cmd + '` finished'); | ||
} | ||
} | ||
console.log('\n'); | ||
} | ||
} | ||
|
||
// closes all children and the process | ||
function close (code) { | ||
var i, len, closed = 0, opened = 0; | ||
var i, len, closeHandler, closed = 0, opened = 0; | ||
|
||
for (i = 0, len = children.length; i < len; i++) { | ||
if (!children[i].exitCode) { | ||
if (children[i].exitCode === null) { | ||
opened++; | ||
children[i].removeAllListeners('close'); | ||
children[i].kill("SIGINT"); | ||
if (verbose) console.log('`' + children[i].cmd + '` will now be closed'); | ||
children[i].on('close', function() { | ||
closed++; | ||
if (opened == closed) { | ||
process.exit(code); | ||
} | ||
}); | ||
if (process.platform != "win32") { | ||
spawn(sh, [shFlag, "kill -INT -"+children[i].pid]); | ||
} else { | ||
children[i].kill("SIGINT"); | ||
} | ||
if (verbose) console.log('parallelshell: `' + children[i].cmd + '` will now be closed'); | ||
closeHandler = function (child) { | ||
child.on('close', function() { | ||
if (verbose) console.log('parallelshell: `' + child.cmd + '` closed successfully'); | ||
closed++; | ||
if (opened == closed) { | ||
process.exit(code); | ||
} | ||
}); | ||
}(children[i]) | ||
|
||
} | ||
} | ||
if (opened == closed) {process.exit(code);} | ||
|
@@ -98,18 +113,27 @@ if (process.platform === 'win32') { | |
// start the children | ||
children = []; | ||
cmds.forEach(function (cmd) { | ||
if (process.platform != 'win32') { | ||
cmd = "exec "+cmd; | ||
if (process.platform === 'win32') { | ||
cmd = cmd.replace(/'/g,"\""); | ||
} | ||
var child = spawn(sh,[shFlag,cmd], { | ||
cwd: process.cwd, | ||
env: process.env, | ||
stdio: ['pipe', process.stdout, process.stderr] | ||
stdio: ['pipe', process.stdout, process.stderr], | ||
windowsVerbatimArguments: process.platform === 'win32', | ||
detached: process.platform != 'win32' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see any output when I run commands now. Seems this line is the culprit. Why are we detaching the process? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So they can be killed properly and we don't end up with zombie processes. I actually think L122 is the culprit more than L124 |
||
}) | ||
.on('close', childClose); | ||
child.cmd = cmd | ||
children.push(child) | ||
}); | ||
|
||
// close all children on ctrl+c | ||
process.on('SIGINT', close) | ||
process.on('SIGINT', function() { | ||
if (verbose) console.log('parallelshell: recieved SIGINT'); | ||
close(); | ||
}); | ||
|
||
process.on('exit', function(code) { | ||
if (verbose) console.log('parallelshell: exit code:', code); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
succes is spelled success.