utils/lib/esm/loadConfig.js

54 lines
7.1 KiB
JavaScript
Raw Permalink Normal View History

2024-01-27 21:29:33 +00:00
"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");
2024-01-28 01:42:43 +00:00
exports.default = (opts, envKey, filePath = "") => {
const { env } = process, defPath = filePath || path_1.default.join(process.cwd() ?? ".", "defaults.json5"), def = (0, utils_1.readJsonIfExists)(defPath),
2024-01-27 21:29:33 +00:00
// 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];
};
2024-01-28 01:42:43 +00:00
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 });
2024-01-27 21:29:33 +00:00
return {
...Object.keys(def).reduce((acc, curr) => {
if (Array.isArray(def[curr]))
acc[curr] = getArray(curr);
else
acc[curr] = getVal(curr);
return acc;
}, {}),
};
};
2024-01-28 01:42:43 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibG9hZENvbmZpZy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9sb2FkQ29uZmlnLn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