From 88198b223469c7e65bdb13916e2d810c950c1fea Mon Sep 17 00:00:00 2001 From: Matthew Ray Date: Tue, 12 May 2020 10:21:30 -0400 Subject: [PATCH] roll back issue with exec --- src/class/Util.ts | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/src/class/Util.ts b/src/class/Util.ts index d3880d8..b2338db 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -32,27 +32,18 @@ export default class Util { * @param options childProcess.ExecOptions */ public async exec(command: string, options: childProcess.ExecOptions = {}): Promise { - 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); - }); - }); + 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; } + /** * Resolves a command * @param query Command input