From ab0d4c977fff2f3c5f8991f0923bcd77be69fd51 Mon Sep 17 00:00:00 2001 From: Noah Santschi-Cooney Date: Thu, 31 May 2018 23:09:39 +0100 Subject: [PATCH] Added an async process spawner using promises --- src/asyncSpawn.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/asyncSpawn.ts diff --git a/src/asyncSpawn.ts b/src/asyncSpawn.ts new file mode 100644 index 0000000..e683f54 --- /dev/null +++ b/src/asyncSpawn.ts @@ -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((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 +} \ No newline at end of file