Skip to content

Commit 618f8a2

Browse files
authored
refactor: replace cross-spawn-promise with cross-spawn (#49)
1 parent 34a393b commit 618f8a2

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"dependencies": {
3838
"asar": "^2.0.1",
39-
"cross-spawn-promise": "^0.10.1",
39+
"cross-spawn": "^7.0.1",
4040
"debug": "^4.1.1",
4141
"fs-extra": "^8.0.1",
4242
"glob": "^7.1.4",

src/spawn.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
'use strict'
22

3-
const spawn = require('cross-spawn-promise')
3+
const spawn = require('cross-spawn')
44

55
/**
66
* Spawn a child process and make the error message more human friendly, if possible.
77
*
8+
* If logger is specified, it's usually a debug or console.log function pointer.
89
* Specify updateErrorCallback (a callback) to adjust the error object before it is rethrown.
910
*/
1011
module.exports = async function (cmd, args, logger, updateErrorCallback) {
1112
if (logger) logger(`Executing command ${cmd} ${args.join(' ')}`)
1213

13-
try {
14-
const stdout = await spawn(cmd, args)
15-
return stdout.toString()
16-
} catch (err) {
17-
const stderr = err.stderr ? err.stderr.toString() : ''
18-
if (updateErrorCallback) {
19-
updateErrorCallback(err, !!logger)
20-
}
21-
22-
throw new Error(`Error executing command (${err.message || err}):\n${cmd} ${args.join(' ')}\n${stderr}`)
23-
}
14+
return new Promise((resolve, reject) => {
15+
let stdout = ''
16+
let stderr = ''
17+
const process = spawn(cmd, args)
18+
process.stdout.on('data', data => {
19+
stdout += data.toString()
20+
})
21+
process.stderr.on('data', data => {
22+
/* istanbul ignore next */
23+
stderr += data.toString()
24+
})
25+
process.on('close', code => {
26+
if (code === 0) {
27+
resolve(stdout)
28+
} else {
29+
reject(new Error(`Command failed with a non-zero return code (${code}):\n${cmd} ${args.join(' ')}\n${stdout}\n${stderr}`))
30+
}
31+
})
32+
process.on('error', err => {
33+
if (updateErrorCallback) {
34+
updateErrorCallback(err, !!logger)
35+
}
36+
reject(new Error(`Error executing command (${err.message || err}):\n${cmd} ${args.join(' ')}\n${stderr}`))
37+
})
38+
})
2439
}

0 commit comments

Comments
 (0)