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

66 lines
2.0 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
}
2022-04-05 02:58:57 +00:00
return await new Promise<string>((promise, throwError) => {
2022-02-01 02:31:20 +00:00
let output = '';
const child = exec(command, (error, stdout, stderr) => {
2022-04-05 02:58:57 +00:00
if (!suppressError && error) {
RemoteClientLogger.log(error.toString());
throwError(error);
2022-02-01 02:31:20 +00:00
}
if (stderr) {
2022-04-05 02:58:57 +00:00
RemoteClientLogger.log('stderr');
2022-02-01 02:31:20 +00:00
const diagnosticOutput = `${stderr.toString()}`;
if (!suppressLogs) {
RemoteClientLogger.logCliDiagnostic(diagnosticOutput);
}
2022-02-01 02:31:20 +00:00
output += diagnosticOutput;
}
2022-04-05 02:58:57 +00:00
RemoteClientLogger.log('stdout');
2022-02-01 02:31:20 +00:00
const outputChunk = `${stdout}`;
output += outputChunk;
});
2022-04-05 02:58:57 +00:00
child.on('message', (message) => {
RemoteClientLogger.log('message');
const outputChunk = `${message}`;
output += outputChunk;
});
child.on('error', (error) => {
RemoteClientLogger.log('error');
RemoteClientLogger.log(error.toString());
if (error) {
throwError(error);
}
});
child.on('disconnect', (error) => {
RemoteClientLogger.log('disconnect');
if (error) {
throwError(error);
}
});
child.on('close', (code) => {
RemoteClientLogger.log('close');
if (!suppressLogs) {
RemoteClientLogger.log(`[Exit code ${code}]`);
}
2022-02-01 02:31:20 +00:00
if (code !== 0 && !suppressError) {
2022-04-05 02:58:57 +00:00
throwError(output);
2022-02-01 02:31:20 +00:00
}
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);
});
});
}
}