console.xxx({_at,_from,...})
This commit is contained in:
parent
c4f591367d
commit
424e363b60
@ -25,15 +25,15 @@ module.exports = {
|
||||
if (!global.envar) {
|
||||
global.envar = rawEnvar // 不知为何,必须定义成全局变量,才能保证多次require只执行一次。
|
||||
|
||||
console.info(`<<<<<<<< Configuring [${process.env.NODE_ENV}] Environment <<<<<<<<`)
|
||||
console.info({ _at: new Date().toJSON(), _from: 'merge_envar', about: `<<<<<<<< Configuring [${process.env.NODE_ENV}] Environment <<<<<<<<` }, '\n,')
|
||||
|
||||
console.info('- Loading Configuration Files (读取配置文件)')
|
||||
console.info({ _at: new Date().toJSON(), _from: 'merge_envar', about: '- Loading Configuration Files (读取配置文件)' }, '\n,')
|
||||
if (typeof envarFiles === 'string') {
|
||||
// 例如当输入参数为 envarFiles = 'envar-base.js' 里面应当 module.exports 一个数组
|
||||
if (fs.existsSync(path.resolve(envarFiles))) {
|
||||
envarFiles = require(path.resolve(envarFiles))
|
||||
} else {
|
||||
console.warn(` - ${envarFiles} is missing.`)
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'merge_envar', about: ` - ${envarFiles} is missing.` }, '\n,')
|
||||
envarFiles = undefined
|
||||
}
|
||||
}
|
||||
@ -42,26 +42,26 @@ module.exports = {
|
||||
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.`)
|
||||
console.info({ _at: new Date().toJSON(), _from: 'merge_envar', about: ` - ${configFile} is loaded.` }, '\n,')
|
||||
} else {
|
||||
console.warn(` - ${configFile} is missing.`)
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'merge_envar', about: ` - ${configFile} is missing.` }, '\n,')
|
||||
}
|
||||
}
|
||||
} else if (typeof envarFiles === 'object') {
|
||||
global.envar = deepmerge(global.envar, envarFiles)
|
||||
} else {
|
||||
console.warn(` - unrecognized envarFiles!`)
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'merge_envar', about: ` - unrecognized envarFiles!` }, '\n,')
|
||||
}
|
||||
|
||||
if (process.argv.length > 2 && Array.isArray(global.envar.commanderOptions)) {
|
||||
console.info('- Loading Command Line Parameters (载入命令行参数)')
|
||||
console.info({ _at: new Date().toJSON(), _from: 'merge_envar', about: '- Loading Command Line Parameters (载入命令行参数)' }, '\n,')
|
||||
const commander = require('commander')
|
||||
commander.version(global.envar.Base_Version || '0.0.1', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
|
||||
for (let [key, param, desc] of global.envar.commanderOptions || []) {
|
||||
commander.option(param, `${desc} Default = "${global.envar[key]}"`)
|
||||
}
|
||||
commander.parse(process.argv)
|
||||
// console.log('- Merging Command Line Parameters into Configuration (把命令行参数值合并入配置)')
|
||||
// console.log({_at:new Date().toJSON(),_from:'merge_envar', about: '- Merging Command Line Parameters into Configuration (把命令行参数值合并入配置)' },'\n,')
|
||||
for (let key in commander) {
|
||||
if (!/^_/.test(key) && typeof commander[key] === 'string') {
|
||||
// commander 自带了一批 _开头的属性,过滤掉
|
||||
@ -71,7 +71,7 @@ module.exports = {
|
||||
delete global.envar.commanderOptions
|
||||
}
|
||||
|
||||
console.log(`>>>>>>>> Configured [${process.env.NODE_ENV}] Environment >>>>>>>>`)
|
||||
console.log({ _at: new Date().toJSON(), _from: 'merge_envar', about: `>>>>>>>> Configured [${process.env.NODE_ENV}] Environment >>>>>>>>` }, '\n,')
|
||||
}
|
||||
|
||||
return global.envar
|
||||
@ -89,7 +89,7 @@ module.exports = {
|
||||
if (fs.existsSync(path.resolve(dynamicEnvarFiles))) {
|
||||
dynamicEnvarFiles = require(path.resolve(dynamicEnvarFiles))
|
||||
} else {
|
||||
console.warn(` - ${dynamicEnvarFiles} is missing.`)
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'get_dynamic_envar', about: ` - ${dynamicEnvarFiles} is missing.` }, '\n,')
|
||||
dynamicEnvarFiles = undefined
|
||||
}
|
||||
}
|
||||
@ -99,15 +99,15 @@ module.exports = {
|
||||
if (fs.existsSync(path.resolve(dynamicFile))) {
|
||||
delete require.cache[require.resolve(path.resolve(dynamicFile))] // delete require.cache[fullpath] 不起作用,必须要加 require.resolve
|
||||
dynamicEnvar = deepmerge(dynamicEnvar, require(path.resolve(dynamicFile))) // 在这里其实不需要 deepmerge
|
||||
console.info(` - ${dynamicFile} is parsed.`)
|
||||
console.info({ _at: new Date().toJSON(), _from: 'get_dynamic_envar', about: ` - ${dynamicFile} is parsed.` }, '\n,')
|
||||
} else {
|
||||
console.warn(` - ${dynamicFile} is missing.`)
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'get_dynamic_envar', about: ` - ${dynamicFile} is missing.` }, '\n,')
|
||||
}
|
||||
}
|
||||
} else if (typeof dynamicEnvarFiles === 'object') {
|
||||
dynamicEnvar = dynamicEnvarFiles
|
||||
} else {
|
||||
console.warn(` - unrecognized dynamicEnvarFiles!`)
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'get_dynamic_envar', about: ` - unrecognized dynamicEnvarFiles!` }, '\n,')
|
||||
}
|
||||
|
||||
return dynamicEnvar
|
||||
@ -125,25 +125,25 @@ module.exports = {
|
||||
if (fs.existsSync(path.resolve(secretEnvarFiles))) {
|
||||
secretEnvarFiles = require(path.resolve(secretEnvarFiles))
|
||||
} else {
|
||||
console.warn(` - ${secretEnvarFiles} is missing.`)
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'mask_secret_envar', about: ` - ${secretEnvarFiles} is missing.` }, '\n,')
|
||||
secretEnvarFiles = undefined
|
||||
}
|
||||
}
|
||||
|
||||
console.info(` - Parsing secretEnvarFiles...`)
|
||||
console.info({ _at: new Date().toJSON(), _from: 'mask_secret_envar', about: ` - Parsing secretEnvarFiles...` }, '\n,')
|
||||
if (Array.isArray(secretEnvarFiles)) {
|
||||
for (let secretFile of secretEnvarFiles) {
|
||||
if (fs.existsSync(path.resolve(secretFile))) {
|
||||
secretEnvar = deepmerge(secretEnvar, require(path.resolve(secretFile))) // 在这里其实不需要 deepmerge
|
||||
console.info(` - ${secretFile} is parsed.`)
|
||||
console.info({ _at: new Date().toJSON(), _from: 'mask_secret_envar', about: ` - ${secretFile} is parsed.` }, '\n,')
|
||||
} else {
|
||||
console.warn(` - ${secretFile} is missing.`)
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'mask_secret_envar', about: ` - ${secretFile} is missing.` }, '\n,')
|
||||
}
|
||||
}
|
||||
} else if (typeof secretEnvarFiles === 'object') {
|
||||
secretEnvar = secretEnvarFiles
|
||||
} else {
|
||||
console.warn(` - unrecognized secretEnvarFiles!`)
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'mask_secret_envar', about: ` - unrecognized secretEnvarFiles!` }, '\n,')
|
||||
}
|
||||
|
||||
for (let key in secretEnvar) {
|
||||
@ -166,14 +166,14 @@ module.exports = {
|
||||
} = {}) {
|
||||
let envar = this.get_envar({ inProcess: false, refresh: true, files })
|
||||
if (process.argv.length > 2 && Array.isArray(envar.commanderOptions)) {
|
||||
console.info('- Loading Command Line Parameters (载入命令行参数)')
|
||||
console.info({ _at: new Date().toJSON(), _from: 'envar_all', about: '- Loading Command Line Parameters (载入命令行参数)' }, '\n,')
|
||||
const commander = require('commander')
|
||||
commander.version(envar.Base_Version || '0.0.1', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
|
||||
for (let [key, param, desc] of envar.commanderOptions || []) {
|
||||
commander.option(param, `${desc} Default = "${envar[key]}"`)
|
||||
}
|
||||
commander.parse(process.argv)
|
||||
// console.log('- Merging Command Line Parameters into Configuration (把命令行参数值合并入配置)')
|
||||
// console.log({_at:new Date().toJSON(),_from:'envar_all', about: '- Merging Command Line Parameters into Configuration (把命令行参数值合并入配置)' },'\n,')
|
||||
for (let key in commander) {
|
||||
if (!/^_/.test(key) && typeof commander[key] === 'string') {
|
||||
// commander 自带了一批 _开头的属性,过滤掉
|
||||
@ -211,7 +211,7 @@ module.exports = {
|
||||
if (fs.existsSync(path.resolve(files))) {
|
||||
files = require(path.resolve(files))
|
||||
} else {
|
||||
console.warn(` - ${files} is missing.`)
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'merge_envar_files', about: ` - ${files} is missing.` }, '\n,')
|
||||
files = undefined
|
||||
}
|
||||
}
|
||||
@ -223,15 +223,15 @@ module.exports = {
|
||||
delete require.cache[require.resolve(path.resolve(envarFile))] // delete require.cache[fullpath] 不起作用,必须要加 require.resolve
|
||||
}
|
||||
envar = deepmerge(envar, require(path.resolve(envarFile)))
|
||||
console.info(` - ${envarFile} is parsed.`)
|
||||
console.info({ _at: new Date().toJSON(), _from: 'merge_envar_files', about: ` - ${envarFile} is parsed.` }, '\n,')
|
||||
} else {
|
||||
console.warn(` - ${envarFile} is missing.`)
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'merge_envar_files', about: ` - ${envarFile} is missing.` }, '\n,')
|
||||
}
|
||||
}
|
||||
} else if (typeof files === 'object') {
|
||||
envar = files
|
||||
} else {
|
||||
console.warn(` - unrecognized files!`)
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'merge_envar_files', about: ` - unrecognized files!` }, '\n,')
|
||||
}
|
||||
|
||||
return envar
|
||||
|
Loading…
Reference in New Issue
Block a user