2025-04-05 06:50:57 -07:00
|
|
|
import fs from "fs";
|
|
|
|
import fsPromises from "fs/promises";
|
|
|
|
import { logger } from "../utils/logger";
|
|
|
|
import { config, configPath, loadConfig } from "./configService";
|
2025-05-24 01:48:25 -07:00
|
|
|
import { getWebPorts, startWebServer, stopWebServer } from "./webService";
|
2025-04-05 06:50:57 -07:00
|
|
|
|
|
|
|
let amnesia = false;
|
|
|
|
fs.watchFile(configPath, () => {
|
|
|
|
if (amnesia) {
|
|
|
|
amnesia = false;
|
|
|
|
} else {
|
|
|
|
logger.info("Detected a change to config.json, reloading its contents.");
|
|
|
|
try {
|
|
|
|
loadConfig();
|
|
|
|
} catch (e) {
|
|
|
|
logger.error("Failed to reload config.json. Did you delete it?! Execution cannot continue.");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
validateConfig();
|
2025-05-24 01:48:25 -07:00
|
|
|
|
|
|
|
const webPorts = getWebPorts();
|
|
|
|
if (config.httpPort != webPorts.http || config.httpsPort != webPorts.https) {
|
|
|
|
logger.info(`Restarting web server to apply port changes.`);
|
|
|
|
void stopWebServer().then(startWebServer);
|
|
|
|
}
|
2025-04-05 06:50:57 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export const validateConfig = (): void => {
|
|
|
|
if (typeof config.administratorNames == "string") {
|
|
|
|
logger.info(`Updating config.json to make administratorNames an array.`);
|
|
|
|
config.administratorNames = [config.administratorNames];
|
|
|
|
void saveConfig();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const updateConfig = async (data: string): Promise<void> => {
|
|
|
|
amnesia = true;
|
|
|
|
await fsPromises.writeFile(configPath, data);
|
|
|
|
Object.assign(config, JSON.parse(data));
|
|
|
|
};
|
|
|
|
|
|
|
|
export const saveConfig = async (): Promise<void> => {
|
|
|
|
amnesia = true;
|
|
|
|
await fsPromises.writeFile(configPath, JSON.stringify(config, null, 2));
|
|
|
|
};
|