Skip to content

fix for #12 #14

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

Merged
merged 2 commits into from
May 5, 2015
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ This will execute the commands `echo 1` `echo 2` and `echo 3` simultaneously.

Note that on Windows, you need to use double-quotes to avoid confusing the
argument parser.

Available options:
```
-h, --help output usage information
-v, --verbose verbose logging
-w, --wait will not close silbling processes on error

```
97 changes: 86 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,101 @@
'use strict';
var spawn = require('child_process').spawn;

function potentialExit (childCmd, code) {
code = code? (code.code || code) : code;
if (code > 0) {
console.error('`' + childCmd + '` failed with exit code ' + code);
process.exit(code);
var sh, shFlag, children, args, wait, cmds, verbose, i ,len;
// parsing argv
cmds = [];
args = process.argv.slice(2);
for (i = 0, len = args.length; i < len; i++) {
if (args[i][0] === '-') {
switch (args[i]) {
case '-w':
case '--wait':
wait = 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 silbling processes on error')
process.exit();
break;
}
} else {
cmds.push(args[i]);
}
}
var sh = 'sh';
var shFlag = '-c';

// 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);
} else {
console.log('`' + this.cmd + '` ended successfully');
}
}
if (code > 0 && !wait) close(code);
status();
}

function status () {
if (verbose) {
var i, len;
console.log('\n');
console.log('### Status ###');
for (i = 0, len = children.length; i < len; i++) {
if (children[i].exitCode === null) {
console.log('`' + children[i].cmd + '` is still running');
} else if (children[i].exitCode > 0) {
console.log('`' + children[i].cmd + '` errored');
} else {
console.log('`' + children[i].cmd + '` finished');
}
}
console.log('\n');
}
}

// closes all children and the process
function close (code) {
var i, len;
for (i = 0, len = children.length; i < len; i++) {
if (!children[i].exitCode) {
children[i].removeAllListeners('close');
children[i].kill('SIGINT');
if (verbose) console.log('`' + children[i].cmd + '` will now be closed');
}
}
process.exit(code);
}

// cross platform compatibility
if (process.platform === 'win32') {
sh = 'cmd';
shFlag = '/c';
} else {
sh = 'sh';
shFlag = '-c';
}
process.argv.slice(2).forEach(function (childCmd) {
var child = spawn(sh,[shFlag,childCmd], {

// start the children
children = [];
cmds.forEach(function (cmd) {
var child = spawn(sh,[shFlag,cmd], {
cwd: process.cwd,
env: process.env,
stdio: ['pipe', process.stdout, process.stderr]
})
.on('error', potentialExit.bind(null, childCmd))
.on('exit', potentialExit.bind(null, childCmd));
.on('close', childClose);
child.cmd = cmd
children.push(child)
});

// close all children on ctrl+c
process.on('SIGINT', close)
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
"parallelshell": "./index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha"
},
"keywords": [
"parallel",
"shell"
],
"author": "Keith Cirkel <npm@keithcirkel.co.uk> (http://keithcirkel.co.uk/)",
"license": "MIT"
"license": "MIT",
"devDependencies": {
"bluebird": "^2.9.25",
"chai": "^2.3.0",
"coffee-script": "^1.9.2",
"mocha": "^2.2.4"
}
}
84 changes: 84 additions & 0 deletions test/index.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
chai = require "chai"
should = chai.should()
spawn = require("child_process").spawn;
Promise = require("bluebird")

# cross platform compatibility
if process.platform == "win32"
sh = "cmd";
shFlag = "/c";
else
sh = "sh";
shFlag = "-c";


# children
waitingProcess = "\\\"node -e 'setTimeout(function(){},10000);'\\\""
failingProcess = "\\\"node -e 'throw new Error(\"someError\");'\\\""

usageInfo = """
-h, --help output usage information
-v, --verbose verbose logging
-w, --wait will not close silbling processes on error
""".split("\n")

spawnParallelshell = (cmd) ->
return spawn sh, [shFlag, "node './index.js' " + cmd], {
cwd: process.cwd
}

testOutput = (cmd, expectedOutput) ->
return new Promise (resolve) ->
ps = spawnParallelshell(cmd)
ps.stdout.setEncoding("utf8")
output = []
ps.stdout.on "data", (data) ->
lines = data.split("\n")
lines.pop() if lines[lines.length-1] == ""
output = output.concat(lines)
ps.stdout.on "end", () ->
for line,i in output
line.should.equal expectedOutput[i]
resolve()

describe "parallelshell", ->
it "should print on -h and --help", (done) ->
Promise.all([testOutput("-h", usageInfo), testOutput("-help", usageInfo)])
.finally done

it "should close with exitCode 2 on child error", (done) ->
ps = spawnParallelshell(failingProcess)
ps.on "close", () ->
ps.exitCode.should.equal 2
done()

it "should run with a normal child", (done) ->
ps = spawnParallelshell(waitingProcess)
setTimeout (() ->
should.not.exist(ps.signalCode)
ps.kill()
done()
),100

it "should close silbling processes on child error", (done) ->
ps = spawnParallelshell([waitingProcess,failingProcess,waitingProcess].join(" "))
ps.on "close", () ->
ps.exitCode.should.equal 2
done()

it "should wait for silbling processes on child error when called with -w or --wait", (done) ->
ps = spawnParallelshell(["-w",waitingProcess,failingProcess,waitingProcess].join(" "))
ps2 = spawnParallelshell(["--wait",waitingProcess,failingProcess,waitingProcess].join(" "))
setTimeout (() ->
should.not.exist(ps.signalCode)
should.not.exist(ps2.signalCode)
ps.kill()
ps2.kill()
done()
),100
it "should close on CTRL+C / SIGINT", (done) ->
ps = spawnParallelshell(["-w",waitingProcess,failingProcess,waitingProcess].join(" "))
ps.on "close", () ->
ps.signalCode.should.equal "SIGINT"
done()
ps.kill("SIGINT")
2 changes: 2 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--compilers coffee:coffee-script/register
--timeout 500