From fd78b2ef3587fc84807ad2ff0c6eab0e1ebc5654 Mon Sep 17 00:00:00 2001 From: Bsian Date: Tue, 12 May 2020 15:54:07 +0100 Subject: [PATCH] Back --- src/class/Util.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/class/Util.ts b/src/class/Util.ts index b2338db..851337d 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -32,15 +32,25 @@ export default class Util { * @param options childProcess.ExecOptions */ public async exec(command: string, options: childProcess.ExecOptions = {}): Promise { - const ex = promisify(childProcess.exec); - let result: string; - try { - const res = await ex(command, options); - result = `${res.stdout}${res.stderr}`; - } catch (err) { - return Promise.reject(new Error(`Command failed: ${err.cmd}\n${err.stderr}${err.stdout}`)); - } - return result; + return new Promise((res, rej) => { + let error = false; + let output = ''; + const writeFunction = (data: string|Buffer|Error) => { + if (data instanceof Error) error = true; + output += `${data}`; + }; + const cmd = childProcess.exec(command, options); + cmd.stdout.on('data', writeFunction); + cmd.stderr.on('data', writeFunction); + cmd.on('error', writeFunction); + cmd.once('close', (code, signal) => { + cmd.stdout.removeListener('data', writeFunction); + cmd.stderr.removeListener('data', writeFunction); + cmd.removeListener('error', writeFunction); + if (error) rej(output); + res(output); + }); + }); }