-
Notifications
You must be signed in to change notification settings - Fork 3
/
shellpromise.js
44 lines (39 loc) · 1.09 KB
/
shellpromise.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
'use strict';
var spawn = require('child_process').spawn;
function shellpromise (processToRun, options) {
options = options || {};
if (options.verbose) {
console.log("shellpromise: about to spawn " + processToRun);
}
return new Promise(function(resolve, reject) {
var local = spawn('sh', ['-c', processToRun], { env: options.env || process.env, cwd: options.cwd || process.cwd() });
var output = "";
function toStdErr(data) {
output += data;
if (options.verbose) {
console.warn("shellpromise: error: " + data.toString());
}
}
function toStdOut(data) {
output += data;
if (options.verbose) {
console.log("shellpromise: output: " + data.toString());
}
}
local.stdout.on('data', toStdOut);
local.stderr.on('data', toStdErr);
local.on('error', reject);
local.on('close', function(code) {
if (code === 0) {
resolve(output);
} else {
if (options.verbose) {
console.warn(processToRun + ' exited with exit code ' + code);
}
reject(new Error(output));
}
});
});
};
module.exports = shellpromise;
module.exports.shellpromise = shellpromise;