-
Notifications
You must be signed in to change notification settings - Fork 6
/
promiseCommand.js
38 lines (34 loc) · 924 Bytes
/
promiseCommand.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
const spawnShell = require('spawn-shell');
const concat = require('concat-stream');
const argv = require('./arguments').get();
module.exports = function promiseCommand (command, opts={}) {
if (!argv.json) {
console.log('>>>>', command);
}
const pSpawn = spawnShell(command, Object.assign({
stdio: [0, 'pipe', 2],
env: process.env
}, opts))
const pOutput = new Promise((resolve, reject) => {
pSpawn.stdout.pipe(concat(
{encoding: 'string'},
output => {
resolve(output)
}
)
).on('error', reject)
})
return Promise.all([
pSpawn.exitPromise
.then((exitCode) => {
if (opts.ignoreExit || exitCode === 0) {
if (!argv.json) {
console.log('>>>> exit:', exitCode)
}
return
} else {
throw Error('Exit ' + exitCode)
}
}),
pOutput]).then(arr => arr[1])
}