-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (60 loc) · 1.76 KB
/
index.js
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
/**
* Copyright: E2E Technologies Ltd
*/
'use strict';
var cp = require('child_process');
module.exports.spawn = spawn;
module.exports.fork = fork;
module.exports.exec = exec;
module.exports.execFile = execFile;
function spawn(command, args, options, callback) {
return run(cp.spawn.bind(this, command), args, options, callback);
}
function fork(modulePath, args, options, callback) {
return run(cp.fork.bind(this, modulePath), args, options, callback);
}
function exec(command, options, callback) {
return cp.exec(command, options, callback);
}
function execFile(file, args, options, callback) {
return cp.execFile(file, args, options, callback);
}
function run(func, args, options, callback) {
var childProcess;
var errors = [];
var output = [];
if (typeof args === 'function') {
callback = args;
args = [];
options = {};
} else if (typeof options === 'function') {
callback = options;
options = {};
}
if (!callback) {
callback = function () {};
}
childProcess = func(args, options);
if (childProcess.stdout) {
childProcess.stdout.on('data', function (data) {
output.push(data);
});
}
if (childProcess.stderr) {
childProcess.stderr.on('data', function (data) {
errors.push(data);
});
}
childProcess.on('error', callback);
childProcess.on('close', function (code, signal) {
if (code === 0 && errors.length === 0) {
callback(null, output.join(''));
} else {
var error = new Error(errors.join(''));
error.code = code;
if (output.length > 0) error.output = output.join('');
callback(error);
}
});
return childProcess;
}