github-desktop/script/changelog/spawn.ts
Brendan Forster a3f854a2af lint
2018-01-31 10:52:20 +11:00

32 lines
692 B
TypeScript

import * as ChildProcess from 'child_process'
export function spawn(
cmd: string,
args: ReadonlyArray<string>
): Promise<string> {
return new Promise((resolve, reject) => {
const child = ChildProcess.spawn(cmd, args as string[], { shell: true })
let receivedData = ''
child.on('error', reject)
child.stdout.on('data', data => {
receivedData += data
})
child.on('close', (code, signal) => {
if (code === 0) {
resolve(receivedData)
} else {
reject(
new Error(
`'${cmd} ${args.join(
' '
)}' exited with code ${code}, signal ${signal}`
)
)
}
})
})
}