init
This commit is contained in:
		
						commit
						162f1f2c8d
					
				
							
								
								
									
										60
									
								
								index.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								index.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,60 @@
 | 
				
			|||||||
 | 
					const fs = require('fs')
 | 
				
			||||||
 | 
					const path = require('path')
 | 
				
			||||||
 | 
					const commander = require('commander')
 | 
				
			||||||
 | 
					const deepmerge = require('deepmerge')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module.exports = (function(){
 | 
				
			||||||
 | 
					  if (!global.Config) { // 不知为何,必须定义成全局变量,才能保证多次require只执行一次。
 | 
				
			||||||
 | 
					    console.info('★★★★★★★★ 配置参数:依次载入系统配置、用户配置、命令行参数 ★★★★★★★★')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // 配置参数(按优先级从低到高):
 | 
				
			||||||
 | 
					    // ConfigBasic: 系统常量(全大写) 以及 默认参数(小写开头驼峰式)
 | 
				
			||||||
 | 
					    // ConfigCustom: 用户或应用自定义参数。本文件不应纳入版本管理。
 | 
				
			||||||
 | 
					    // ConfigSecret: 机密参数,例如哈希盐,webtoken密钥,等等。本文件绝对不能纳入版本管理。
 | 
				
			||||||
 | 
					    // 命令行参数
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    console.info("  -- 读取配置文件")
 | 
				
			||||||
 | 
					    let configFile
 | 
				
			||||||
 | 
					    if (fs.existsSync(configFile = path.join(process.cwd(), './ConfigBasic.js'))) {
 | 
				
			||||||
 | 
					      global.Config = deepmerge({}, require(configFile))
 | 
				
			||||||
 | 
					      console.info(`${configFile} loaded`)
 | 
				
			||||||
 | 
					    }else {
 | 
				
			||||||
 | 
					      console.info(`Missing and omitting ${configFile}`)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (fs.existsSync(configFile = path.join(process.cwd(), './ConfigCustom.js'))) { // 如果存在,覆盖掉 ConfigBasic 里的默认参数
 | 
				
			||||||
 | 
					      global.Config = deepmerge(global.Config, require(configFile)) // 注意,objectMerge后,产生了一个新的对象,而不是在原来的Config里添加
 | 
				
			||||||
 | 
					      console.info(`${configFile} loaded`)
 | 
				
			||||||
 | 
					    }else {
 | 
				
			||||||
 | 
					      console.info(`Missing and omitting ${configFile}`)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (fs.existsSync(configFile = path.join(process.cwd(), './ConfigSecret.js'))) { // 如果存在,覆盖掉 ConfigBasic 和 ConfigCustom 里的参数
 | 
				
			||||||
 | 
					      global.Config = deepmerge(global.Config, require(configFile))
 | 
				
			||||||
 | 
					      console.info(`${configFile} loaded`)
 | 
				
			||||||
 | 
					    }else {
 | 
				
			||||||
 | 
					      console.info(`Missing and omitting ${configFile}`)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    global.Config = global.Config || {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    console.log("  -- 载入命令行参数")
 | 
				
			||||||
 | 
					    commander.version(global.Config.VERSION || '0.0.1', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
 | 
				
			||||||
 | 
					    for (let [key, param, desc] of global.Config.commanderOptions||[]){
 | 
				
			||||||
 | 
					      commander.option(param, `${desc} Default = "${global.Config[key]}"`)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    commander.parse(process.argv).parse(process.argv)
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					    console.log("  -- 如果是生产环境,加载生产配置")
 | 
				
			||||||
 | 
					    global.Config.env = commander.env || global.Config.env || process.env.NODE_ENV
 | 
				
			||||||
 | 
					    if (global.Config.env === 'production' && global.Config.production) {
 | 
				
			||||||
 | 
					      global.Config = deepmerge(global.Config, global.Config.production)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    delete global.Config.production
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    console.log("  -- 把命令行参数合并入配置")
 | 
				
			||||||
 | 
					    for (let key in commander){
 | 
				
			||||||
 | 
					      if (typeof(commander[key])==='string' && !/^_/.test(key)) {
 | 
				
			||||||
 | 
					        global.Config[key] = commander[key] || global.Config[key]
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  return global.Config
 | 
				
			||||||
 | 
					})()
 | 
				
			||||||
							
								
								
									
										14
									
								
								package.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								package.json
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,14 @@
 | 
				
			|||||||
 | 
					{
 | 
				
			||||||
 | 
					  "name": "so.base",
 | 
				
			||||||
 | 
					  "version": "0.1.0",
 | 
				
			||||||
 | 
					  "private": true,
 | 
				
			||||||
 | 
					  "dependencies": {
 | 
				
			||||||
 | 
					    "commander": "^4.1.1",
 | 
				
			||||||
 | 
					    "deepmerge": "^4.2.2"
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					  "devDependencies": {},
 | 
				
			||||||
 | 
					  "scripts": {
 | 
				
			||||||
 | 
					    "setup": "npm install"
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					  "author": ""
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user