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

35 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-12-26 00:32:00 +00:00
import { exec } from 'child_process';
2021-12-25 20:05:17 +00:00
import CloudRunnerLogger from '../../cloud-runner/services/cloud-runner-logger';
2021-12-27 21:53:22 +00:00
export class CloudRunnerAgentSystem {
2021-12-25 20:05:17 +00:00
public static async Run(command: string) {
2021-12-28 00:24:58 +00:00
CloudRunnerLogger.logCli(`${command}`);
2021-12-26 00:32:00 +00:00
return await new Promise<string>((promise) => {
2021-12-26 01:05:51 +00:00
let output = '';
2021-12-26 01:48:36 +00:00
const child = exec(command, (error, stdout, stderr) => {
if (error) {
2021-12-26 19:24:10 +00:00
CloudRunnerLogger.logCli(`[ERROR] ${error.message}`);
2021-12-26 01:48:36 +00:00
throw new Error(error.toString());
}
if (stderr) {
2021-12-26 19:24:10 +00:00
CloudRunnerLogger.logCli(`[DIAGNOSTIC] ${stderr.toString()}`);
2021-12-26 03:07:37 +00:00
return;
2021-12-26 01:48:36 +00:00
}
2021-12-27 19:20:52 +00:00
const outputChunk = `${stdout}`;
2021-12-26 01:05:51 +00:00
output += outputChunk;
2021-12-26 01:08:40 +00:00
});
child.on('close', function (code) {
2021-12-26 20:42:55 +00:00
CloudRunnerLogger.logCli(`[Exit code ${code}]`);
2021-12-26 01:52:09 +00:00
if (code !== 0) {
2021-12-26 19:04:43 +00:00
throw new Error(output);
2021-12-26 01:52:09 +00:00
}
2021-12-27 19:35:12 +00:00
const outputLines = output.split(`\n`);
for (const element of outputLines) {
CloudRunnerLogger.logCli(element);
}
2021-12-26 01:08:40 +00:00
promise(output);
2021-12-26 00:32:00 +00:00
});
});
2021-12-25 20:05:17 +00:00
}
}