98 lines
4.2 KiB
JavaScript
98 lines
4.2 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.envar) {
|
||
global.envar = rawConfig
|
||
// 不知为何,必须定义成全局变量,才能保证多次require只执行一次。
|
||
console.info('<<<<<<<< Merging Environment Configuration (依次载入基础配置、用户配置、机密配置、命令行参数) <<<<<<<<')
|
||
|
||
// 配置参数(按优先级从低到高):
|
||
// ConfigBasic: 系统常量(全大写) 以及 默认参数(小写开头驼峰式)
|
||
// ConfigCustom: 用户或应用自定义参数。本文件不应纳入版本管理。
|
||
// ConfigSecret: 机密参数,例如哈希盐,webtoken密钥,等等。本文件绝对不能纳入版本管理。
|
||
// 命令行参数
|
||
|
||
console.info('(1) Loading Configuration Files (读取配置文件)')
|
||
let configFile
|
||
|
||
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigBasic.js')))) {
|
||
global.envar = deepmerge(global.envar, 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.envar = deepmerge(global.envar, 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.envar = deepmerge(global.envar, secretConfig)
|
||
console.info(` - ${configFile} is loaded.`)
|
||
} else {
|
||
console.warn(` - ${configFile} is missing.`)
|
||
}
|
||
global.envar = global.envar || {}
|
||
|
||
global.envar.prodev = commander.prodev || global.envar.prodev || process.env.NODE_ENV || 'development' // server = require('express')(); server.get('env') === server.settings.env === process.env.NODE_ENV
|
||
if (global.envar.prodev === 'production' && global.envar.production) {
|
||
console.info(' - Loading Production Configuration (加载生产环境配置)')
|
||
global.envar = deepmerge(global.envar, global.envar.production)
|
||
}
|
||
delete global.envar.production
|
||
|
||
console.info('(2) Loading Command Line Parameters (载入命令行参数)')
|
||
commander.version(global.envar.VERSION || '0.0.1', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
|
||
for (let [key, param, desc] of global.envar.Commander_Option_List || []) {
|
||
commander.option(param, `${desc} Default = "${global.envar[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.envar[key] = commander[key] || global.envar[key]
|
||
}
|
||
}
|
||
}
|
||
|
||
delete global.envar.Commander_Option_List // do not print Commander_Option_List to console
|
||
|
||
console.log('>>>>>>>> Merged Environment Configuration >>>>>>>>')
|
||
return global.envar
|
||
},
|
||
|
||
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 {}
|
||
}
|
||
},
|
||
|
||
maskSecret() {
|
||
let envar = JSON.parse(JSON.stringify(global.envar))
|
||
for (let key in envar) {
|
||
if (my.secretKeys.includes(key)) {
|
||
envar[key] = '***confidential***'
|
||
}
|
||
}
|
||
return envar
|
||
}
|
||
}
|