From 12740a2d063ab6a790b2c2466a582c53fef0f422 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Sun, 6 Apr 2025 21:40:00 +0200 Subject: [PATCH] feat(config): :sparkles: Allow specifying config path via env variable --- config.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/config.ts b/config.ts index 75a265ec..b8d7e9f8 100644 --- a/config.ts +++ b/config.ts @@ -5,19 +5,23 @@ * 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 chalk from "chalk"; import type { z } from "zod"; import { fromZodError } from "zod-validation-error"; import { ConfigSchema } from "./classes/config/schema.ts"; -if (!(await file("config/config.toml").exists())) { - throw new Error("config.toml does not or is not accessible."); +const CONFIG_LOCATION = env.CONFIG_LOCATION ?? "./config/config.toml"; + +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>({ - configFile: "./config/config.toml", + configFile: CONFIG_LOCATION, overrides: ( await loadConfig>({ @@ -30,7 +34,7 @@ const parsed = await ConfigSchema.safeParseAsync(config); if (!parsed.success) { console.error( - `⚠ Error encountered while loading ${chalk.gray("config.toml")}.`, + `⚠ Error encountered while loading ${chalk.gray(CONFIG_LOCATION)}.`, ); console.error( "⚠ This is due to invalid, missing or incorrect values in the configuration file.",