UNPKG

@nova-ts/core

Version:

A serverside framework used to build scalable application

41 lines (40 loc) 1.3 kB
// src/Utils/ConfigLoader.ts import path from "path"; import * as fs from "fs"; import * as yml from "js-yaml"; var ConfigLoader = class { static config; /** * Loads the configuration file (default: `application.yml`) from the specified directory * and caches it statically. * * @param directory - Optional directory path where the config file is located. * Defaults to current working directory. * @throws Will throw an error if no config file is found. */ static load(directory) { const filePath = directory || "application.yml"; const absolutPath = path.resolve(process.cwd(), filePath); try { const file = fs.readFileSync(absolutPath, "utf8"); this.config = yml.load(file); } catch (error) { console.error(error); this.config = {}; } } /** * Gets a nested value from the config using dot notation (e.g., "server.port"). * * @param path - Dot-notated path to a specific configuration key. * @returns The resolved value or `undefined` if not found. */ static get(key, defaultValue) { const value = key.split(".").reduce((o, i) => o ? o[i] : void 0, this.config); return value !== void 0 ? value : defaultValue; } }; export { ConfigLoader }; //# sourceMappingURL=chunk-VUR3D4HI.js.map