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:50:07 +02:00
|
|
|
import { loadConfig, watchConfig } from "c12";
|
2024-06-14 11:26:20 +02:00
|
|
|
import { fromZodError } from "zod-validation-error";
|
2024-05-16 04:50:07 +02:00
|
|
|
import { type Config, configValidator } from "./config.type";
|
2024-11-02 00:43:33 +01:00
|
|
|
export type { Config } from "./config.type";
|
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) {
|
2024-06-14 11:26:20 +02:00
|
|
|
console.error("Invalid config file:");
|
2024-10-03 11:59:26 +02:00
|
|
|
throw fromZodError(parsed.error).message;
|
2024-05-16 04:37:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const exportedConfig = parsed.data;
|
2024-04-07 06:16:54 +02:00
|
|
|
|
|
|
|
|
export { exportedConfig as config };
|