98 lines
4.4 KiB
JavaScript
98 lines
4.4 KiB
JavaScript
const fs = require('fs')
|
||
const path = require('path')
|
||
const commander = require('commander')
|
||
const deepmerge = require('deepmerge')
|
||
|
||
const my = { secretKeys:[] }
|
||
|
||
module.exports = {
|
||
mergeConfig(rawConfig) {
|
||
if (!global.EnviConfig) {
|
||
global.EnviConfig = rawConfig
|
||
// 不知为何,必须定义成全局变量,才能保证多次require只执行一次。
|
||
console.info('★★★★★★★★ Starting System Configuration (开始系统配置:依次载入基础配置、用户配置、机密配置、命令行参数) ★★★★★★★★')
|
||
|
||
// 配置参数(按优先级从低到高):
|
||
// ConfigBasic: 系统常量(全大写) 以及 默认参数(小写开头驼峰式)
|
||
// ConfigCustom: 用户或应用自定义参数。本文件不应纳入版本管理。
|
||
// ConfigSecret: 机密参数,例如哈希盐,webtoken密钥,等等。本文件绝对不能纳入版本管理。
|
||
// 命令行参数
|
||
|
||
console.info('(1) Loading Configuration Files (读取配置文件)')
|
||
let configFile
|
||
|
||
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigBasic.js')))) {
|
||
global.EnviConfig = deepmerge(global.EnviConfig, require(configFile))
|
||
console.info(` - ${configFile} is loaded.`)
|
||
} else {
|
||
console.warn(` - ${configFile} is missing.`)
|
||
}
|
||
|
||
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigCustom.js')))) {
|
||
// 如果存在,覆盖掉 ConfigBasic 里的默认参数
|
||
global.EnviConfig = deepmerge(global.EnviConfig, require(configFile)) // 注意,objectMerge后,产生了一个新的对象,而不是在原来的Config里添加
|
||
console.info(`${configFile} is loaded.`)
|
||
} else {
|
||
console.warn(` - ${configFile} is missing.`)
|
||
}
|
||
|
||
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigSecret.js')))) {
|
||
// 如果存在,覆盖掉 ConfigBasic 和 ConfigCustom 里的参数
|
||
let secretConfig = require(configFile)
|
||
my.secretKeys = Object.keys(secretConfig)
|
||
global.EnviConfig = deepmerge(global.EnviConfig, secretConfig)
|
||
console.info(` - ${configFile} is loaded.`)
|
||
} else {
|
||
console.warn(` - ${configFile} is missing.`)
|
||
}
|
||
global.EnviConfig = global.EnviConfig || {}
|
||
|
||
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.EnviConfig = deepmerge(global.EnviConfig, global.EnviConfig.production)
|
||
}
|
||
delete global.EnviConfig.production
|
||
|
||
console.info('(2) Loading Command Line Parameters (载入命令行参数)')
|
||
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)
|
||
|
||
console.log('(3) Merging Command Line Parameters into Configuration (把命令行参数合并入配置)')
|
||
for (let key in commander) {
|
||
if (typeof commander[key] === 'string' && !/^_/.test(key)) {
|
||
global.EnviConfig[key] = commander[key] || global.EnviConfig[key]
|
||
}
|
||
}
|
||
}
|
||
|
||
delete global.EnviConfig.commanderOptions // do not print commanderOptions to console
|
||
|
||
console.log('######## Completed Environment Configuration ########')
|
||
return global.EnviConfig
|
||
},
|
||
|
||
getDynamicConfig(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 {}
|
||
}
|
||
},
|
||
|
||
showEnvi() {
|
||
let envi = JSON.parse(JSON.stringify(global.EnviConfig))
|
||
for (let key in envi) {
|
||
if (my.secretKeys.hasOwnProperty(key)) {
|
||
envi[key] = '******'
|
||
}
|
||
}
|
||
console.log(envi)
|
||
}
|
||
}
|