添加 showEnvi(),过滤掉机密数据再输出,以防被存入日志文件

This commit is contained in:
陆柯 2022-03-12 08:39:50 +08:00
parent a3c3b0c231
commit fa009491ac

View File

@ -3,8 +3,10 @@ const path = require('path')
const commander = require('commander') const commander = require('commander')
const deepmerge = require('deepmerge') const deepmerge = require('deepmerge')
const my = { secretKeys:[] }
module.exports = { module.exports = {
mergeConfig: function(rawConfig) { mergeConfig(rawConfig) {
if (!global.EnviConfig) { if (!global.EnviConfig) {
global.EnviConfig = rawConfig global.EnviConfig = rawConfig
// 不知为何必须定义成全局变量才能保证多次require只执行一次。 // 不知为何必须定义成全局变量才能保证多次require只执行一次。
@ -33,7 +35,9 @@ module.exports = {
} }
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)) let secretConfig = require(configFile)
my.secretKeys = Object.keys(secretConfig)
global.EnviConfig = deepmerge(global.EnviConfig, secretConfig)
console.info(`${configFile} loaded`) console.info(`${configFile} loaded`)
} else { } else {
console.warn(` - Missing and omitting ${configFile}`) console.warn(` - Missing and omitting ${configFile}`)
@ -61,13 +65,14 @@ module.exports = {
} }
} }
} }
delete global.EnviConfig.commanderOptions // do not print commanderOptions to console delete global.EnviConfig.commanderOptions // do not print commanderOptions to console
console.log('- Final Configuration: ', global.EnviConfig)
console.log('######## Completed System Configuration ########') console.log('######## Completed Environment Configuration ########')
return global.EnviConfig return global.EnviConfig
}, },
getDynamicConfig: function (dynamicConfigFile='ConfigDynamic.js') { // dynamicConfigFile should be absolute or relative to the node process's dir. getDynamicConfig(dynamicConfigFile='ConfigDynamic.js') { // dynamicConfigFile should be absolute or relative to the node process's dir.
const fullpath = path.join(process.cwd(), dynamicConfigFile) const fullpath = path.join(process.cwd(), dynamicConfigFile)
if (fs.existsSync(fullpath)) { if (fs.existsSync(fullpath)) {
delete require.cache[require.resolve(fullpath)] // delete require.cache[fullpath] 不起作用 delete require.cache[require.resolve(fullpath)] // delete require.cache[fullpath] 不起作用
@ -76,4 +81,16 @@ module.exports = {
return {} return {}
} }
}, },
showEnvi() {
console.log('{')
for (let key in global.EnviConfig) {
if (my.secretKeys.hasOwnProperty(key)) {
console.log(` ${key} : ******,`)
} else {
console.log(` ${key} : `, global.EnviConfig[key], ',')
}
}
console.log('}')
}
} }