forked from nachaphon-phontree/starter-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.ts
More file actions
42 lines (37 loc) · 939 Bytes
/
exec.ts
File metadata and controls
42 lines (37 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { spawn } from "child_process";
export class ExecResult {
stdout = "";
exitCode = 0;
}
/**
* Executes a process
*/
export async function exec(
command: string,
args: string[] = [],
allowAllExitCodes: boolean = false
): Promise<ExecResult> {
process.stdout.write(`EXEC: ${command} ${args.join(" ")}\n`);
return new Promise((resolve, reject) => {
const execResult = new ExecResult();
const cp = spawn(command, args, {});
// STDOUT
cp.stdout.on("data", (data) => {
process.stdout.write(data);
execResult.stdout += data.toString();
});
// STDERR
cp.stderr.on("data", (data) => {
process.stderr.write(data);
});
// Close
cp.on("close", (code) => {
execResult.exitCode = code;
if (code === 0 || allowAllExitCodes) {
resolve(execResult);
} else {
reject(new Error(`Command exited with code ${code}`));
}
});
});
}