rename envi to envar
This commit is contained in:
parent
f5b7abe635
commit
87e6a7ab85
42
index.js
42
index.js
@ -7,8 +7,8 @@ const my = { secretKeys:[] }
|
||||
|
||||
module.exports = {
|
||||
mergeConfig(rawConfig) {
|
||||
if (!global.EnviConfig) {
|
||||
global.EnviConfig = rawConfig
|
||||
if (!global.envar) {
|
||||
global.envar = rawConfig
|
||||
// 不知为何,必须定义成全局变量,才能保证多次require只执行一次。
|
||||
console.info('<<<<<<<< Merging Environment Configuration (依次载入基础配置、用户配置、机密配置、命令行参数) <<<<<<<<')
|
||||
|
||||
@ -22,7 +22,7 @@ module.exports = {
|
||||
let configFile
|
||||
|
||||
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigBasic.js')))) {
|
||||
global.EnviConfig = deepmerge(global.EnviConfig, require(configFile))
|
||||
global.envar = deepmerge(global.envar, require(configFile))
|
||||
console.info(` - ${configFile} is loaded.`)
|
||||
} else {
|
||||
console.warn(` - ${configFile} is missing.`)
|
||||
@ -30,7 +30,7 @@ module.exports = {
|
||||
|
||||
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigCustom.js')))) {
|
||||
// 如果存在,覆盖掉 ConfigBasic 里的默认参数
|
||||
global.EnviConfig = deepmerge(global.EnviConfig, require(configFile)) // 注意,objectMerge后,产生了一个新的对象,而不是在原来的Config里添加
|
||||
global.envar = deepmerge(global.envar, require(configFile)) // 注意,objectMerge后,产生了一个新的对象,而不是在原来的Config里添加
|
||||
console.info(`${configFile} is loaded.`)
|
||||
} else {
|
||||
console.warn(` - ${configFile} is missing.`)
|
||||
@ -40,39 +40,39 @@ module.exports = {
|
||||
// 如果存在,覆盖掉 ConfigBasic 和 ConfigCustom 里的参数
|
||||
let secretConfig = require(configFile)
|
||||
my.secretKeys = Object.keys(secretConfig)
|
||||
global.EnviConfig = deepmerge(global.EnviConfig, secretConfig)
|
||||
global.envar = deepmerge(global.envar, secretConfig)
|
||||
console.info(` - ${configFile} is loaded.`)
|
||||
} else {
|
||||
console.warn(` - ${configFile} is missing.`)
|
||||
}
|
||||
global.EnviConfig = global.EnviConfig || {}
|
||||
global.envar = global.envar || {}
|
||||
|
||||
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) {
|
||||
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.EnviConfig = deepmerge(global.EnviConfig, global.EnviConfig.production)
|
||||
global.envar = deepmerge(global.envar, global.envar.production)
|
||||
}
|
||||
delete global.EnviConfig.production
|
||||
delete global.envar.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.Commander_Option_List || []) {
|
||||
commander.option(param, `${desc} Default = "${global.EnviConfig[key]}"`)
|
||||
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.EnviConfig[key] = commander[key] || global.EnviConfig[key]
|
||||
global.envar[key] = commander[key] || global.envar[key]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delete global.EnviConfig.Commander_Option_List // do not print Commander_Option_List to console
|
||||
delete global.envar.Commander_Option_List // do not print Commander_Option_List to console
|
||||
|
||||
console.log('>>>>>>>> Merged Environment Configuration >>>>>>>>')
|
||||
return global.EnviConfig
|
||||
return global.envar
|
||||
},
|
||||
|
||||
getDynamicConfig(dynamicConfigFile='ConfigDynamic.js') { // dynamicConfigFile should be absolute or relative to the node process's dir.
|
||||
@ -85,13 +85,13 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
showEnvi() {
|
||||
let envi = JSON.parse(JSON.stringify(global.EnviConfig))
|
||||
for (let key in envi) {
|
||||
maskSecret() {
|
||||
let envar = JSON.parse(JSON.stringify(global.envar))
|
||||
for (let key in envar) {
|
||||
if (my.secretKeys.includes(key)) {
|
||||
envi[key] = '***confidential***'
|
||||
envar[key] = '***confidential***'
|
||||
}
|
||||
}
|
||||
console.log(JSON.parse(JSON.stringify(envi, Object.keys(envi).sort())))
|
||||
return envar
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user