UNPKG

configuru

Version:

Manage the configuration of your Nodejs application with multiple environments and custom preferences, utilizing Configuru in CI and development as well!

100 lines 3.79 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createConfigStorage = void 0; var fs_1 = require("fs"); var path_1 = require("path"); var helpers_1 = require("./helpers"); var fileExistsSync = function (path) { try { return (0, fs_1.statSync)(path).isFile(); } catch (_error) { return false; } }; var fromPairs = function (pairs) { return Object.assign.apply(Object, __spreadArray([{}], Array.from(pairs, function (_a) { var _b; var k = _a[0], v = _a[1]; return (_b = {}, _b[k] = v, _b); }), false)); }; var uniq = function (xs) { return Array.from(new Set(xs)); }; var keys = Object.keys; var loadInline = function (secrets) { try { return helpers_1.JSONC.parse(secrets); } catch (_a) { throw new Error('Invalid inline secrets'); } }; var loadFile = function (filePath) { if (!filePath) return {}; var _a = (0, path_1.parse)(filePath), dir = _a.dir, name = _a.name; var testPaths = uniq([ filePath, (0, path_1.format)({ dir: dir, name: name, ext: '.json' }), (0, path_1.format)({ dir: dir, name: name, ext: '.jsonc' }), ]); var resolvedPath = testPaths.find(fileExistsSync); if (!resolvedPath) { throw new Error("File path set, but none of the following tested derivations exist:\n".concat(testPaths .map(function (p) { return " - ".concat((0, path_1.join)((0, path_1.resolve)('.'), p)); }) .join('\n'))); } try { return helpers_1.JSONC.parse((0, fs_1.readFileSync)(resolvedPath, 'utf-8')); } catch (_error) { throw new Error("Invalid config file in ".concat((0, path_1.join)((0, path_1.resolve)('.'), resolvedPath))); } }; var isInlineSecret = function (secretsOrFilename) { var _a; return (_a = secretsOrFilename === null || secretsOrFilename === void 0 ? void 0 : secretsOrFilename.trim().startsWith('{')) !== null && _a !== void 0 ? _a : false; }; var loadSecrets = function (secretsOrFilename) { if (isInlineSecret(secretsOrFilename)) { return loadInline(secretsOrFilename); } return loadFile(secretsOrFilename); }; var createConfigStorage = function (opts) { var defaultConfig = loadSecrets(opts.defaultConfigPath); var userConfig = loadSecrets(opts.userConfigPath); var envConfig = {}; if (opts.envMode === 'default' || opts.envMode === 'merged') { var configKeys = __spreadArray(__spreadArray([], keys(defaultConfig), true), (opts.envMode === 'merged' ? keys(userConfig) : []), true); envConfig = fromPairs(configKeys .map(function (k) { return [k, process.env[k]]; }) .filter(function (x) { return x[1] !== undefined; })); } else if (opts.envMode === 'all') { envConfig = process.env; } return __assign(__assign(__assign({}, defaultConfig), userConfig), envConfig); }; exports.createConfigStorage = createConfigStorage; //# sourceMappingURL=storage.js.map