q13x-eaglerproxy/replit_runtime/index.js

148 lines
4.2 KiB
JavaScript
Raw Permalink Normal View History

2023-06-20 18:26:19 -07:00
// libraries
2023-07-04 19:01:02 -07:00
const fs = require("fs/promises");
const path = require("path");
const crypto = require("crypto");
const { sourceDir } = require("./config.js");
2023-06-20 18:26:19 -07:00
class Logger {
2023-07-04 19:01:02 -07:00
constructor({ name, logDebug }) {
this.name = name;
this.debug = logDebug;
}
2023-06-20 18:26:19 -07:00
2023-07-04 19:01:02 -07:00
_log(logType, data, method) {
console[method](
`[${this.name}] [${logType}] ${
typeof data == "string" ? data : data.toString()
}`
);
}
2023-06-20 18:26:19 -07:00
2023-07-04 19:01:02 -07:00
info(data) {
this._log("info", data, "log");
}
2023-06-20 18:26:19 -07:00
2023-07-04 19:01:02 -07:00
warn(data) {
this._log("warn", data, "error");
}
2023-06-20 18:26:19 -07:00
2023-07-04 19:01:02 -07:00
error(data) {
this._log("error", data, "error");
}
2023-06-20 18:26:19 -07:00
2023-07-04 19:01:02 -07:00
debug(data) {
if (this.debug) {
this._log("debug", data, "error");
2023-06-20 18:26:19 -07:00
}
2023-07-04 19:01:02 -07:00
}
2023-06-20 18:26:19 -07:00
}
async function recursiveFileSearch(dir) {
2023-07-04 19:01:02 -07:00
const fileList = [];
for (const file of await fs.readdir(dir, { withFileTypes: true })) {
let pathDir = path.resolve(dir, file.name);
if (file.isFile()) {
fileList.push(pathDir);
} else if (file.isDirectory()) {
fileList.push(...(await recursiveFileSearch(pathDir)));
} else {
logger.warn(
`Found directory entry that is neither a file or directory (${pathDir}), ignoring!`
);
2023-06-20 18:26:19 -07:00
}
2023-07-04 19:01:02 -07:00
}
return fileList;
2023-06-20 18:26:19 -07:00
}
const logger = new Logger({
2023-07-04 19:01:02 -07:00
name: "launcher",
logDebug: process.env.DEBUG == "true",
}),
LINE_SEPERATOR = "-----------------------------------";
2023-06-20 18:26:19 -07:00
if (!process.env.REPL_SLUG) {
2023-07-04 19:01:02 -07:00
logger.error(LINE_SEPERATOR);
logger.error("Repl not detected!");
logger.error("");
logger.error("This file is meant to be ran in a Repl");
logger.error(LINE_SEPERATOR);
2023-06-20 18:26:19 -07:00
}
2023-07-04 19:01:02 -07:00
logger.info(LINE_SEPERATOR);
logger.info("Checking if the proxy needs to be recompiled...");
logger.info(LINE_SEPERATOR);
2023-06-20 18:26:19 -07:00
fs.readFile(path.join(__dirname, ".sourcehash"))
2023-07-04 19:01:02 -07:00
.then((data) => {
let oldHash = data.toString();
logger.info("Found old hash, calculating hash of source files...");
recursiveFileSearch(sourceDir)
.then((files) => {
Promise.all(files.map((f) => fs.readFile(f))).then((data) => {
const hash = crypto.createHash("sha256");
data.forEach((d) => hash.update(d));
let sourceHash = hash.digest().toString();
2023-06-20 18:26:19 -07:00
2023-07-04 19:01:02 -07:00
if (sourceHash === oldHash) {
logger.info("Source hasn't been changed, skipping compilation...");
process.exit(0);
} else {
logger.info("Source has been changed, recompiling...");
fs.writeFile(path.join(__dirname, ".sourcehash"), sourceHash)
.then(() => {
process.exit(2);
})
.catch((err) => {
logger.error(`Could not write new hash to disk!\n${err.stack}`);
process.exit(1);
});
}
});
})
.catch((err) => {
logger.error(
`Could not calculate file hashes for files in directory ${sourceDir}!\n${err.stack}`
);
process.exit(1);
});
})
.catch((err) => {
if (err.code == "ENOENT") {
logger.warn(
"Previous source hash not found! Assuming a clean install is being used."
);
logger.info("Calculating hash...");
recursiveFileSearch(sourceDir)
.then((files) => {
Promise.all(files.map((f) => fs.readFile(f))).then((data) => {
const hash = crypto.createHash("sha256");
data.forEach((d) => hash.update(d));
let sourceHash = hash.digest().toString();
fs.writeFile(path.join(__dirname, ".sourcehash"), sourceHash)
.then(() => {
logger.info("Saved hash to disk.");
process.exit(2);
})
.catch((err) => {
logger.error(`Could not write new hash to disk!\n${err.stack}`);
process.exit(1);
});
});
})
.catch((err) => {
logger.error(
`Could not calculate file hashes for files in directory ${sourceDir}!\n${err.stack}`
);
process.exit(1);
});
} else {
logger.error(
`Could not read .sourcehash file in ${path.join(
__dirname,
".sourcehash"
)} due to an unknown error! Try again with a clean repl?\n${err.stack}`
);
process.exit(1);
}
});