Added an async process spawner using promises

This commit is contained in:
Noah Santschi-Cooney 2018-05-31 23:09:39 +01:00
parent 558de42068
commit ab0d4c977f
No known key found for this signature in database
GPG key ID: 3B22282472C8AE48

26
src/asyncSpawn.ts Normal file
View file

@ -0,0 +1,26 @@
import { spawn } from 'child_process'
export function runLinter(...args: any[]) {
const child = spawn(args[0], ...(args.slice(1)))
let stderr = ''
child.stderr.on('data', data => {
stderr += data
})
const promise = new Promise<string>((resolve, reject) => {
child.on('error', () => reject(new Error('fatal error ${stderr}')))
child.on('exit', (code, signal) => {
switch (code) {
case 0:
case 2:
resolve(stderr)
default:
reject(new Error('standard error ${signal} ${stderr}'))
}
})
})
return promise
}