forked from mariocasciaro/spawned
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (55 loc) · 1.67 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
var os = require('os'),
spawn = require('child_process').spawn,
exec = require('child_process').exec,
when = require('when'),
util = require('util'),
through2 = require('through2'),
Buffers = require("buffers");
var self = module.exports = function(command, args, options) {
var deferred = when.defer();
if(args && !util.isArray(args)) {
options = args;
}
args = args || [];
options = options || {};
var proc = os.platform() === 'win32' ? exec(command + ' ' + args, options, null) : spawn(command, args, options);
var bufferCombined = new Buffers();
var bufferErr = new Buffers();
var bufferOut = new Buffers();
var outStream = proc.stdout.pipe(through2(function(chunk, enc, callback) {
bufferCombined.push(chunk);
bufferOut.push(chunk);
this.push(chunk);
callback();
}));
if(options.out) {
outStream.pipe(options.out);
}
var errStream = proc.stderr.pipe(through2(function(chunk, enc, callback) {
bufferCombined.push(chunk);
bufferErr.push(chunk);
this.push(chunk);
callback();
}));
if(options.err) {
errStream.pipe(options.err);
}
proc.on('close', function (code) {
if(code !== 0) {
var err = new Error("Command '" + command + "' exited with code " + code);
err.code = code;
err.out = bufferOut.toString("utf8");
err.err = bufferErr.toString("utf8");
err.combined = bufferCombined.toString("utf8");
deferred.reject(err);
} else {
deferred.resolve({
code: code,
out: bufferOut.toString("utf8"),
err: bufferErr.toString("utf8"),
combined: bufferCombined.toString("utf8")
});
}
});
return deferred.promise;
};