pandora
Version:
A powerful and lightweight application manager for Node.js applications powered by TypeScript.
63 lines • 2.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const pandora_dollar_1 = require("pandora-dollar");
const const_1 = require("../const");
const LoggerBroker_1 = require("./LoggerBroker");
const extend = require("extend");
class GlobalConfigProcessor {
constructor() {
this.globalConfig = {};
this.initialized = false;
this.loadedConfigPath = [require.resolve('../default')];
}
static getInstance() {
if (!this.instance) {
this.instance = new GlobalConfigProcessor();
}
return this.instance;
}
getAllProperties() {
if (!this.initialized) {
const cwd = process.env[const_1.PANDORA_CWD] || process.cwd();
const cwdRequire = pandora_dollar_1.makeRequire(cwd);
const configPaths = process.env[const_1.PANDORA_GLOBAL_CONFIG] ? process.env[const_1.PANDORA_GLOBAL_CONFIG].split(GlobalConfigProcessor.GLOBAL_PACKAGE_SPLIT) : [];
let globalConfig = require('../default').default;
this.mergeProperties(globalConfig);
for (const configPath of configPaths) {
if (configPath && configPath !== 'undefined') {
try {
let extendConfig = cwdRequire(configPath);
extendConfig = extendConfig.default ? extendConfig.default : extendConfig;
this.mergeProperties(extendConfig);
this.loadedConfigPath.push(configPath);
}
catch (err) {
// info
LoggerBroker_1.consoleLogger.info(`Can't find config from ${configPath}`);
LoggerBroker_1.consoleLogger.warn(err);
}
}
}
this.initialized = true;
}
return this.globalConfig;
}
// merge other properties after init
mergeProperties(properties) {
try {
this.globalConfig = extend(true, this.globalConfig, properties);
}
catch (err) {
// info
LoggerBroker_1.consoleLogger.error(`merge global config error`, err);
}
}
// for test
clearProperties() {
this.globalConfig = {};
this.initialized = false;
}
}
GlobalConfigProcessor.GLOBAL_PACKAGE_SPLIT = ':';
exports.GlobalConfigProcessor = GlobalConfigProcessor;
//# sourceMappingURL=GlobalConfigProcessor.js.map