-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecute.ts
57 lines (49 loc) · 1.34 KB
/
execute.ts
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Copyright (C) 2023 DevMatch Co. - All Rights Reserved
**/
const { spawn } = require("child_process");
export function execute(command, printOutput = false, cwd = "") {
console.log("\u001b[33mExecuting: " + command + "\u001b[0m");
return new Promise((resolve, reject) => {
if (!cwd) {
cwd = process.cwd();
}
const [cmd, ...args] = command.split(" ");
const child = spawn(cmd, args, {
cwd: cwd,
shell: true,
});
let stdout = "";
let stderr = "";
child.stdout.on("data", (data) => {
stdout += data.toString();
if (printOutput) {
let lines = data.toString().trim().split("\n");
for (let line of lines) {
console.log("\u001b[33m > " + line + "\u001b[0m");
}
}
});
child.stderr.on("data", (data) => {
stderr += data.toString();
if (printOutput) {
let lines = data.toString().trim().split("\n");
for (let line of lines) {
console.log("\u001b[31m > " + line + "\u001b[0m");
}
}
});
child.on("close", (code) => {
if (code !== 0) {
console.log(`spawn error: ${stderr}`);
reject(new Error(stderr));
return;
}
resolve(stdout);
});
child.on("error", (error) => {
console.log(`spawn error: ${error}`);
reject(error);
});
});
}