From 8b945b405dfcd90cb74d78f0cc307a02acb1f144 Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Sun, 24 Apr 2022 16:42:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BC=A0=E5=85=A5=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E5=90=8D=E4=BD=9C=E4=B8=BA=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 68 ++++++++++++++++++-------------------------------------- 1 file changed, 22 insertions(+), 46 deletions(-) diff --git a/index.js b/index.js index 3acd0da..e1f25d8 100644 --- a/index.js +++ b/index.js @@ -6,77 +6,53 @@ const deepmerge = require('deepmerge') const my = { secretKeys:[] } module.exports = { - mergeConfig(rawConfig) { + mergeConfig({rawConfig={}, envarFiles=['./envar-base-basic.js', './envar-base-custom.js', './envar-base-secret.js']} = {}) { if (!global.envar) { global.envar = rawConfig // 不知为何,必须定义成全局变量,才能保证多次require只执行一次。 - console.info('<<<<<<<< Merging Environment Configuration (依次载入基础配置、用户配置、机密配置、命令行参数) <<<<<<<<') - // 配置参数(按优先级从低到高): - // envar-base-basic.js: 系统常量(全大写) 以及 默认参数(小写开头驼峰式) - // envar-base-custom.js: 用户或应用自定义参数。本文件不应纳入版本管理。 - // envar-base-secret.js: 机密参数,例如哈希盐,webtoken密钥,等等。本文件绝对不能纳入版本管理。 - // 命令行参数 + console.info('<<<<<<<< Collecting Environment Configuration <<<<<<<<') - console.info('(1) Loading Configuration Files (读取配置文件)') - let configFile + console.info('- Loading Configuration Files (读取配置文件)') - if (fs.existsSync((configFile = path.join(process.cwd(), './envar-base-basic.js')))) { - global.envar = deepmerge(global.envar, require(configFile)) - console.info(` - ${configFile} is loaded.`) - } else { - console.warn(` - ${configFile} is missing.`) + 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.production) { - console.info(' - Loading Production Configuration (加载生产环境配置)') - global.envar = deepmerge(global.envar, global.envar.production) + console.info('- Applying Production Configuration (加载生产环境配置)') + global.envar = deepmerge(global.envar, global.envar.production) // 注意,objectMerge后,产生了一个新的对象,而不是在原来的Config里添加 delete global.envar.production } - if (fs.existsSync((configFile = path.join(process.cwd(), './envar-base-custom.js')))) { - // 如果存在,覆盖掉 envar-base-basic.js 里的默认参数 - 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(), './envar-base-secret.js')))) { - // 如果存在,覆盖掉 envar-base-basic.js 和 envar-base-custom.js 里的参数 - 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 || {} - - console.info('(2) Loading Command Line Parameters (载入命令行参数)') + 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('(3) Merging Command Line Parameters into Configuration (把命令行参数合并入配置)') + console.log('- 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] + if (!/^_/.test(key) && typeof commander[key] === 'string') { // commander 自带了一批 _开头的属性,过滤掉 + global.envar[key] = commander[key] } } } - delete global.envar.Commander_Option_List // do not print Commander_Option_List to console - - console.log('>>>>>>>> Merged Environment Configuration >>>>>>>>') + 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.join(process.cwd(), dynamicConfigFile) + const fullpath = path.resolve(dynamicConfigFile) if (fs.existsSync(fullpath)) { delete require.cache[require.resolve(fullpath)] // delete require.cache[fullpath] 不起作用 return require(fullpath) @@ -86,10 +62,10 @@ module.exports = { }, maskSecret() { - let envar = JSON.parse(JSON.stringify(global.envar)) + let envar = JSON.parse(JSON.stringify(global.envar)) // 复制一份,避免污染 for (let key in envar) { - if (my.secretKeys.includes(key)) { - envar[key] = '***confidential***' + if (/^SECRET_/.test(key)) { + envar[key] = '*** confidentical ***' } } return envar