UNPKG

@codesupport/inherited-config

Version:

Project configuration system with inheritance support for multiple environments.

62 lines (61 loc) 2.49 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const process = __importStar(require("process")); const path = __importStar(require("path")); const fs = __importStar(require("fs")); class InheritedConfig { constructor(options) { var _a; this.config = {}; this.options = { log: false, path: "" }; const root = path.resolve(process.cwd(), (_a = options === null || options === void 0 ? void 0 : options.path) !== null && _a !== void 0 ? _a : ""); const productionConfig = path.resolve(root, "config.json"); this.options = Object.assign(Object.assign({}, this.options), options); if (fs.existsSync(productionConfig)) { this.config = this.require(productionConfig); const env = process.env.NODE_ENV; const envConfig = path.resolve(root, `config.${env === null || env === void 0 ? void 0 : env.toLowerCase()}.json`); if (env && fs.existsSync(envConfig)) { this.config = Object.assign(Object.assign({}, this.config), this.require(envConfig)); } } else { this.log(`Failed to initialise Inherited Config: no file at location '${productionConfig}'.`); } } require(path) { // eslint-disable-next-line global-require return require(path); } log(message) { if (this.options.log) { console.debug(`[@codesupport/inherited-config DEBUG] ${message}`); } } getValue(key) { return this.config[key]; } } exports.default = InheritedConfig;