wo-base-envar/index.js

74 lines
3.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const fs = require('fs')
const path = require('path')
const commander = require('commander')
const deepmerge = require('deepmerge')
const my = { secretKeys:[] }
module.exports = {
mergeConfig({rawConfig={}, envarFiles=['./envar-base-basic.js', './envar-base-custom.js', './envar-base-secret.js']} = {}) {
if (!global.envar) {
global.envar = rawConfig
// 不知为何必须定义成全局变量才能保证多次require只执行一次。
console.info('<<<<<<<< Collecting Environment Configuration <<<<<<<<')
console.info('- Loading Configuration Files (读取配置文件)')
for (let configFile of envarFiles){
if (fs.existsSync(path.resolve(configFile))) {
global.envar = deepmerge(global.envar, require(path.resolve(configFile)))
console.info(` - ${configFile} is loaded.`)
} else {
console.warn(` - ${configFile} is missing.`)
}
}
global.envar.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.ENV_PRODUCTION) {
console.info('- Applying Production Configuration (加载生产环境配置)')
global.envar = deepmerge(global.envar, global.envar.ENV_PRODUCTION) // 注意objectMerge后产生了一个新的对象而不是在原来的Config里添加
delete global.envar.ENV_PRODUCTION
}
console.info('- Loading Command Line Parameters (载入命令行参数)')
commander.version(global.envar.Base_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)
delete global.envar.Commander_Option_List // do not print Commander_Option_List to console
console.log('- Merging Command Line Parameters into Configuration (把命令行参数值合并入配置)')
for (let key in commander) {
if (!/^_/.test(key) && typeof commander[key] === 'string') { // commander 自带了一批 _开头的属性过滤掉
global.envar[key] = commander[key]
}
}
}
console.log('>>>>>>>> Collected Environment Configuration >>>>>>>>')
return global.envar
},
getDynamicConfig(dynamicConfigFile='envar-base-dynamic.js') { // dynamicConfigFile should be absolute or relative to the node process's dir.
const fullpath = path.resolve(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 (/^SECRET_/.test(key)) {
envar[key] = '*** confidentical ***'
}
}
return envar
}
}