-
Notifications
You must be signed in to change notification settings - Fork 482
Expand file tree
/
Copy pathprocess.js
More file actions
244 lines (221 loc) · 5.98 KB
/
process.js
File metadata and controls
244 lines (221 loc) · 5.98 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import * as child_process from "node:child_process";
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { bsc_exe, rescript_exe } from "#cli/bins";
/**
* @typedef {{
* throwOnFail?: boolean,
* } & child_process.SpawnOptions} ExecOptions
*
* @typedef {{
* status: number,
* stdout: string,
* stderr: string,
* }} ExecResult
*/
const signals = {
SIGINT: 2,
SIGQUIT: 3,
SIGKILL: 9,
SIGTERM: 15,
};
export const {
shell,
node,
npm,
yarn,
mocha,
bsc,
execBin,
rescript,
execBuild,
execBuildOrThrow,
execClean,
} = setup();
/**
* @param {string} [cwd]
*/
export function setup(cwd = process.cwd()) {
/**
* @param {string} command
* @param {string[]} [args]
* @param {ExecOptions} [options]
* @return {Promise<ExecResult>}
*/
async function exec(command, args = [], options = {}) {
const { throwOnFail = options.stdio === "inherit" } = options;
const stdoutChunks = [];
const stderrChunks = [];
const subprocess = child_process.spawn(command, args, {
cwd,
shell: process.platform === "win32",
stdio: ["ignore", "pipe", "pipe"],
...options,
});
subprocess.stdout?.on("data", chunk => {
stdoutChunks.push(chunk);
});
subprocess.stderr?.on("data", chunk => {
stderrChunks.push(chunk);
});
return await new Promise((resolve, reject) => {
subprocess.once("error", err => {
reject(err);
});
subprocess.once("close", (exitCode, signal) => {
const stdout = Buffer.concat(stdoutChunks).toString("utf8");
const stderr = Buffer.concat(stderrChunks).toString("utf8");
let code = exitCode ?? 1;
if (signals[signal]) {
// + 128 is standard POSIX practice, see also https://nodejs.org/api/process.html#exit-codes
code = signals[signal] + 128;
}
if (throwOnFail && code !== 0) {
reject(
new Error(
`Command ${command} exited with non-zero status: ${code}`,
),
);
} else {
resolve({ status: code, stdout, stderr });
}
});
});
}
return {
/**
* bash shell script
*
* @param {string} script
* @param {string[]} [args]
* @param {ExecOptions} [options]
* @return {Promise<ExecResult>}
*/
shell(script, args = [], options = {}) {
return exec("bash", [script, ...args], options);
},
/**
* Execute JavaScript on Node.js
*
* @param {string} script
* @param {string[]} [args]
* @param {ExecOptions} [options]
* @return {Promise<ExecResult>}
*/
node(script, args = [], options = {}) {
return exec("node", [script, ...args], options);
},
/**
* Execute npm command
*
* @param {string} command
* @param {string[]} [args]
* @param {ExecOptions} [options]
* @return {Promise<ExecResult>}
*/
npm(command, args = [], options = {}) {
return exec("npm", [...command.split(" "), ...args], options);
},
/**
* Execute Yarn command
*
* @param {string} command
* @param {string[]} [args]
* @param {ExecOptions} [options]
* @return {Promise<ExecResult>}
*/
yarn(command, args = [], options = {}) {
return exec("yarn", [...command.split(" "), ...args], options);
},
/**
* Execute Mocha CLI
*
* @param {string[]} [args]
* @param {ExecOptions} [options]
* @return {Promise<ExecResult>}
*/
mocha(args = [], options = {}) {
// `yarn mocha` works, but format output differently
// No more efforts here since we're plannig to drop Mocha
return exec("npx", ["mocha", ...args], options);
},
/**
* `bsc` CLI
*
* @param {string[]} [args]
* @param {ExecOptions} [options]
* @return {Promise<ExecResult>}
*/
bsc(args = [], options = {}) {
return exec(bsc_exe, args, options);
},
/**
* `rescript` CLI
*
* @param {(
* | "build"
* | "clean"
* | "format"
* | (string & {})
* )} command
* @param {string[]} [args]
* @param {ExecOptions} [options]
* @return {Promise<ExecResult>}
*/
rescript(command, args = [], options = {}) {
const cliPath = path.join(import.meta.dirname, "../cli/rescript.js");
return exec("node", [cliPath, command, ...args].filter(Boolean), options);
},
/**
* Execute ReScript `build` command directly
*
* @param {string[]} [args]
* @param {ExecOptions} [options]
* @return {Promise<ExecResult>}
*/
execBuild(args = [], options = {}) {
return exec(rescript_exe, ["build", ...args], options);
},
/**
* Execute ReScript `build` command directly and throw on non-zero exit
* while preserving captured stdout/stderr for quiet successful tests.
*
* @param {string[]} [args]
* @param {ExecOptions} [options]
* @return {Promise<ExecResult>}
*/
async execBuildOrThrow(args = [], options = {}) {
const out = await exec(rescript_exe, ["build", ...args], options);
if (out.status !== 0) {
const err = new Error("ReScript build failed");
err.stack = out.stdout + out.stderr;
Object.assign(err, { execResult: out });
throw err;
}
return out;
},
/**
* Execute ReScript `clean` command directly
*
* @param {string[]} [args]
* @param {ExecOptions} [options]
* @return {Promise<ExecResult>}
*/
execClean(args = [], options = {}) {
return exec(rescript_exe, ["clean", ...args], options);
},
/**
* Execute any binary or wrapper.
* It should support Windows as well
*
* @param {string} bin
* @param {string[]} [args]
* @param {ExecOptions} [options]
* @return {Promise<ExecResult>}
*/
async execBin(bin, args = [], options = {}) {
const realPath = await fs.realpath(bin);
return exec(realPath, args, options);
},
};
}