diff --git a/index.js b/index.js index 4c22fc7..6e6efcd 100644 --- a/index.js +++ b/index.js @@ -4,8 +4,8 @@ const commander = require('commander') const deepmerge = require('deepmerge') module.exports = function (rawConfig) { - if (!global.SysConfig) { - global.SysConfig = rawConfig + if (!global.EnviConfig) { + global.EnviConfig = rawConfig // 不知为何,必须定义成全局变量,才能保证多次require只执行一次。 console.info('★★★★★★★★ Starting System Configuration (开始系统配置:依次载入基础配置、用户配置、机密配置、命令行参数) ★★★★★★★★') @@ -18,49 +18,49 @@ module.exports = function (rawConfig) { console.info('- Loading Configuration Files (读取配置文件)') let configFile if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigBasic.js')))) { - global.SysConfig = deepmerge(global.SysConfig, require(configFile)) + global.EnviConfig = deepmerge(global.EnviConfig, require(configFile)) console.info(` - ${configFile} loaded`) } else { console.warn(` - Missing and omitting ${configFile}`) } if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigCustom.js')))) { // 如果存在,覆盖掉 ConfigBasic 里的默认参数 - global.SysConfig = deepmerge(global.SysConfig, require(configFile)) // 注意,objectMerge后,产生了一个新的对象,而不是在原来的Config里添加 + global.EnviConfig = deepmerge(global.EnviConfig, require(configFile)) // 注意,objectMerge后,产生了一个新的对象,而不是在原来的Config里添加 console.info(`${configFile} loaded`) } else { console.warn(` - Missing and omitting ${configFile}`) } if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigSecret.js')))) { // 如果存在,覆盖掉 ConfigBasic 和 ConfigCustom 里的参数 - global.SysConfig = deepmerge(global.SysConfig, require(configFile)) + global.EnviConfig = deepmerge(global.EnviConfig, require(configFile)) console.info(`${configFile} loaded`) } else { console.warn(` - Missing and omitting ${configFile}`) } - global.SysConfig = global.SysConfig || {} + global.EnviConfig = global.EnviConfig || {} console.info('- Loading Command Line Parameters (载入命令行参数)') - commander.version(global.SysConfig.VERSION || '0.0.1', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version' - for (let [key, param, desc] of global.SysConfig.commanderOptions || []) { - commander.option(param, `${desc} Default = "${global.SysConfig[key]}"`) + commander.version(global.EnviConfig.VERSION || '0.0.1', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version' + for (let [key, param, desc] of global.EnviConfig.commanderOptions || []) { + commander.option(param, `${desc} Default = "${global.EnviConfig[key]}"`) } commander.parse(process.argv) - global.SysConfig.runenv = commander.runenv || global.SysConfig.runenv || process.env.NODE_ENV || 'development' // server = require('express')(); server.get('env) === server.settings.env === process.env.NODE_ENV - if (global.SysConfig.runenv === 'production' && global.SysConfig.production) { + global.EnviConfig.prodev = commander.prodev || global.EnviConfig.prodev || process.env.NODE_ENV || 'development' // server = require('express')(); server.get('env') === server.settings.env === process.env.NODE_ENV + if (global.EnviConfig.prodev === 'production' && global.EnviConfig.production) { console.info('- Loading Production Configuration (加载生产环境配置)') - global.SysConfig = deepmerge(global.SysConfig, global.SysConfig.production) + global.EnviConfig = deepmerge(global.EnviConfig, global.EnviConfig.production) } - delete global.SysConfig.production + delete global.EnviConfig.production console.log('- Merging Command Line Parameters into Configuration (把命令行参数合并入配置)') for (let key in commander) { if (typeof commander[key] === 'string' && !/^_/.test(key)) { - global.SysConfig[key] = commander[key] || global.SysConfig[key] + global.EnviConfig[key] = commander[key] || global.EnviConfig[key] } } } - console.log('- Final Configuration: ', global.SysConfig) + console.log('- Final Configuration: ', global.EnviConfig) console.log('######## Completed System Configuration ########') - return global.SysConfig + return global.EnviConfig }