@dazejs/framework
Version:
Daze.js - A powerful web framework for Node.js
146 lines • 5.49 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Config = void 0;
const fs = __importStar(require("fs"));
const glob = __importStar(require("glob"));
const path = __importStar(require("path"));
const illegal_argument_error_1 = require("../errors/illegal-argument-error");
class Config {
constructor(app) {
this._items = {};
this._app = app;
return new Proxy(this, this.proxy());
}
proxy() {
return {
get(t, prop, receiver) {
if (Reflect.has(t, prop) || typeof prop === 'symbol') {
return Reflect.get(t, prop, receiver);
}
return t.get(prop);
}
};
}
async initialize() {
await this.parse();
}
async parse() {
var _a, _b;
if (!fs.existsSync(this._app.configPath))
return;
const currentEnv = this.env;
const filePaths = glob.sync(path.join(this._app.configPath, '**/*.@(js|ts)'), {
nodir: true,
matchBase: true
});
for (const file of filePaths) {
const extname = path.extname(file);
const normalBasename = path.basename(file, extname);
const currentConfig = (await (_a = file, Promise.resolve().then(() => __importStar(require(_a))))).default;
if (!~path.basename(file, extname).indexOf('.')) {
if (!this.has(normalBasename)) {
this.set(normalBasename, currentConfig);
}
else {
const oldConfig = this.get(normalBasename);
if (oldConfig && !Array.isArray(oldConfig) && typeof oldConfig === 'object') {
this.set(normalBasename, Object.assign(Object.assign({}, oldConfig), currentConfig));
}
else {
this.set(normalBasename, currentConfig);
}
}
}
}
for (const file of filePaths) {
const extname = path.extname(file);
const envBasename = path.basename(file, `.${currentEnv}${extname}`);
const currentConfig = (await (_b = file, Promise.resolve().then(() => __importStar(require(_b))))).default;
if (~file.indexOf(`.${currentEnv}${extname}`)) {
if (!this.has(envBasename)) {
this.set(envBasename, currentConfig);
}
else {
const oldConfig = this.get(envBasename);
if (oldConfig && !Array.isArray(oldConfig) && typeof oldConfig === 'object') {
this.set(envBasename, Object.assign(Object.assign({}, oldConfig), currentConfig));
}
else {
this.set(envBasename, currentConfig);
}
}
}
}
return this._items;
}
setValue(names, value, index = 0) {
const name = names[index];
const original = names.slice(0, index).join('.') || '';
const res = this.get(original, {});
const { length, } = names;
if (length > index + 1) {
res[name] = this.setValue(names, value, index + 1);
}
else {
res[name] = value;
}
return res;
}
set(name, value) {
if (typeof name !== 'string')
throw new illegal_argument_error_1.IllegalArgumentError('Config#set name must be String!');
const names = name.split('.');
const nameValue = this.setValue(names, value);
this._items = Object.assign(Object.assign({}, this._items), nameValue);
return this._items;
}
get(name, def) {
let value = this._items;
if (!name) {
return value;
}
const names = String(name).split('.');
for (const n of names) {
if (typeof value === 'object' && !Reflect.has(value, n)) {
value = undefined;
break;
}
value = value[n];
}
return value === undefined ? def : value;
}
has(name = '') {
if (!name) {
return false;
}
return !(this.get(name) === undefined);
}
get env() {
return this._app.getEnv();
}
}
exports.Config = Config;
//# sourceMappingURL=config.js.map