-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.js
More file actions
124 lines (104 loc) 路 3.36 KB
/
Copy pathindex.js
File metadata and controls
124 lines (104 loc) 路 3.36 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
require("dotenv").config(); // Load environment variables FIRST
const { spawn, execSync } = require("child_process");
const path = require("path");
const fs = require("fs");
const SCRIPT_FILE = "kokoro.js";
const SCRIPT_PATH = path.join(__dirname, SCRIPT_FILE);
let npmPackages = [
{ name: "canvas", version: "latest" },
{ name: "kleur", version: "latest" }
];
if (isTypeScriptSupported()) {
npmPackages.push(
{ name: "typescript", version: "latest" },
{ name: "ts-node", version: "latest" }
);
}
const restartEnabled = process.env.PID !== "0";
let mainProcess;
function getOutdatedPackages() {
try {
const outdatedData = JSON.parse(execSync("npm outdated -g --json", { encoding: "utf8" }));
return npmPackages.filter(pkg => outdatedData[pkg.name]);
} catch (error) {
return npmPackages;
}
}
function installPackages(callback) {
console.log("Checking npm packages...");
const outdatedPackages = getOutdatedPackages();
if (outdatedPackages.length === 0) return callback();
console.log(`Installing/Updating packages:`);
outdatedPackages.forEach(pkg => {
const version = pkg.version ? `@${pkg.version}` : '';
console.log(`- Installing ${pkg.name}${version}`);
const installProcess = spawn("npm", ["install", "-g", `${pkg.name}${version}`], { stdio: "inherit", shell: true });
installProcess.on("close", (code) => {
if (code !== 0) {
console.error(`Failed to install/update ${pkg.name}. Skipping...`);
}
});
});
callback();
}
function setupTypeScript() {
try {
if (!fs.existsSync("tsconfig.json")) {
console.log("Setting up TypeScript...");
execSync("tsc --init", { stdio: "inherit" });
} else {
console.log("TypeScript already set up.");
}
} catch (error) {
console.error("TypeScript setup failed (likely due to environment limitations). Skipping TypeScript setup...");
}
}
function isTypeScriptSupported() {
try {
execSync("tsc --version", { stdio: "ignore" });
return true;
} catch (error) {
return false;
}
}
function start() {
const port = process.env.PORT;
if (port) {
console.log(`Starting main process on PORT=${port}`);
} else {
console.log("No PORT set, starting main process without a specific port.");
}
mainProcess = spawn("node", ["--no-warnings", SCRIPT_PATH], {
cwd: __dirname,
stdio: "inherit",
shell: true,
env: { ...process.env, PORT: port },
});
mainProcess.on("error", (error) => {
console.error("Failed to start main process:", error);
process.exit(1);
});
mainProcess.on("close", (exitCode) => {
console.log(`Main process exited with code [${exitCode}]`);
if (restartEnabled) {
console.log("Restarting in 5 seconds...");
setTimeout(restartProcess, 5000);
} else {
console.log("Shutdown complete.");
process.exit(exitCode);
}
});
}
function restartProcess() {
if (mainProcess && mainProcess.pid) {
mainProcess.kill("SIGKILL");
console.log("Main process killed. Restarting...");
}
start();
}
installPackages(() => {
if (isTypeScriptSupported()) {
setupTypeScript();
}
start();
});