| 9 | * Executes a process |
| 10 | */ |
| 11 | export async function exec( |
| 12 | command: string, |
| 13 | args: string[] = [], |
| 14 | allowAllExitCodes: boolean = false |
| 15 | ): Promise<ExecResult> { |
| 16 | process.stdout.write(`EXEC: ${command} ${args.join(" ")}\n`); |
| 17 | return new Promise((resolve, reject) => { |
| 18 | const execResult = new ExecResult(); |
| 19 | const cp = spawn(command, args, {}); |
| 20 | |
| 21 | // STDOUT |
| 22 | cp.stdout.on("data", (data) => { |
| 23 | process.stdout.write(data); |
| 24 | execResult.stdout += data.toString(); |
| 25 | }); |
| 26 | |
| 27 | // STDERR |
| 28 | cp.stderr.on("data", (data) => { |
| 29 | process.stderr.write(data); |
| 30 | }); |
| 31 | |
| 32 | // Close |
| 33 | cp.on("close", (code) => { |
| 34 | execResult.exitCode = code; |
| 35 | if (code === 0 || allowAllExitCodes) { |
| 36 | resolve(execResult); |
| 37 | } else { |
| 38 | reject(new Error(`Command exited with code ${code}`)); |
| 39 | } |
| 40 | }); |
| 41 | }); |
| 42 | } |