roll back issue with exec

merge-requests/4/head
Matthew 2020-05-12 10:21:30 -04:00
parent 8ce060282e
commit 88198b2234
No known key found for this signature in database
GPG Key ID: D499B75C1390E321
1 changed files with 10 additions and 19 deletions

View File

@ -32,26 +32,17 @@ export default class Util {
* @param options childProcess.ExecOptions
*/
public async exec(command: string, options: childProcess.ExecOptions = {}): Promise<string> {
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