refactor(config): 🔥 Replace config validation with Zod

This commit is contained in:
Jesse Wierzbinski 2024-05-15 16:37:25 -10:00
parent 093337dd4f
commit fb31375b74
No known key found for this signature in database
15 changed files with 543 additions and 3491 deletions

File diff suppressed because it is too large Load diff

View file

@ -5,22 +5,43 @@
* Fuses both and provides a way to retrieve individual values
*/
import { watchConfig } from "c12";
import { type Config, defaultConfig } from "./config.type";
import { watchConfig, loadConfig } from "c12";
import { configValidator, type Config } from "./config.type";
import { fromError } from "zod-validation-error";
import chalk from "chalk";
const { config } = await watchConfig<Config>({
const { config } = await watchConfig({
configFile: "./config/config.toml",
defaultConfig: defaultConfig,
overrides:
(
await watchConfig<Config>({
await loadConfig<Config>({
configFile: "./config/config.internal.toml",
defaultConfig: {} as Config,
})
).config ?? undefined,
});
const exportedConfig = config ?? defaultConfig;
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;
export { exportedConfig as config };
export type { Config };

View file

@ -4,6 +4,8 @@
"main": "index.ts",
"type": "module",
"dependencies": {
"c12": "^1.10.0"
"c12": "^1.10.0",
"zod": "^3.23.8",
"zod-validation-error": "^3.3.0"
}
}