utils/lib/cjs/loadConfig.js

43 lines
5.9 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const utils_1 = require("./utils");
exports.default = (opts, envKey, localDefaultFilePath = "") => {
const { env } = process, { __dirname } = (0, utils_1.getDirname)(), def = (0, utils_1.readJsonIfExists)(path_1.default.join(__dirname, localDefaultFilePath || "./defaults.json5")),
// gets value from ENV || options || defaults (in that order)
getVal = (envName) => {
const snakeEnvName = `${envKey}_${(0, utils_1.convertCamelToUpperSnakeCase)(envName)}`;
if (env[snakeEnvName])
return env[snakeEnvName];
if (opts[envName])
return opts[envName];
return def[envName];
},
// gets array from ENV || options || defaults (in that order)
getArray = (envName, optName = "") => {
let newEnvName;
if (optName === "") {
optName = envName;
newEnvName = (0, utils_1.convertCamelToUpperSnakeCase)(envName);
}
newEnvName = `${envKey}_${String(envName)}`;
if (env[newEnvName])
return env?.[newEnvName]?.split(path_1.default.delimiter);
if (Array.isArray(opts[optName]) && opts[optName].length)
return opts[optName];
return def[optName];
};
return {
...Object.keys(def).reduce((acc, curr) => {
if (Array.isArray(def[curr]))
acc[curr] = getArray(curr);
else
acc[curr] = getVal(curr);
return acc;
}, {}),
};
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibG9hZENvbmZpZy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9sb2FkQ29uZmlnLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7O0FBQUEsZ0RBQXdCO0FBRXhCLG1DQUlpQjtBQUVqQixrQkFBZSxDQUNkLElBQWdCLEVBQ2hCLE1BQWMsRUFDZCxvQkFBb0IsR0FBRyxFQUFFLEVBQ3hCLEVBQUU7SUFDSCxNQUFNLEVBQUUsR0FBRyxFQUFFLEdBQUcsT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