u
This commit is contained in:
parent
fa009491ac
commit
5f1a46b28e
45
index.js
45
index.js
@ -18,47 +18,50 @@ module.exports = {
|
||||
// ConfigSecret: 机密参数,例如哈希盐,webtoken密钥,等等。本文件绝对不能纳入版本管理。
|
||||
// 命令行参数
|
||||
|
||||
console.info('- Loading Configuration Files (读取配置文件)')
|
||||
console.info('(1) Loading Configuration Files (读取配置文件)')
|
||||
let configFile
|
||||
|
||||
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigBasic.js')))) {
|
||||
global.EnviConfig = deepmerge(global.EnviConfig, require(configFile))
|
||||
console.info(` - ${configFile} loaded`)
|
||||
console.info(` - ${configFile} is loaded.`)
|
||||
} else {
|
||||
console.warn(` - Missing and omitting ${configFile}`)
|
||||
console.warn(` - ${configFile} is missing.`)
|
||||
}
|
||||
|
||||
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigCustom.js')))) {
|
||||
// 如果存在,覆盖掉 ConfigBasic 里的默认参数
|
||||
global.EnviConfig = deepmerge(global.EnviConfig, require(configFile)) // 注意,objectMerge后,产生了一个新的对象,而不是在原来的Config里添加
|
||||
console.info(`${configFile} loaded`)
|
||||
console.info(`${configFile} is loaded.`)
|
||||
} else {
|
||||
console.warn(` - Missing and omitting ${configFile}`)
|
||||
console.warn(` - ${configFile} is missing.`)
|
||||
}
|
||||
|
||||
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigSecret.js')))) {
|
||||
// 如果存在,覆盖掉 ConfigBasic 和 ConfigCustom 里的参数
|
||||
let secretConfig = require(configFile)
|
||||
my.secretKeys = Object.keys(secretConfig)
|
||||
global.EnviConfig = deepmerge(global.EnviConfig, secretConfig)
|
||||
console.info(`${configFile} loaded`)
|
||||
console.info(` - ${configFile} is loaded.`)
|
||||
} else {
|
||||
console.warn(` - Missing and omitting ${configFile}`)
|
||||
console.warn(` - ${configFile} is missing.`)
|
||||
}
|
||||
global.EnviConfig = global.EnviConfig || {}
|
||||
|
||||
console.info('- Loading Command Line Parameters (载入命令行参数)')
|
||||
global.EnviConfig.prodev = commander.prodev || global.EnviConfig.prodev || process.env.NODE_ENV || 'development' // server = require('express')(); server.get('env') === server.settings.env === process.env.NODE_ENV
|
||||
if (global.EnviConfig.prodev === 'production' && global.EnviConfig.production) {
|
||||
console.info(' - Loading Production Configuration (加载生产环境配置)')
|
||||
global.EnviConfig = deepmerge(global.EnviConfig, global.EnviConfig.production)
|
||||
}
|
||||
delete global.EnviConfig.production
|
||||
|
||||
console.info('(2) Loading Command Line Parameters (载入命令行参数)')
|
||||
commander.version(global.EnviConfig.VERSION || '0.0.1', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
|
||||
for (let [key, param, desc] of global.EnviConfig.commanderOptions || []) {
|
||||
commander.option(param, `${desc} Default = "${global.EnviConfig[key]}"`)
|
||||
}
|
||||
commander.parse(process.argv)
|
||||
|
||||
global.EnviConfig.prodev = commander.prodev || global.EnviConfig.prodev || process.env.NODE_ENV || 'development' // server = require('express')(); server.get('env') === server.settings.env === process.env.NODE_ENV
|
||||
if (global.EnviConfig.prodev === 'production' && global.EnviConfig.production) {
|
||||
console.info('- Loading Production Configuration (加载生产环境配置)')
|
||||
global.EnviConfig = deepmerge(global.EnviConfig, global.EnviConfig.production)
|
||||
}
|
||||
delete global.EnviConfig.production
|
||||
|
||||
console.log('- Merging Command Line Parameters into Configuration (把命令行参数合并入配置)')
|
||||
console.log('(3) Merging Command Line Parameters into Configuration (把命令行参数合并入配置)')
|
||||
for (let key in commander) {
|
||||
if (typeof commander[key] === 'string' && !/^_/.test(key)) {
|
||||
global.EnviConfig[key] = commander[key] || global.EnviConfig[key]
|
||||
@ -83,14 +86,12 @@ module.exports = {
|
||||
},
|
||||
|
||||
showEnvi() {
|
||||
console.log('{')
|
||||
for (let key in global.EnviConfig) {
|
||||
let envi = JSON.parse(JSON.stringify(global.EnviConfig))
|
||||
for (let key in envi) {
|
||||
if (my.secretKeys.hasOwnProperty(key)) {
|
||||
console.log(` ${key} : ******,`)
|
||||
} else {
|
||||
console.log(` ${key} : `, global.EnviConfig[key], ',')
|
||||
envi[key] = '******'
|
||||
}
|
||||
}
|
||||
console.log('}')
|
||||
console.log(envi)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user