-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
40 lines (33 loc) · 853 Bytes
/
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
const { spawn } = require('child_process')
const BufferList = require('bl')
module.exports = (...args) => {
const child = spawn(...args)
const stdout = child.stdout ? new BufferList() : ''
const stderr = child.stderr ? new BufferList() : ''
if (child.stdout) {
child.stdout.on('data', data => {
stdout.append(data)
})
}
if (child.stderr) {
child.stderr.on('data', data => {
stderr.append(data)
})
}
const promise = new Promise((resolve, reject) => {
child.on('error', reject)
child.on('close', code => {
if (code === 0) {
resolve(stdout)
} else {
const err = new Error(`child exited with code ${code}`)
err.code = code
err.stderr = stderr
err.stdout = stdout
reject(err)
}
})
})
promise.child = child
return promise
}