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