feat(config): Allow specifying config path via env variable

This commit is contained in:
Jesse Wierzbinski 2025-04-06 21:40:00 +02:00
parent 838f2fd4cf
commit 12740a2d06
No known key found for this signature in database

View file

@ -5,19 +5,23 @@
* Fuses both and provides a way to retrieve individual values * Fuses both and provides a way to retrieve individual values
*/ */
import { file } from "bun"; import { env, file } from "bun";
import { loadConfig, watchConfig } from "c12"; import { loadConfig, watchConfig } from "c12";
import chalk from "chalk"; import chalk from "chalk";
import type { z } from "zod"; import type { z } from "zod";
import { fromZodError } from "zod-validation-error"; import { fromZodError } from "zod-validation-error";
import { ConfigSchema } from "./classes/config/schema.ts"; import { ConfigSchema } from "./classes/config/schema.ts";
if (!(await file("config/config.toml").exists())) { const CONFIG_LOCATION = env.CONFIG_LOCATION ?? "./config/config.toml";
throw new Error("config.toml does not or is not accessible.");
if (!(await file(CONFIG_LOCATION).exists())) {
throw new Error(
`config file at "${CONFIG_LOCATION}" does not exist or is not accessible.`,
);
} }
const { config } = await watchConfig<z.infer<typeof ConfigSchema>>({ const { config } = await watchConfig<z.infer<typeof ConfigSchema>>({
configFile: "./config/config.toml", configFile: CONFIG_LOCATION,
overrides: overrides:
( (
await loadConfig<z.infer<typeof ConfigSchema>>({ await loadConfig<z.infer<typeof ConfigSchema>>({
@ -30,7 +34,7 @@ const parsed = await ConfigSchema.safeParseAsync(config);
if (!parsed.success) { if (!parsed.success) {
console.error( console.error(
`⚠ Error encountered while loading ${chalk.gray("config.toml")}.`, `⚠ Error encountered while loading ${chalk.gray(CONFIG_LOCATION)}.`,
); );
console.error( console.error(
"⚠ This is due to invalid, missing or incorrect values in the configuration file.", "⚠ This is due to invalid, missing or incorrect values in the configuration file.",