renaming Config*.js to envar-base-*.js

This commit is contained in:
陆柯 2022-04-23 16:03:59 +08:00
parent 44b89632d7
commit b8f677155c

View File

@ -13,31 +13,31 @@ module.exports = {
console.info('<<<<<<<< Merging Environment Configuration (依次载入基础配置、用户配置、机密配置、命令行参数) <<<<<<<<') console.info('<<<<<<<< Merging Environment Configuration (依次载入基础配置、用户配置、机密配置、命令行参数) <<<<<<<<')
// 配置参数(按优先级从低到高): // 配置参数(按优先级从低到高):
// ConfigBasic: 系统常量(全大写) 以及 默认参数(小写开头驼峰式) // envar-base-basic.js: 系统常量(全大写) 以及 默认参数(小写开头驼峰式)
// ConfigCustom: 用户或应用自定义参数。本文件不应纳入版本管理。 // envar-base-custom.js: 用户或应用自定义参数。本文件不应纳入版本管理。
// ConfigSecret: 机密参数例如哈希盐webtoken密钥等等。本文件绝对不能纳入版本管理。 // envar-base-secret.js: 机密参数例如哈希盐webtoken密钥等等。本文件绝对不能纳入版本管理。
// 命令行参数 // 命令行参数
console.info('(1) Loading Configuration Files (读取配置文件)') console.info('(1) Loading Configuration Files (读取配置文件)')
let configFile let configFile
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigBasic.js')))) { if (fs.existsSync((configFile = path.join(process.cwd(), './envar-base-basic.js')))) {
global.envar = deepmerge(global.envar, require(configFile)) global.envar = deepmerge(global.envar, require(configFile))
console.info(` - ${configFile} is loaded.`) console.info(` - ${configFile} is loaded.`)
} else { } else {
console.warn(` - ${configFile} is missing.`) console.warn(` - ${configFile} is missing.`)
} }
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigCustom.js')))) { if (fs.existsSync((configFile = path.join(process.cwd(), './envar-base-custom.js')))) {
// 如果存在,覆盖掉 ConfigBasic 里的默认参数 // 如果存在,覆盖掉 envar-base-basic.js 里的默认参数
global.envar = deepmerge(global.envar, require(configFile)) // 注意objectMerge后产生了一个新的对象而不是在原来的Config里添加 global.envar = deepmerge(global.envar, require(configFile)) // 注意objectMerge后产生了一个新的对象而不是在原来的Config里添加
console.info(`${configFile} is loaded.`) console.info(`${configFile} is loaded.`)
} else { } else {
console.warn(` - ${configFile} is missing.`) console.warn(` - ${configFile} is missing.`)
} }
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigSecret.js')))) { if (fs.existsSync((configFile = path.join(process.cwd(), './envar-base-secret.js')))) {
// 如果存在,覆盖掉 ConfigBasic 和 ConfigCustom 里的参数 // 如果存在,覆盖掉 envar-base-basic.js 和 envar-base-custom.js 里的参数
let secretConfig = require(configFile) let secretConfig = require(configFile)
my.secretKeys = Object.keys(secretConfig) my.secretKeys = Object.keys(secretConfig)
global.envar = deepmerge(global.envar, secretConfig) global.envar = deepmerge(global.envar, secretConfig)
@ -75,7 +75,7 @@ module.exports = {
return global.envar return global.envar
}, },
getDynamicConfig(dynamicConfigFile='ConfigDynamic.js') { // dynamicConfigFile should be absolute or relative to the node process's dir. getDynamicConfig(dynamicConfigFile='config-dynamic.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] 不起作用