Skip to content

Commit f094e9a

Browse files
committed
moved to use exec on unix
1 parent 3b29f57 commit f094e9a

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

index.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,7 @@ function close (code) {
7272
if (!children[i].exitCode) {
7373
opened++;
7474
children[i].removeAllListeners('close');
75-
if (process.platform === 'win32') {
76-
children[i].kill("SIGINT");
77-
} else {
78-
spawn(sh,[shFlag,'kill -TERM -'+children[i].pid]);
79-
}
75+
children[i].kill("SIGINT");
8076
if (verbose) console.log('`' + children[i].cmd + '` will now be closed');
8177
children[i].on('close', function() {
8278
closed++;
@@ -86,7 +82,7 @@ function close (code) {
8682
});
8783
}
8884
}
89-
if (opened == closed) process.exit(code);
85+
if (opened == closed) {process.exit(code);}
9086

9187
}
9288

@@ -102,10 +98,12 @@ if (process.platform === 'win32') {
10298
// start the children
10399
children = [];
104100
cmds.forEach(function (cmd) {
101+
if (process.platform != 'win32') {
102+
cmd = "exec "+cmd;
103+
}
105104
var child = spawn(sh,[shFlag,cmd], {
106105
cwd: process.cwd,
107106
env: process.env,
108-
detached: true,
109107
stdio: ['pipe', process.stdout, process.stderr]
110108
})
111109
.on('close', childClose);

test/index.coffee

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ should = chai.should()
33
spawn = require("child_process").spawn
44
Promise = require("bluebird")
55

6+
verbose = 0
7+
68
# cross platform compatibility
79
if process.platform == "win32"
810
sh = "cmd"
9-
shFlag = "/c"
11+
shArg = "/c"
1012
else
1113
sh = "sh"
12-
shFlag = "-c"
13-
14+
shArg = "-c"
1415

1516
# children
1617
waitingProcess = "\"node -e 'setTimeout(function(){},10000);'\""
@@ -22,53 +23,61 @@ usageInfo = """
2223
-w, --wait will not close sibling processes on error
2324
""".split("\n")
2425

26+
cmdWrapper = (cmd) ->
27+
if process.platform != "win32"
28+
cmd = "exec "+cmd
29+
if verbose
30+
console.log "Calling: "+cmd
31+
return cmd
32+
2533
spawnParallelshell = (cmd) ->
26-
return spawn sh, [shFlag, "node './index.js' " + cmd], {
27-
detached: true,
34+
return spawn sh, [shArg, cmdWrapper("node ./index.js "+cmd )], {
2835
cwd: process.cwd
2936
}
3037

3138
killPs = (ps) ->
32-
if process.platform == 'win32'
33-
ps.kill "SIGINT"
34-
else
35-
spawn(sh,[shFlag,"kill -INT -"+ps.pid])
36-
37-
spyOnPs = (ps) ->
38-
ps.stdout.setEncoding("utf8")
39-
ps.stdout.on "data", (data) ->
40-
console.log data
41-
ps.stderr.setEncoding("utf8")
42-
ps.stderr.on "data", (data) ->
43-
console.log "err: "+data
39+
ps.kill "SIGINT"
40+
41+
spyOnPs = (ps, verbosity=1) ->
42+
if verbose >= verbosity
43+
ps.stdout.setEncoding("utf8")
44+
ps.stdout.on "data", (data) ->
45+
console.log data
46+
ps.stderr.setEncoding("utf8")
47+
ps.stderr.on "data", (data) ->
48+
console.log "err: "+data
4449

4550
testOutput = (cmd, expectedOutput) ->
4651
return new Promise (resolve) ->
4752
ps = spawnParallelshell(cmd)
53+
spyOnPs ps, 3
4854
ps.stdout.setEncoding("utf8")
4955
output = []
5056
ps.stdout.on "data", (data) ->
5157
lines = data.split("\n")
5258
lines.pop() if lines[lines.length-1] == ""
5359
output = output.concat(lines)
5460
ps.stdout.on "end", () ->
55-
for line,i in output
56-
line.should.equal expectedOutput[i]
61+
for line,i in expectedOutput
62+
line.should.equal output[i]
5763
resolve()
5864

5965
describe "parallelshell", ->
6066
it "should print on -h and --help", (done) ->
6167
Promise.all([testOutput("-h", usageInfo), testOutput("--help", usageInfo)])
62-
.finally done
68+
.then -> done()
69+
.catch done
6370

6471
it "should close with exitCode 1 on child error", (done) ->
6572
ps = spawnParallelshell(failingProcess)
73+
spyOnPs ps, 2
6674
ps.on "close", () ->
6775
ps.exitCode.should.equal 1
6876
done()
6977

7078
it "should run with a normal child", (done) ->
7179
ps = spawnParallelshell(waitingProcess)
80+
spyOnPs ps, 1
7281
ps.on "close", () ->
7382
ps.signalCode.should.equal "SIGINT"
7483
done()
@@ -81,13 +90,16 @@ describe "parallelshell", ->
8190

8291
it "should close sibling processes on child error", (done) ->
8392
ps = spawnParallelshell([waitingProcess,failingProcess,waitingProcess].join(" "))
93+
spyOnPs ps,2
8494
ps.on "close", () ->
8595
ps.exitCode.should.equal 1
8696
done()
8797

8898
it "should wait for sibling processes on child error when called with -w or --wait", (done) ->
8999
ps = spawnParallelshell(["-w",waitingProcess,failingProcess,waitingProcess].join(" "))
90100
ps2 = spawnParallelshell(["--wait",waitingProcess,failingProcess,waitingProcess].join(" "))
101+
spyOnPs ps,2
102+
spyOnPs ps2,2
91103
setTimeout (() ->
92104
should.not.exist(ps.signalCode)
93105
should.not.exist(ps2.signalCode)
@@ -96,9 +108,11 @@ describe "parallelshell", ->
96108
),50
97109
Promise.all [new Promise((resolve) -> ps.on("close",resolve)),
98110
new Promise (resolve) -> ps2.on("close",resolve)]
99-
.finally done
111+
.then -> done()
112+
.catch done
100113
it "should close on CTRL+C / SIGINT", (done) ->
101114
ps = spawnParallelshell(["-w",waitingProcess,failingProcess,waitingProcess].join(" "))
115+
spyOnPs ps,2
102116
ps.on "close", () ->
103117
ps.signalCode.should.equal "SIGINT"
104118
done()

0 commit comments

Comments
 (0)