-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (36 loc) · 1.36 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
'use strict';
var exec = require('child_process').exec;
var spawn = require('cross-spawn-async');
var promisifyEvent = require('promisify-event');
var toArgv = require('shell-quote').parse;
var Promise = require('pinkie-promise');
module.exports = {
exec: function (command) {
return new Promise(function (resolve, reject) {
exec(command, function (err, stdout) {
if (err)
reject(err);
else
resolve(stdout.trim());
}).unref();
});
},
spawn: function (command, silent) {
var args = toArgv(command);
command = args.shift();
var stdio = silent ? 'ignore' : 'inherit';
var proc = spawn(command, args, { stdio: stdio });
proc.unref();
var error = promisifyEvent(proc, 'error')
.catch(function (err) {
throw new Error('Command `' + command + '` thrown error: \n' + err.message);
});
var completion = promisifyEvent(proc, 'exit')
.then(function (code) {
code = Array.isArray(code) ? code[0] : code;
if (code !== 0)
throw new Error('Command `' + command + '` exited with code ' + code + '.');
});
return Promise.race([completion, error]);
}
};