-
Notifications
You must be signed in to change notification settings - Fork 0
/
base
106 lines (89 loc) · 2.72 KB
/
base
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
#!/usr/bin/env node
if (
!process.argv.includes("pre-commit") &&
!process.argv.includes("post-commit")
) {
console.log(`
▄▄▄ ▄· ▄▌ ▄▄▄· ▄▄▌
▀▄ █·▪ ▐█▪██▌▐█ ▀█ ██•
▐▀▀▄ ▄█▀▄ ▐█▌▐█▪▄█▀▀█ ██▪
▐█•█▌▐█▌.▐▌ ▐█▀·.▐█ ▪▐▌▐█▌▐▌
.▀ ▀ ▀█▄▀▪ ▀ • ▀ ▀ .▀▀▀
`);
}
const { spawn, fork } = require("child_process");
const { existsSync } = require("fs");
const { join, delimiter, dirname } = require("path");
const basePath = join(process.cwd(), ..."pkgs/base/main.js".split("/"));
const args = process.argv.slice(2);
let baseReady = false;
const main = async () => {
if (!existsSync(join(process.cwd(), "node_modules"))) {
await new Promise((resolve) => {
const pnpm = spawn("pnpm", ["i"], { stdio: "inherit", shell: true });
pnpm.on("exit", resolve);
});
}
function exitHandler(options, exitCode) {
global.exiting = true;
if (!!global.base) global.base.kill(2);
if (!!global.build) global.build.kill(2);
}
//do something when app is closing
process.on("exit", exitHandler.bind(null, { cleanup: true }));
//catches ctrl+c event
process.on("SIGINT", exitHandler.bind(null, { exit: true }));
// catches "kill pid" (for example: nodemon restart)
process.on("SIGUSR1", exitHandler.bind(null, { exit: true }));
process.on("SIGUSR2", exitHandler.bind(null, { exit: true }));
//catches uncaught exceptions
process.on("uncaughtException", exitHandler.bind(null, { exit: true }));
if (args.includes("devbase") || !existsSync(basePath)) {
const buildBase = () => {
return new Promise((resolve) => {
let resolved = false;
global.build = fork(
join(process.cwd(), ..."pkgs/base/src/builder/base.mjs".split("/")),
args,
{
cwd: process.cwd(),
}
);
global.build.on("message", (m) => {
if (!resolved) {
resolved = true;
resolve();
} else if (baseReady) {
global.base.kill(2);
}
});
});
};
await buildBase();
}
runBase();
};
const runBase = () => {
global.base = fork(basePath, args, {
env: {
...process.env,
NODE_PATH: join(dirname(basePath), "node_modules"),
},
cwd: process.cwd(),
});
global.base.on("message", (e) => {
if (e === "base-ready") baseReady = true;
});
global.base.on("exit", (code) => {
if (global.exiting) return;
if (code === 1 || code === 2) {
if (!!global.build) {
global.build.kill();
}
process.exit();
} else {
runBase();
}
});
};
main();