-
Notifications
You must be signed in to change notification settings - Fork 14
/
utils.js
59 lines (46 loc) · 1.42 KB
/
utils.js
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
const child_process = require("node:child_process");
const http = require("node:http");
const finalhandler = require("finalhandler");
const serveStatic = require("serve-static");
const treeKill = require("tree-kill");
exports.exec = (command, env, cwd) => {
process.env.NODE_NO_WARNINGS = "1"; // disable deprecation warnings
const proc = child_process.exec(command, {
env: { ...process.env, ...env },
cwd,
});
proc.on("error", (error) => {
console.log(`error: ${error}`);
});
// proc.stdout.setEncoding("utf8");
// proc.stdout.on("data", (data) => {
// console.log(`stdout: [${command}] ${data}`);
// });
proc.stderr.setEncoding("utf8");
proc.stderr.on("data", (data) => {
console.log(`stderr: [${command}] ${data}`);
});
return async () => {
await new Promise((resolve, reject) => {
treeKill(proc.pid, "SIGKILL", (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
};
};
exports.execSync = (command, env, cwd) => {
child_process.execSync(command, { env: { ...process.env, ...env }, cwd });
};
exports.serveStaticFiles = (path, port) => {
const serve = serveStatic(path, { index: ["index.html", "index.htm"] });
const server = http.createServer(function onRequest(req, res) {
serve(req, res, finalhandler(req, res));
});
server.on("error", () => {});
server.listen(port);
return () => server.close();
};