unity-builder/src/model/cli/remote-client/remote-client-services/cloud-runner-system.ts

46 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-02-01 02:31:20 +00:00
import { exec } from 'child_process';
import { RemoteClientLogger } from './remote-client-logger';
export class CloudRunnerSystem {
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`)) {
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()}`;
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) {
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) {
if (!suppressLogs) {
RemoteClientLogger.log(element);
}
2022-02-01 02:31:20 +00:00
}
promise(output);
});
});
}
}