utils/lib/cjs/loadConfig.js
2024-01-27 20:42:43 -05:00

54 lines
7.1 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, filePath = "") => {
const { env } = process, defPath = filePath || path_1.default.join(process.cwd() ?? ".", "defaults.json5"), def = (0, utils_1.readJsonIfExists)(defPath),
// 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];
};
console.log({ def });
const retVal = {
...Object.keys(def).reduce((acc, curr) => {
if (Array.isArray(def[curr]))
acc[curr] = getArray(curr);
else
acc[curr] = getVal(curr);
return acc;
}, {}),
};
console.log({ retVal });
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibG9hZENvbmZpZy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9sb2FkQ29uZmlnLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7O0FBQUEsZ0RBQXdCO0FBRXhCLG1DQUF5RTtBQUV6RSxrQkFBZSxDQUFJLElB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