forked from darkguy2008/parallelshell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·161 lines (150 loc) · 4.28 KB
/
index.js
File metadata and controls
executable file
·161 lines (150 loc) · 4.28 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env node
//'use strict';
var spawn = require('child_process').spawn;
var sh, shFlag, children, args, cmds, i ,len;
verbose = false;
kill = false;
wait = false;
// 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 '-k':
case '--kill':
kill = 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('-k, --kill kill all children when one has exited')
process.exit();
break;
}
} else {
cmds.push(args[i]);
}
}
// 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 (kill)
{
if (verbose) console.log("Running SIGINT on all children");
close(code);
}
else
{
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, closed = 0, opened = 0;
for (i = 0, len = children.length; i < len; i++) {
if (!children[i].exitCode) {
opened++;
children[i].removeAllListeners('close');
try {
process.kill(-children[i].pid, "SIGINT");
} catch (err) {
//Do not handle error process.kill is apparently a GREAT peace of shit
}
if (verbose) console.log('`' + children[i].cmd + '` will now be closed');
children[i].on('close', function() {
closed++;
if (opened == closed) {
process.exit(code);
}
});
}
}
if (opened == closed) {process.exit(code);}
}
// cross platform compatibility
if (process.platform === 'win32') {
sh = 'cmd';
shFlag = '/c';
} else {
sh = 'sh';
shFlag = '-c';
}
// start the children
children = [];
cmds.forEach(function (cmd) {
if (process.platform != 'win32') {
cmd = "exec "+cmd;
}
var child = spawn(sh,[shFlag,cmd], {
cwd: checkNodeVersion('8.0.0', process.versions.node) ? process.cwd() : process.cwd,
env: process.env,
stdio: ['pipe', process.stdout, process.stderr],
detached: true
})
.on('close', childClose);
child.cmd = cmd
children.push(child)
});
// close all children on ctrl+c
process.on('SIGINT', close)
/* Return true if version >= minimumRequired
* @string minimumRequired example : 8.0.0
* @string version example : 10.0.0
*/
function checkNodeVersion(minimumRequired, version) {
var minVer = minimumRequired.split('.')
var ver = version.split('.')
var result = false;
for (var i in minVer) {
var min = parseInt(minVer[i])
if (!ver[i]) { break ; }
var vers = parseInt(ver[i])
if (vers > min) {
return true
}
else if (vers == min) {
result = true
}
else {
return false
}
}
return result
}