修改一些名字;让 maskSecret 换用 secretEnvarFile 参数来过滤机密
This commit is contained in:
		
							parent
							
								
									d391472855
								
							
						
					
					
						commit
						f37c431c62
					
				
							
								
								
									
										47
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										47
									
								
								index.js
									
									
									
									
									
								
							@ -3,15 +3,13 @@ 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({rawConfig={}, envarFiles=['./envar-base-basic.js', './envar-base-custom.js', './envar-base-secret.js']} = {}) {
 | 
					  mergeEnvar({rawEnvar={}, envarFiles=['./envar-base-basic.js', './envar-base-custom.js', './envar-base-secret.js'], hasCommander=true} = {}) {
 | 
				
			||||||
    if (!global.envar) {
 | 
					    if (!global.envar) {
 | 
				
			||||||
      global.envar = rawConfig
 | 
					      global.envar = rawEnvar
 | 
				
			||||||
      // 不知为何,必须定义成全局变量,才能保证多次require只执行一次。
 | 
					      // 不知为何,必须定义成全局变量,才能保证多次require只执行一次。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      console.info('<<<<<<<< Collecting Environment Configuration <<<<<<<<')
 | 
					      console.info('<<<<<<<< Configuring Environment Variables <<<<<<<<')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      console.info('- Loading Configuration Files (读取配置文件)')
 | 
					      console.info('- Loading Configuration Files (读取配置文件)')
 | 
				
			||||||
      
 | 
					      
 | 
				
			||||||
@ -31,28 +29,30 @@ module.exports = {
 | 
				
			|||||||
        delete global.envar.ENV_PRODUCTION
 | 
					        delete global.envar.ENV_PRODUCTION
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      console.info('- Loading Command Line Parameters (载入命令行参数)')
 | 
					      if (hasCommander) {
 | 
				
			||||||
      commander.version(global.envar.Base_Version || '0.0.1', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
 | 
					        console.info('- Loading Command Line Parameters (载入命令行参数)')
 | 
				
			||||||
      for (let [key, param, desc] of global.envar.Commander_Option_List || []) {
 | 
					        commander.version(global.envar.Base_Version || '0.0.1', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
 | 
				
			||||||
        commander.option(param, `${desc} Default = "${global.envar[key]}"`)
 | 
					        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
 | 
					        commander.parse(process.argv)
 | 
				
			||||||
 | 
					        delete global.envar.Commander_Option_List
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      console.log('- Merging Command Line Parameters into Configuration (把命令行参数值合并入配置)')
 | 
					        console.log('- Merging Command Line Parameters into Configuration (把命令行参数值合并入配置)')
 | 
				
			||||||
      for (let key in commander) {
 | 
					        for (let key in commander) {
 | 
				
			||||||
        if (!/^_/.test(key) && typeof commander[key] === 'string') { // commander 自带了一批 _开头的属性,过滤掉
 | 
					          if (!/^_/.test(key) && typeof commander[key] === 'string') { // commander 自带了一批 _开头的属性,过滤掉
 | 
				
			||||||
          global.envar[key] = commander[key]
 | 
					            global.envar[key] = commander[key]
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    console.log('>>>>>>>> Collected Environment Configuration >>>>>>>>')
 | 
					    console.log('>>>>>>>> Configured Environment Variables >>>>>>>>')
 | 
				
			||||||
    return global.envar
 | 
					    return global.envar
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  getDynamicConfig(dynamicConfigFile='envar-base-dynamic.js') { // dynamicConfigFile should be absolute or relative to the node process's dir.
 | 
					  getDynamicEnvar({dynamicEnvarFile='envar-base-dynamic.js'}={}) { // dynamicEnvarFile should be absolute or relative to the node process's dir.
 | 
				
			||||||
    const fullpath = path.resolve(dynamicConfigFile)
 | 
					    const fullpath = path.resolve(dynamicEnvarFile)
 | 
				
			||||||
    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] 不起作用
 | 
				
			||||||
      return require(fullpath)
 | 
					      return require(fullpath)
 | 
				
			||||||
@ -61,11 +61,12 @@ module.exports = {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  maskSecret() {
 | 
					  maskSecret({secretEnvarFile='./envar-base-secret.js'}={}) {
 | 
				
			||||||
    let envar = JSON.parse(JSON.stringify(global.envar)) // 复制一份,避免污染
 | 
					    let envar = JSON.parse(JSON.stringify(global.envar)) // 复制一份,避免污染
 | 
				
			||||||
    for (let key in envar) {
 | 
					    if (fs.existsSync(path.resolve(secretEnvarFile))) {
 | 
				
			||||||
      if (/^SECRET_/.test(key)) {
 | 
					      const secretEnvar = require(path.resolve(secretEnvarFile))
 | 
				
			||||||
        envar[key] = '*** confidentical ***'
 | 
					      for (let key in secretEnvar) {
 | 
				
			||||||
 | 
					        envar[key] = '****** confidential ******'
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return envar
 | 
					    return envar
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user