lin-cms-test
Version:
The core library of Lin CMS, this package is just for test!
69 lines (68 loc) • 1.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const path = tslib_1.__importStar(require("path"));
const lodash_1 = require("lodash");
/**
* 挂载到app的context上,你可以通过 ctx.config使用
* 配置实例也独立于app应用,这样才可在全局使用配置
* 否则每次都要拿到app或者ctx才能访问配置
*
* @export
* @class Config
*/
class Config {
constructor() {
/**
* 存储所有配置信息
*
* @private
* @type {Object}
* @memberof Config
*/
this.store = {};
}
initApp(app) {
app.context.config = this;
}
/**
* getItem
*/
getItem(key, defaultVal) {
return lodash_1.get(this.store, key, defaultVal);
}
/**
* hasItem
*/
hasItem(key) {
return lodash_1.has(this.store, key);
}
/**
* setItem
*/
setItem(key, val) {
lodash_1.set(this.store, key, val);
}
/**
* import导入是异步导入
* require导入是同步导入
* getConfigFromFile 选择同步导入配置文件
*/
getConfigFromFile(filepath) {
const baseDir = process.cwd();
const mod = require(path.resolve(baseDir, filepath));
// const conf = get(mod, "default");
this.store = lodash_1.merge(this.store, mod);
}
/**
* getConfigFromObj
*/
getConfigFromObj(obj) {
this.store = lodash_1.merge(this.store, obj);
}
}
exports.Config = Config;
/**
* 全局的config实例
*/
exports.config = new Config();