optional args to Util.exec

merge-requests/2/merge
Matthew 2020-01-01 15:55:43 -05:00
parent a9cd35b21a
commit ae971f9f32
No known key found for this signature in database
GPG Key ID: 766BE43AE75F7559
1 changed files with 16 additions and 2 deletions

View File

@ -24,12 +24,26 @@ export default class Util {
}); });
} }
public async exec(command: string): Promise<string> { /**
* Executes a terminal command async.
* @param command The command to execute
* @param options childProcess.ExecOptions, the env option is automatically set if not provided.
*/
public async exec(command: string, options?: childProcess.ExecOptions): Promise<string> {
const ex = promisify(childProcess.exec); const ex = promisify(childProcess.exec);
let result: string; let result: string;
// eslint-disable-next-line no-useless-catch // eslint-disable-next-line no-useless-catch
let args: childProcess.ExecOptions;
try { try {
const res = await ex(command, { env: { HOME: '/root' } }); if (options) {
args = options;
if (!args.env) {
args.env = { HOME: '/root' };
}
} else {
args.env = { HOME: '/root' };
}
const res = await ex(command, args);
result = res.stderr || res.stdout; result = res.stderr || res.stdout;
} catch (err) { } catch (err) {
return Promise.reject(new Error(`Command failed: ${err.cmd}\n${err.stderr || err.stdout}`)); return Promise.reject(new Error(`Command failed: ${err.cmd}\n${err.stderr || err.stdout}`));