UNPKG

dash-core

Version:

A foundational toolkit of types, collections, services, and architectural patterns designed to accelerate application development.

51 lines (50 loc) 2.26 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigReader = void 0; const file_1 = require("./file"); const path_1 = __importDefault(require("path")); /** Reads config files and merges with defaults.*/ class ConfigReader { logger; constructor(logger) { this.logger = logger; } /** * Reads the configuration from the specified file and populates the given object with values from the file. * @param {string} configPath - The path to the configuration file (e.g., './config.json'). * @param {TConfig} defaultConfig - The object that contains the default configuration values. * @returns {Promise<TConfig>} A promise that resolves to the merged configuration object. * @template TConfig - The type of the configuration object. */ async readConfig(configPath, defaultConfig) { try { return ConfigReader.readConfig(configPath, defaultConfig); } catch (error) { this.logger?.error(error instanceof Error ? error.message : String(error)); return defaultConfig; } } /** * Reads the configuration from the specified file and populates the given object with values from the file. * @param {string} configPath - The path to the configuration file (e.g., './config.json'). * @param {TConfig} defaultConfig - The object that contains the default configuration values. * @returns {Promise<TConfig>} A promise that resolves to the merged configuration object. * @template TConfig - The type of the configuration object. */ static async readConfig(configPath, defaultConfig) { try { const fullPath = path_1.default.resolve(configPath); const fileContent = await file_1.IOFile.read(fullPath); const parsedConfig = JSON.parse(fileContent); return Object.assign({}, defaultConfig, parsedConfig); } catch (error) { throw new Error('Error reading or parsing the configuration file: ' + error); } } } exports.ConfigReader = ConfigReader;