upgrade to include mergeConfig() and getDynamicConfig()
This commit is contained in:
parent
41d5fe36fd
commit
533d8126b2
118
index.js
118
index.js
@ -3,64 +3,76 @@ const path = require('path')
|
|||||||
const commander = require('commander')
|
const commander = require('commander')
|
||||||
const deepmerge = require('deepmerge')
|
const deepmerge = require('deepmerge')
|
||||||
|
|
||||||
module.exports = function (rawConfig) {
|
module.exports = {
|
||||||
if (!global.EnviConfig) {
|
mergeConfig: function(rawConfig) {
|
||||||
global.EnviConfig = rawConfig
|
if (!global.EnviConfig) {
|
||||||
// 不知为何,必须定义成全局变量,才能保证多次require只执行一次。
|
global.EnviConfig = rawConfig
|
||||||
console.info('★★★★★★★★ Starting System Configuration (开始系统配置:依次载入基础配置、用户配置、机密配置、命令行参数) ★★★★★★★★')
|
// 不知为何,必须定义成全局变量,才能保证多次require只执行一次。
|
||||||
|
console.info('★★★★★★★★ Starting System Configuration (开始系统配置:依次载入基础配置、用户配置、机密配置、命令行参数) ★★★★★★★★')
|
||||||
|
|
||||||
// 配置参数(按优先级从低到高):
|
// 配置参数(按优先级从低到高):
|
||||||
// ConfigBasic: 系统常量(全大写) 以及 默认参数(小写开头驼峰式)
|
// ConfigBasic: 系统常量(全大写) 以及 默认参数(小写开头驼峰式)
|
||||||
// ConfigCustom: 用户或应用自定义参数。本文件不应纳入版本管理。
|
// ConfigCustom: 用户或应用自定义参数。本文件不应纳入版本管理。
|
||||||
// ConfigSecret: 机密参数,例如哈希盐,webtoken密钥,等等。本文件绝对不能纳入版本管理。
|
// ConfigSecret: 机密参数,例如哈希盐,webtoken密钥,等等。本文件绝对不能纳入版本管理。
|
||||||
// 命令行参数
|
// 命令行参数
|
||||||
|
|
||||||
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.EnviConfig = deepmerge(global.EnviConfig, 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.EnviConfig = deepmerge(global.EnviConfig, 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.EnviConfig = deepmerge(global.EnviConfig, 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.EnviConfig = global.EnviConfig || {}
|
global.EnviConfig = global.EnviConfig || {}
|
||||||
|
|
||||||
console.info('- Loading Command Line Parameters (载入命令行参数)')
|
console.info('- Loading Command Line Parameters (载入命令行参数)')
|
||||||
commander.version(global.EnviConfig.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.EnviConfig.commanderOptions || []) {
|
for (let [key, param, desc] of global.EnviConfig.commanderOptions || []) {
|
||||||
commander.option(param, `${desc} Default = "${global.EnviConfig[key]}"`)
|
commander.option(param, `${desc} Default = "${global.EnviConfig[key]}"`)
|
||||||
}
|
}
|
||||||
commander.parse(process.argv)
|
commander.parse(process.argv)
|
||||||
|
|
||||||
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
|
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) {
|
if (global.EnviConfig.prodev === 'production' && global.EnviConfig.production) {
|
||||||
console.info('- Loading Production Configuration (加载生产环境配置)')
|
console.info('- Loading Production Configuration (加载生产环境配置)')
|
||||||
global.EnviConfig = deepmerge(global.EnviConfig, global.EnviConfig.production)
|
global.EnviConfig = deepmerge(global.EnviConfig, global.EnviConfig.production)
|
||||||
}
|
}
|
||||||
delete global.EnviConfig.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.EnviConfig[key] = commander[key] || global.EnviConfig[key]
|
global.EnviConfig[key] = commander[key] || global.EnviConfig[key]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
console.log('- Final Configuration: ', global.EnviConfig)
|
||||||
console.log('- Final Configuration: ', global.EnviConfig)
|
console.log('######## Completed System Configuration ########')
|
||||||
console.log('######## Completed System Configuration ########')
|
return global.EnviConfig
|
||||||
return global.EnviConfig
|
},
|
||||||
|
|
||||||
|
getDynamicConfig: function (dynamicConfigFile='ConfigDynamic.js') { // dynamicConfigFile should be absolute or relative to the node process's dir.
|
||||||
|
const fullpath = path.join(process.cwd(), dynamicConfigFile)
|
||||||
|
if (fs.existsSync(fullpath)) {
|
||||||
|
delete require.cache[require.resolve(fullpath)] // delete require.cache[fullpath] 不起作用
|
||||||
|
return require(fullpath)
|
||||||
|
} else {
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user