Merge branch 'master' of gitlab.libraryofcode.org:engineering/cloudservices

merge-requests/4/head
Matthew 2020-05-12 11:06:08 -04:00
commit 0420f5dd87
No known key found for this signature in database
GPG Key ID: D499B75C1390E321
1 changed files with 19 additions and 9 deletions

View File

@ -32,15 +32,25 @@ export default class Util {
* @param options childProcess.ExecOptions * @param options childProcess.ExecOptions
*/ */
public async exec(command: string, options: childProcess.ExecOptions = {}): Promise<string> { public async exec(command: string, options: childProcess.ExecOptions = {}): Promise<string> {
const ex = promisify(childProcess.exec); return new Promise((res, rej) => {
let result: string; let error = false;
try { let output = '';
const res = await ex(command, options); const writeFunction = (data: string|Buffer|Error) => {
result = `${res.stdout}${res.stderr}`; if (data instanceof Error) error = true;
} catch (err) { output += `${data}`;
return Promise.reject(new Error(`Command failed: ${err.cmd}\n${err.stderr}${err.stdout}`)); };
} const cmd = childProcess.exec(command, options);
return result; 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);
});
});
} }