2024-03-08 06:34:50 +01:00
|
|
|
/**
|
|
|
|
|
* @file index.ts
|
|
|
|
|
* @summary ConfigManager system to retrieve and modify system configuration
|
|
|
|
|
* @description Can read from a hand-written file, config.toml, or from a machine-saved file, config.internal.toml
|
|
|
|
|
* Fuses both and provides a way to retrieve individual values
|
|
|
|
|
*/
|
|
|
|
|
|
2024-05-16 04:37:25 +02:00
|
|
|
import { watchConfig, loadConfig } from "c12";
|
|
|
|
|
import { configValidator, type Config } from "./config.type";
|
|
|
|
|
import { fromError } from "zod-validation-error";
|
|
|
|
|
import chalk from "chalk";
|
2024-04-07 06:16:54 +02:00
|
|
|
|
2024-05-16 04:37:25 +02:00
|
|
|
const { config } = await watchConfig({
|
2024-04-07 07:30:49 +02:00
|
|
|
configFile: "./config/config.toml",
|
|
|
|
|
overrides:
|
|
|
|
|
(
|
2024-05-16 04:37:25 +02:00
|
|
|
await loadConfig<Config>({
|
2024-04-07 07:30:49 +02:00
|
|
|
configFile: "./config/config.internal.toml",
|
|
|
|
|
})
|
|
|
|
|
).config ?? undefined,
|
2024-04-07 06:16:54 +02:00
|
|
|
});
|
|
|
|
|
|
2024-05-16 04:37:25 +02:00
|
|
|
const parsed = await configValidator.safeParseAsync(config);
|
|
|
|
|
|
|
|
|
|
if (!parsed.success) {
|
|
|
|
|
console.log(
|
|
|
|
|
`${chalk.bgRed.white(
|
|
|
|
|
" CRITICAL ",
|
|
|
|
|
)} There was an error parsing the config file at ${chalk.bold(
|
|
|
|
|
"./config/config.toml",
|
|
|
|
|
)}. Please fix the file and try again.`,
|
|
|
|
|
);
|
|
|
|
|
console.log(
|
|
|
|
|
`${chalk.bgRed.white(
|
|
|
|
|
" CRITICAL ",
|
|
|
|
|
)} Follow the installation intructions and get a sample config file from the repository if needed.`,
|
|
|
|
|
);
|
|
|
|
|
console.log(
|
|
|
|
|
`${chalk.bgRed.white(" CRITICAL ")} ${fromError(parsed.error).message}`,
|
|
|
|
|
);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const exportedConfig = parsed.data;
|
2024-04-07 06:16:54 +02:00
|
|
|
|
|
|
|
|
export { exportedConfig as config };
|
|
|
|
|
export type { Config };
|