Created
September 6, 2018 06:53
-
-
Save Alexgalinier/4dd2bb1283f6df36c3b01ef5ba4d827c to your computer and use it in GitHub Desktop.
spawn with promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { spawn } = require('child_process'); | |
const spawnP = async command => { | |
return new Promise((res, rej) => { | |
const cmdParts = command.split(' '); | |
const cmdSpawm = spawn( | |
cmdParts[0], | |
cmdParts.length > 1 ? cmdParts.slice(1) : [], | |
{ | |
stdio: 'inherit' | |
} | |
); | |
cmdSpawm.on('error', data => { | |
console.error(`Error: ${data}`); | |
rej(); | |
}); | |
cmdSpawm.on('close', code => { | |
if (code === 0) { | |
res(); | |
} else { | |
rej(); | |
} | |
}); | |
}); | |
}; | |
module.exports = spawnP; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment