2022-02-01 02:31:20 +00:00
|
|
|
import { exec } from 'child_process';
|
|
|
|
|
import { RemoteClientLogger } from './remote-client-logger';
|
|
|
|
|
|
|
|
|
|
export class CloudRunnerSystem {
|
2022-03-15 00:27:00 +00:00
|
|
|
public static async Run(command: string, suppressError = false, suppressLogs = false) {
|
2022-02-01 02:31:20 +00:00
|
|
|
for (const element of command.split(`\n`)) {
|
2022-03-15 00:27:00 +00:00
|
|
|
if (!suppressLogs) {
|
|
|
|
|
RemoteClientLogger.log(element);
|
|
|
|
|
}
|
2022-02-01 02:31:20 +00:00
|
|
|
}
|
|
|
|
|
return await new Promise<string>((promise) => {
|
|
|
|
|
let output = '';
|
|
|
|
|
const child = exec(command, (error, stdout, stderr) => {
|
|
|
|
|
if (error && !suppressError) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
if (stderr) {
|
|
|
|
|
const diagnosticOutput = `${stderr.toString()}`;
|
2022-03-15 00:27:00 +00:00
|
|
|
if (!suppressLogs) {
|
|
|
|
|
RemoteClientLogger.logCliDiagnostic(diagnosticOutput);
|
|
|
|
|
}
|
2022-02-01 02:31:20 +00:00
|
|
|
output += diagnosticOutput;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const outputChunk = `${stdout}`;
|
|
|
|
|
output += outputChunk;
|
|
|
|
|
});
|
|
|
|
|
child.on('close', function (code) {
|
2022-04-02 23:12:35 +00:00
|
|
|
if (!suppressLogs) {
|
|
|
|
|
RemoteClientLogger.log(`[Exit code ${code}]`);
|
|
|
|
|
}
|
2022-02-01 02:31:20 +00:00
|
|
|
if (code !== 0 && !suppressError) {
|
|
|
|
|
throw new Error(output);
|
|
|
|
|
}
|
|
|
|
|
const outputLines = output.split(`\n`);
|
|
|
|
|
for (const element of outputLines) {
|
2022-03-15 00:27:00 +00:00
|
|
|
if (!suppressLogs) {
|
|
|
|
|
RemoteClientLogger.log(element);
|
|
|
|
|
}
|
2022-02-01 02:31:20 +00:00
|
|
|
}
|
|
|
|
|
promise(output);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|