@anyme/anymejs
Version:
158 lines (154 loc) • 5.81 kB
JavaScript
'use strict';
var default_config = require('./default.config.js');
var require$$0 = require('node:path');
var index$1 = require('../_virtual/index.js');
var index = require('../utils/index.js');
class Config {
#config = default_config.CONFIG;
fileGroups = new Map();
configs = new Map();
path = process.env.CONFIG_PATH || "./config";
env = process.env.NODE_ENV || "development";
ignore = ["**/node_modules/**", "**/dist/**", "**/*.d.ts"];
order = [".ts", ".js", ".mjs", ".cjs", ".json"];
constructor() {
this.fileGroups = this.groupConfigs(this.loadPaths());
}
async get(name) {
if (!name)
return await this.loadCore();
if (this.configs.has(name))
return this.configs.get(name);
if (this.fileGroups.has(name)) {
const module = await this.importConfig(this.fileGroups.get(name));
if (!index.isEmpty(module)) {
this.configs.set(name, module);
return module;
}
}
return undefined;
}
async loadCore() {
if (this.configs.has("core") || index.isEmpty(this.fileGroups))
return this.#config;
await this.loadAllConfigs();
this.#config = index.deepMerge(this.#config, ...this.getCoreConfigs());
this.loadEnvConfig();
this.resolveServerPaths();
this.validate();
this.configs.set("core", this.#config);
return this.#config;
}
getCoreConfigs() {
const configs = [];
if (this.configs.has("default")) {
configs.push(this.configs.get("default"));
}
if (this.env === "development" && this.configs.has("local")) {
configs.push(this.configs.get("local"));
}
else if (this.env === "production" && this.configs.has("prod")) {
configs.push(this.configs.get("prod"));
}
else if (this.env && this.configs.has(this.env)) {
configs.push(this.configs.get(this.env));
}
return configs;
}
loadAllConfigs() {
return index.all(this.fileGroups, async ([key, path]) => {
const module = await this.importConfig(path);
if (!index.isEmpty(module))
this.configs.set(key, module);
return module;
});
}
loadPaths() {
return index$1.default.sync(this.getPattern(), {
onlyFiles: true,
ignore: this.ignore,
absolute: true,
});
}
groupConfigs(paths) {
const fileGroups = new Map();
for (const path of paths) {
const ext = require$$0.extname(path);
const index = this.order.indexOf(ext);
if (index === -1)
continue;
const fileName = require$$0.basename(path, ext).slice(0, -7);
if (!fileGroups.has(fileName))
fileGroups.set(fileName, path);
else {
const oldIndex = this.order.indexOf(require$$0.extname(fileGroups.get(fileName)));
if (index < oldIndex)
fileGroups.set(fileName, path);
}
}
return fileGroups;
}
async importConfig(path) {
try {
if (require$$0.extname(path) === ".json")
return index.importJson(path);
const module = await index.importModule(path);
const result = index.isFunction(module) ? module(index.ctx()) : module;
return index.isEmpty(result) ? {} : result;
}
catch (error) {
console.error("❌ Failed to load config:", error);
throw error;
}
}
async loadEnvConfig() {
default_config.ENV_KEY_VALUES.forEach((item) => {
if (process.env[item.value]) {
if (item.type === "number")
this.merge(item.key, parseInt(process.env[item.value]));
else if (item.type === "boolean")
this.merge(item.key, process.env[item.value] === "true");
else if (item.type === "resolve")
this.merge(item.key, require$$0.resolve(process.env[item.value]));
else
this.merge(item.key, process.env[item.value]);
}
});
}
getPattern() {
const ext = this.order.join(",");
return require$$0.join(this.path, `*.config{${ext}}`).replace(/\\/g, "/");
}
merge(str, value) {
this.#config = index.deepMerge(this.#config, index.set(str, value));
}
async validate() {
if (!this.#config.session?.client?.secret) {
console.warn("⚠️ Session secret is not set.");
}
if (this.env === "production") {
if (this.#config.session?.enable) {
if (!this.#config.session?.client?.cookie?.secure) {
console.warn("⚠️ Forcing secure cookies in production environment");
}
}
if (this.#config.db?.enable) {
if (this.#config.db?.client?.synchronize) {
console.warn("⚠️ Database synchronization is enabled in production environment");
}
}
}
}
resolveServerPaths() {
const serverPaths = ["controllers", "middlewares", "interceptors"];
serverPaths.forEach((key) => {
if (this.#config.server?.route?.[key]?.length === 1 &&
typeof this.#config.server?.route?.[key][0] === "string") {
const path = this.#config.server.route[key][0];
this.merge(`server.route.${key}`, [index.getAbsolutePath(path)]);
}
});
}
}
exports.Config = Config;
//# sourceMappingURL=index.js.map