-
Notifications
You must be signed in to change notification settings - Fork 0
/
prod.ts
111 lines (97 loc) · 3.5 KB
/
prod.ts
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
// NOTE: THIS FILE IS NOT AFFECTED BY COMMIT AUTO RUN THINGY, ONLY THE ACTUAL INDEX.TS STUFF IS
import { spawn, ChildProcess, exec } from "child_process";
import { simpleGit, SimpleGit } from "simple-git";
const git: SimpleGit = simpleGit("./");
let childProcess: ChildProcess | null = null;
function startScript() {
// Ensure only one instance of the script is running
if (childProcess) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
childProcess.on("exit", (code, signal) => {});
childProcess.kill();
childProcess = null;
return;
}
childProcess = spawn("node", ["dist/src/index.js", "-ssl"], {
stdio: "inherit",
});
// Listen for unexpected exit (crash)
childProcess.on("exit", (code, signal) => {
if (signal != "SIGINT") {
console.log(
`Child process exited with code ${code} and signal ${signal}, restarting`,
);
// Restart the script if it crashes
childProcess = null;
startScript();
}
});
}
async function runInstallCommand() {
return new Promise<void>((resolve, reject) => {
console.log("Running npm install...");
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const installProcess = exec("npm install", (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return reject(error);
}
//console.log(`stdout: ${stdout}`);
console.log("Install complete");
//console.error(`stderr: ${stderr}`);
resolve();
});
});
}
async function runBuildCommand() {
return new Promise<void>((resolve, reject) => {
console.log("Running npm run build...");
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const buildProcess = exec("npm run build", (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return reject(error);
}
//console.log(`stdout: ${stdout}`);
console.log("Build complete");
//console.error(`stderr: ${stderr}`);
resolve();
});
});
}
async function repoUpdateLoop() {
while (true) {
try {
const pullSummary = await git.pull("origin", "main");
if (pullSummary.files.length > 0) {
console.log("Changes detected, running build and restarting script",);
await runInstallCommand();
await runBuildCommand();
await new Promise<void>((resolve) => {
if (childProcess) {
// ig I have to check again
childProcess.on("exit", () => {
console.log("Child process exited, continuing...");
resolve();
});
childProcess.kill("SIGINT");
childProcess = null;
}
});
await new Promise((resolve) => setTimeout(resolve, 200));
startScript();
}
}
catch (e) {
console.error("Error in repoUpdateLoop", e);
}
await new Promise((resolve) => setTimeout(resolve, 30000));
}
}
async function main() {
await runInstallCommand();
await runBuildCommand();
startScript();
await repoUpdateLoop();
}
main();