-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
79 lines (73 loc) · 2.55 KB
/
index.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
// Copyright 2020-2022 BadAimWeeb. All rights reserved. MIT license.
(async () => {
var semver = require("semver");
var nodeVersion = semver.parse(process.version);
if (nodeVersion.major < 12 || (nodeVersion.major == 12 && nodeVersion.minor < 9)) {
console.error("ERROR: Node.js 12+ (>=12.9) is required to run this! (current: " + process.version + ")");
process.exit(1);
}
var childProcess = require("child_process");
var http = require("http");
var fs = require("fs");
var path = require("path");
//Heroku: Run a dummy HTTP server. Why? https://i.imgur.com/KgsYleA.png
var herokuCompatible = http.createServer(function (req, res) {
res.writeHead(200, "OK", {
"Content-Type": "text/plain"
});
res.write(`This is just a dummy HTTP server to fool Heroku. https://i.imgur.com/KgsYleA.png \r\nC3CBot - https://github.com/c3cbot/legacy-c3cbot`);
res.end();
});
// eslint-disable-next-line no-process-env
herokuCompatible.listen(process.env.PORT || 0, "0.0.0.0");
function spawn(cmd, arg) {
return new Promise(resolve => {
var npmProcess = childProcess.spawn(cmd, arg, {
shell: true,
stdio: "inherit",
cwd: __dirname
});
npmProcess.on("close", function (code) {
resolve(code);
});
});
}
async function loader(message = "") {
if (message !== "") {
console.log();
console.log("[Loader] " + message);
}
if (fs.existsSync(path.join(__dirname, "c3c-nextbootupdate"))) {
await (spawn("npm", ["--production", "install"])
.then(() => spawn("npm", ["--depth", "9999", "update"]))
.then(() => {
fs.unlinkSync(path.join(__dirname, "c3c-nextbootupdate"));
})
.catch(() => { }));
}
var child = childProcess.spawn("node", ["--experimental-repl-await", "--trace-warnings", "main.js"], {
cwd: __dirname,
maxBuffer: 16384 * 1024,
stdio: "inherit",
shell: true
});
child.on("close", async (code) => {
if (code % 256 === 102) {
await loader("Restarting");
return;
}
if (code % 256 === 134) {
await loader("Known bug detected (error 134, 'Assertion `num == numcpus` failed.'). Restarting...");
return;
}
console.log();
console.log(`[Loader] Error code ${code}. Stopping...`);
process.exit();
});
child.on("error", function (err) {
console.log();
console.log("[Loader] Error:", err);
});
}
await loader();
})();