socket-function 0.149.0 → 0.150.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/runPromise.ts +68 -0
package/package.json
CHANGED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import child_process from "child_process";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { blue, red } from "./formatting/logColors";
|
|
4
|
+
|
|
5
|
+
export const runAsync = runPromise;
|
|
6
|
+
export async function runPromise(command: string, config?: {
|
|
7
|
+
cwd?: string;
|
|
8
|
+
quiet?: boolean;
|
|
9
|
+
// Never throw, just return the full output
|
|
10
|
+
nothrow?: boolean;
|
|
11
|
+
detach?: boolean;
|
|
12
|
+
}) {
|
|
13
|
+
return new Promise<string>((resolve, reject) => {
|
|
14
|
+
console.log(">" + blue(command));
|
|
15
|
+
const childProc = child_process.spawn(command, {
|
|
16
|
+
shell: true,
|
|
17
|
+
cwd: config?.cwd,
|
|
18
|
+
stdio: ["inherit", "pipe", "pipe"],
|
|
19
|
+
detached: config?.detach,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
let fullOutput = "";
|
|
23
|
+
let stderr = "";
|
|
24
|
+
|
|
25
|
+
// Always collect output
|
|
26
|
+
childProc.stdout?.on("data", (data) => {
|
|
27
|
+
const chunk = data.toString();
|
|
28
|
+
fullOutput += chunk;
|
|
29
|
+
|
|
30
|
+
// Stream to console unless quiet mode
|
|
31
|
+
if (!config?.quiet) {
|
|
32
|
+
process.stdout.write(chunk);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
childProc.stderr?.on("data", (data) => {
|
|
37
|
+
const chunk = data.toString();
|
|
38
|
+
stderr += chunk;
|
|
39
|
+
fullOutput += chunk;
|
|
40
|
+
|
|
41
|
+
// Stream to console unless quiet mode
|
|
42
|
+
if (!config?.quiet) {
|
|
43
|
+
process.stderr.write(red(chunk));
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
childProc.on("error", (err) => {
|
|
48
|
+
if (config?.nothrow) {
|
|
49
|
+
resolve(fullOutput);
|
|
50
|
+
} else {
|
|
51
|
+
reject(err);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
childProc.on("close", (code) => {
|
|
56
|
+
if (code === 0 || config?.nothrow) {
|
|
57
|
+
resolve(fullOutput);
|
|
58
|
+
} else {
|
|
59
|
+
let errorMessage = `Process exited with code ${code} for command: ${command}`;
|
|
60
|
+
if (stderr) {
|
|
61
|
+
errorMessage += `\n${stderr}`;
|
|
62
|
+
}
|
|
63
|
+
const error = new Error(errorMessage);
|
|
64
|
+
reject(error);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
}
|