-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpromised-exec.js
57 lines (41 loc) · 1.27 KB
/
promised-exec.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
//
// This code orginally from here, but modified to fit my needs.
//
// https://www.npmjs.com/package/promised-exec
//
'use strict';
var exec, getBufferContents;
exec = require('child_process').exec;
module.exports = function (command, options) {
function ExecError(message, stdout, stderr, code, origError) {
this.name = "ExecError";
this.message = (message || "");
this.stdout = stdout;
this.stderr = stderr;
this.code = code;
this.origError = origError;
}
ExecError.prototype = Error.prototype;
var q, deferred;
q = require('q');
if (!command || typeof command !== 'string') {
throw {
message: 'Command must be a string.'
};
}
deferred = q.defer();
var child = exec(command, options || {}, function (error, stdout, stderr) {
if (error) {
return deferred.reject(new ExecError('Error running cmd ' + command, stdout, stderr, error.code, error));
}
deferred.resolve({
stdout: stdout.toString('utf8'),
stderr: stderr.toString('utf8')
});
});
if (child && options && options.stdin) {
child.stdin.write(options.stdin, 'utf8');
child.stdin.end();
}
return deferred.promise;
};