-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathprocessHelpers.js
53 lines (50 loc) · 1.2 KB
/
processHelpers.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
47
48
49
50
51
52
53
'use strict'
const spawn = require('child_process').spawn
const execFile = require('child_process').execFile
const processHelpers = {
run: function (command, options) {
if (!options) options = {}
return new Promise(function (resolve, reject) {
const env = Object.assign({}, process.env)
if (env.Path && env.PATH) {
if (env.Path !== env.PATH) {
env.PATH = env.Path + ';' + env.PATH
}
delete env.Path
}
const child = spawn(command[0], command.slice(1), {
stdio: options.silent ? 'ignore' : 'inherit',
env,
})
let ended = false
child.on('error', function (e) {
if (!ended) {
reject(e)
ended = true
}
})
child.on('exit', function (code, signal) {
if (!ended) {
if (code === 0) {
resolve()
} else {
reject(new Error('Process terminated: ' + code || signal))
}
ended = true
}
})
})
},
execFile: function (command) {
return new Promise(function (resolve, reject) {
execFile(command[0], command.slice(1), function (err, stdout, stderr) {
if (err) {
reject(new Error(err.message + '\n' + (stdout || stderr)))
} else {
resolve(stdout)
}
})
})
},
}
module.exports = processHelpers