补充英文的输出信息
This commit is contained in:
parent
38d2c0c26b
commit
1f47f25c8b
40
index.js
40
index.js
@ -4,9 +4,9 @@ const commander = require('commander')
|
|||||||
const deepmerge = require('deepmerge')
|
const deepmerge = require('deepmerge')
|
||||||
|
|
||||||
module.exports = (function () {
|
module.exports = (function () {
|
||||||
if (!global.Config) {
|
if (!global.SysConfig) {
|
||||||
// 不知为何,必须定义成全局变量,才能保证多次require只执行一次。
|
// 不知为何,必须定义成全局变量,才能保证多次require只执行一次。
|
||||||
console.info('★★★★★★★★ 配置参数:依次载入系统配置、用户配置、命令行参数 ★★★★★★★★')
|
console.info('★★★★★★★★ Starting System Configuration (开始系统配置:依次载入基础配置、用户配置、机密配置、命令行参数) ★★★★★★★★')
|
||||||
|
|
||||||
// 配置参数(按优先级从低到高):
|
// 配置参数(按优先级从低到高):
|
||||||
// ConfigBasic: 系统常量(全大写) 以及 默认参数(小写开头驼峰式)
|
// ConfigBasic: 系统常量(全大写) 以及 默认参数(小写开头驼峰式)
|
||||||
@ -14,50 +14,52 @@ module.exports = (function () {
|
|||||||
// ConfigSecret: 机密参数,例如哈希盐,webtoken密钥,等等。本文件绝对不能纳入版本管理。
|
// ConfigSecret: 机密参数,例如哈希盐,webtoken密钥,等等。本文件绝对不能纳入版本管理。
|
||||||
// 命令行参数
|
// 命令行参数
|
||||||
|
|
||||||
console.info(' -- 读取配置文件')
|
console.info(' -- 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(), './ConfigBasic.js')))) {
|
||||||
global.Config = deepmerge({}, require(configFile))
|
global.SysConfig = deepmerge({}, require(configFile))
|
||||||
console.info(`${configFile} loaded`)
|
console.info(`${configFile} loaded`)
|
||||||
} else {
|
} else {
|
||||||
console.info(` Missing and omitting ${configFile}`)
|
console.info(` Missing and omitting ${configFile}`)
|
||||||
}
|
}
|
||||||
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigCustom.js')))) {
|
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigCustom.js')))) {
|
||||||
// 如果存在,覆盖掉 ConfigBasic 里的默认参数
|
// 如果存在,覆盖掉 ConfigBasic 里的默认参数
|
||||||
global.Config = deepmerge(global.Config, require(configFile)) // 注意,objectMerge后,产生了一个新的对象,而不是在原来的Config里添加
|
global.SysConfig = deepmerge(global.SysConfig, require(configFile)) // 注意,objectMerge后,产生了一个新的对象,而不是在原来的Config里添加
|
||||||
console.info(`${configFile} loaded`)
|
console.info(`${configFile} loaded`)
|
||||||
} else {
|
} else {
|
||||||
console.info(` Missing and omitting ${configFile}`)
|
console.info(` Missing and omitting ${configFile}`)
|
||||||
}
|
}
|
||||||
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigSecret.js')))) {
|
if (fs.existsSync((configFile = path.join(process.cwd(), './ConfigSecret.js')))) {
|
||||||
// 如果存在,覆盖掉 ConfigBasic 和 ConfigCustom 里的参数
|
// 如果存在,覆盖掉 ConfigBasic 和 ConfigCustom 里的参数
|
||||||
global.Config = deepmerge(global.Config, require(configFile))
|
global.SysConfig = deepmerge(global.SysConfig, require(configFile))
|
||||||
console.info(`${configFile} loaded`)
|
console.info(`${configFile} loaded`)
|
||||||
} else {
|
} else {
|
||||||
console.info(` Missing and omitting ${configFile}`)
|
console.info(` Missing and omitting ${configFile}`)
|
||||||
}
|
}
|
||||||
global.Config = global.Config || {}
|
global.SysConfig = global.SysConfig || {}
|
||||||
|
|
||||||
console.log(' -- 载入命令行参数')
|
console.log(' -- Loading Command Line Parameters (载入命令行参数)')
|
||||||
commander.version(global.Config.VERSION || '0.0.1', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
|
commander.version(global.SysConfig.VERSION || '0.0.1', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
|
||||||
for (let [key, param, desc] of global.Config.commanderOptions || []) {
|
for (let [key, param, desc] of global.SysConfig.commanderOptions || []) {
|
||||||
commander.option(param, `${desc} Default = "${global.Config[key]}"`)
|
commander.option(param, `${desc} Default = "${global.SysConfig[key]}"`)
|
||||||
}
|
}
|
||||||
commander.parse(process.argv).parse(process.argv)
|
commander.parse(process.argv).parse(process.argv)
|
||||||
|
|
||||||
console.log(' -- 如果是生产环境,加载生产配置')
|
global.SysConfig.env = commander.env || global.SysConfig.env || process.env.NODE_ENV
|
||||||
global.Config.env = commander.env || global.Config.env || process.env.NODE_ENV
|
if (global.SysConfig.env === 'production' && global.SysConfig.production) {
|
||||||
if (global.Config.env === 'production' && global.Config.production) {
|
console.log(' -- Loading Production Configuration (加载生产环境配置)')
|
||||||
global.Config = deepmerge(global.Config, global.Config.production)
|
global.SysConfig = deepmerge(global.SysConfig, global.SysConfig.production)
|
||||||
}
|
}
|
||||||
delete global.Config.production
|
delete global.SysConfig.production
|
||||||
|
|
||||||
console.log(' -- 把命令行参数合并入配置')
|
console.log(' -- Merging Command Line Parameters into Configuration (把命令行参数合并入配置)')
|
||||||
for (let key in commander) {
|
for (let key in commander) {
|
||||||
if (typeof commander[key] === 'string' && !/^_/.test(key)) {
|
if (typeof commander[key] === 'string' && !/^_/.test(key)) {
|
||||||
global.Config[key] = commander[key] || global.Config[key]
|
global.SysConfig[key] = commander[key] || global.SysConfig[key]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return global.Config
|
console.log('Final Configuration: ', global.SysConfig)
|
||||||
|
console.log('######## Completed System Configuration ########')
|
||||||
|
return global.SysConfig
|
||||||
})()
|
})()
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "sysconfig",
|
"name": "so.sysconfig",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
Loading…
Reference in New Issue
Block a user