index.js
1.38 KB
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
57
58
59
60
const spawn = require('child_process').spawn
const spawnPlease = (command, args, stdin, options) => {
// if there are only three arguments and the third argument is an object, treat it as the options object and set stdin to null
if (!options && typeof stdin === 'object') {
options = stdin
stdin = undefined
}
// defaults
options = options || {}
if (options.rejectOnError === undefined) {
options.rejectOnError = true
}
let stdout = ''
let stderr = ''
const child = spawn(command, args, options)
if (!spawnPlease.Promise) {
throw new Error('No built-in Promise. You will need to use a Promise library and spawnPlease.Promise = Promise.')
}
return new spawnPlease.Promise((resolve, reject) => {
if (stdin !== undefined) {
child.stdin.write(stdin)
}
child.stdin.end()
child.stdout.on('data', data => {
stdout += data
if (options.stdout) options.stdout(data)
})
child.stderr.on('data', data => {
stderr += data
if (options.stderr) options.stderr(data)
})
if (options.rejectOnError) {
child.addListener('error', reject)
}
child.on('close', code => {
if (code !== 0 && options.rejectOnError) {
reject(stderr)
}
else {
resolve(stdout)
}
})
})
}
spawnPlease.Promise = typeof Promise !== 'undefined' ? Promise : null
module.exports = spawnPlease