-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
95 lines (91 loc) · 3.06 KB
/
build.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
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
const child_process = require("child_process");
const fs = require("fs").promises;
const buildCommand = "npx tsc";
(async () => {
const tsconfig = await fs.readFile("tsconfig.json", "utf-8").then(json => JSON.parse(json.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) => g ? "" : m)));
const buildPath = tsconfig.compilerOptions.outDir;
if (!buildPath)
throw new Error("no build path");
try {
if (!await fs.stat("node_modules"))
throw new Error();
console.log(buildCommand);
await exec(buildCommand, {});
const _package = JSON.parse(await fs.readFile("package.json", "utf-8"));
delete _package.devDependencies;
await fs.writeFile(buildPath + "/package.json", JSON.stringify(_package));
} catch (_) {
console.log("npm i");
await exec("npm i");
console.log(buildCommand);
await exec(buildCommand);
const package = JSON.parse(await fs.readFile("package.json", "utf-8"));
delete package.devDependencies;
await fs.writeFile(buildPath + "/package.json", JSON.stringify(package));
}
})().catch(e => {
throw e;
});
/**
* @param {string} command
* @param {{[src: string]: string} | boolean} pipe
* @param {{[src: string]: string} | boolean} backpipe
* @returns {Promise<void>}
*/
function exec(
command,
pipe = {
stdout: "stdout",
stderr: "stderr"
},
backpipe = {
stdin: "stdin"
}
) {
if (typeof pipe !== "object" && pipe)
pipe = {
stdout: "stdout",
stderr: "stderr"
};
if (typeof backpipe !== "object" && backpipe)
backpipe = {
stdout: "stdout",
stderr: "stderr"
};
return new Promise((res, rej) => {
const cp = child_process.exec(command, (err) => {
if (err)
rej(err);
else
res();
}).on("spawn", () => {
for (const name of Object.keys(pipe)) {
let pn = pipe[name];
if (typeof pn !== "string")
pn = name;
cp[name].pipe(process[pn]);
}
for (const name of Object.keys(backpipe)) {
let pn = backpipe[name];
if (typeof pn !== "string")
pn = name;
process[pn].pipe(cp[name]);
}
}).on("close", () => {
try {
for (const name of Object.keys(pipe)) {
let pn = pipe[name];
if (typeof pn !== "string")
pn = name;
cp[name].unpipe(process[pn]);
}
for (const name of Object.keys(backpipe)) {
let pn = backpipe[name];
if (typeof pn !== "string")
pn = name;
process[pn].unpipe(cp[name]);
}
} catch (_) { }
});
});
}