use wo.ccxxx instead of console.xxx; accept _clientInfo in get_dynamic_envar()
This commit is contained in:
parent
c27047b348
commit
b7b8355ce1
@ -15,7 +15,7 @@ module.exports = {
|
||||
start_watching ({ envarFiles = ['./envar-config.js', './envar-config.gitignore.js'], rawEnvar = {}, interval = 1000 } = {}) {
|
||||
chokidar.watch(envarFiles, { interval }).on('change', (onpath) => {
|
||||
// .on('all', (event, onpath)) 但这时,即使server刚启动,也会调用到这里一次
|
||||
console.log('envarTool.start_watching: envar file changed:', onpath)
|
||||
globalThis.wo?.cclog?.('envarTool.start_watching: envar file changed:', onpath)
|
||||
try {
|
||||
// 或者调用 get_dynamic_envar。
|
||||
delete require.cache[require.resolve(path.resolve(onpath))]
|
||||
@ -24,10 +24,10 @@ module.exports = {
|
||||
// 注意,assign_deep 会用 newEnvar 里的 undefined 属性覆盖掉 rawEnvar 里的原属性!所以要注意保持一致。(这和发送到前端的不一样,发送到前端的会被 JSON.stringify 以及 express 过滤掉 undefined.)
|
||||
// 目前的解决方案里,已通过 base2app 参数来确保只在发送给前端时才设置 undefined,但为防万一,在这里通过 JSON.stringify 确保删除了 undefined
|
||||
assign_deep(rawEnvar, JSON.parse(JSON.stringify(newEnvar)))
|
||||
console.log(`envarTool.start_watching: OK reload ${onpath}`)
|
||||
globalThis.wo?.cclog?.(`envarTool.start_watching: OK reload ${onpath}`)
|
||||
globalthis.wo?.eventCenter?.emit?.('ENVAR_UPDATED', { envarFile: onpath })
|
||||
} catch (expt) {
|
||||
console.log(`envarTool.start_watching: Fail reload ${onpath}`)
|
||||
globalThis.wo?.cclog?.(`envarTool.start_watching: Fail reload ${onpath}`)
|
||||
}
|
||||
})
|
||||
},
|
||||
@ -39,15 +39,15 @@ module.exports = {
|
||||
* - 对象: 直接添加到 rawEnvar 上。
|
||||
*/
|
||||
merge_envar ({ rawEnvar = {}, envarFiles = ['./envar-config.js', './envar-config.gitignore.js'] } = {}) {
|
||||
console.info({ _at: new Date().toJSON(), _from: 'merge_envar', about: `<<<<<<<< Configuring [${process.env.NODE_ENV}] Environment <<<<<<<<` }, '\n,')
|
||||
globalThis.wo?.ccinfo?.({ _from: 'merge_envar', about: `<<<<<<<< Configuring [${process.env.NODE_ENV}] Environment <<<<<<<<` })
|
||||
|
||||
console.info({ _at: new Date().toJSON(), _from: 'merge_envar', about: '- Loading Configuration Files (读取配置文件)' }, '\n,')
|
||||
globalThis.wo?.ccinfo?.({ _from: 'merge_envar', about: '- Loading Configuration Files (读取配置文件)' })
|
||||
if (typeof envarFiles === 'string') {
|
||||
// 例如当输入参数为 envarFiles = 'envar-config.js' 里面应当 module.exports 一个数组
|
||||
if (fs.existsSync(path.resolve(envarFiles))) {
|
||||
envarFiles = require(path.resolve(envarFiles))
|
||||
} else {
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'merge_envar', about: ` - ${envarFiles} is missing.` }, '\n,')
|
||||
globalThis.wo?.ccwarn?.({ _from: 'merge_envar', about: ` - ${envarFiles} is missing.` })
|
||||
envarFiles = undefined
|
||||
}
|
||||
}
|
||||
@ -61,9 +61,9 @@ module.exports = {
|
||||
} else if (typeof fileContent === 'function') {
|
||||
assign_deep(rawEnvar, fileContent())
|
||||
}
|
||||
console.info({ _at: new Date().toJSON(), _from: 'merge_envar', about: ` - ${configFile} is loaded.` }, '\n,')
|
||||
globalThis.wo?.ccinfo?.({ _from: 'merge_envar', about: ` - ${configFile} is loaded.` })
|
||||
} else {
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'merge_envar', about: ` - ${configFile} is missing.` }, '\n,')
|
||||
globalThis.wo?.ccwarn?.({ _from: 'merge_envar', about: ` - ${configFile} is missing.` })
|
||||
}
|
||||
}
|
||||
} else if (typeof envarFiles === 'object') {
|
||||
@ -71,18 +71,18 @@ module.exports = {
|
||||
} else if (typeof envarFiles === 'function') {
|
||||
assign_deep(rawEnvar, envarFiles())
|
||||
} else {
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'merge_envar', about: ` - unrecognized envarFiles!` }, '\n,')
|
||||
globalThis.wo?.ccwarn?.({ _from: 'merge_envar', about: ` - unrecognized envarFiles!` })
|
||||
}
|
||||
|
||||
if (process.argv.length > 2 && Array.isArray(rawEnvar.commanderOptions)) {
|
||||
console.info({ _at: new Date().toJSON(), _from: 'merge_envar', about: '- Loading Command Line Parameters (载入命令行参数)' }, '\n,')
|
||||
globalThis.wo?.ccinfo?.({ _from: 'merge_envar', about: '- Loading Command Line Parameters (载入命令行参数)' })
|
||||
const commander = require('commander')
|
||||
commander.version(rawEnvar.Base_Version || '0.0.1', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
|
||||
for (let [key, param, desc] of rawEnvar.commanderOptions || []) {
|
||||
commander.option(param, `${desc} Default = "${rawEnvar[key]}"`)
|
||||
}
|
||||
commander.parse(process.argv)
|
||||
// console.log({_at:new Date().toJSON(),_from:'merge_envar', about: '- Merging Command Line Parameters into Configuration (把命令行参数值合并入配置)' },'\n,')
|
||||
// globalThis.wo?.cclog?.({_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 自带了一批 _开头的属性,过滤掉
|
||||
@ -92,17 +92,14 @@ module.exports = {
|
||||
delete rawEnvar.commanderOptions
|
||||
}
|
||||
|
||||
console.log(
|
||||
{ _at: new Date().toJSON(), _from: 'merge_envar', about: `>>>>>>>> Configured [${process.env.NODE_ENV || 'development'}] Environment >>>>>>>>` },
|
||||
'\n,'
|
||||
)
|
||||
globalThis.wo?.cclog?.({ _from: 'merge_envar', about: `>>>>>>>> Configured [${process.env.NODE_ENV || 'development'}] Environment >>>>>>>>` }, '\n,')
|
||||
|
||||
return rawEnvar
|
||||
},
|
||||
|
||||
/* 读取动态配置文件中的环境变量。
|
||||
*/
|
||||
get_dynamic_envar ({ dynamicEnvarFiles = ['./envar-config.js', './envar-config.gitignore.js'], base2app } = {}) {
|
||||
get_dynamic_envar ({ dynamicEnvarFiles = ['./envar-config.js', './envar-config.gitignore.js'], base2app, _clientInfo } = {}) {
|
||||
// config file should be absolute or relative to the node process's dir.
|
||||
|
||||
let dynamicEnvar = {}
|
||||
@ -112,7 +109,7 @@ module.exports = {
|
||||
if (fs.existsSync(path.resolve(dynamicEnvarFiles))) {
|
||||
dynamicEnvarFiles = require(path.resolve(dynamicEnvarFiles))
|
||||
} else {
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'get_dynamic_envar', about: ` - ${dynamicEnvarFiles} is missing.` }, '\n,')
|
||||
globalThis.wo?.ccwarn?.({ _from: 'get_dynamic_envar', about: ` - ${dynamicEnvarFiles} is missing.` })
|
||||
dynamicEnvarFiles = undefined
|
||||
}
|
||||
}
|
||||
@ -125,11 +122,11 @@ module.exports = {
|
||||
if (typeof fileContent === 'object') {
|
||||
assign_deep(dynamicEnvar, fileContent)
|
||||
} else if (typeof fileContent === 'function') {
|
||||
assign_deep(dynamicEnvar, fileContent({ base2app }))
|
||||
assign_deep(dynamicEnvar, fileContent({ base2app, _clientInfo }))
|
||||
}
|
||||
console.info({ _at: new Date().toJSON(), _from: 'get_dynamic_envar', about: ` - ${dynamicFile} is parsed.` }, '\n,')
|
||||
globalThis.wo?.ccinfo?.({ _from: 'get_dynamic_envar', about: ` - ${dynamicFile} is parsed.` })
|
||||
} else {
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'get_dynamic_envar', about: ` - ${dynamicFile} is missing.` }, '\n,')
|
||||
globalThis.wo?.ccwarn?.({ _from: 'get_dynamic_envar', about: ` - ${dynamicFile} is missing.` })
|
||||
}
|
||||
}
|
||||
} else if (typeof dynamicEnvarFiles === 'object') {
|
||||
@ -137,7 +134,7 @@ module.exports = {
|
||||
} else if (typeof dynamicEnvarFiles === 'function') {
|
||||
dynamicEnvar = dynamicEnvarFiles()
|
||||
} else {
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'get_dynamic_envar', about: ` - unrecognized dynamicEnvarFiles!` }, '\n,')
|
||||
globalThis.wo?.ccwarn?.({ _from: 'get_dynamic_envar', about: ` - unrecognized dynamicEnvarFiles!` })
|
||||
}
|
||||
|
||||
return dynamicEnvar
|
||||
@ -155,12 +152,12 @@ module.exports = {
|
||||
if (fs.existsSync(path.resolve(secretEnvarFiles))) {
|
||||
secretEnvarFiles = require(path.resolve(secretEnvarFiles))
|
||||
} else {
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'mask_secret_envar', about: ` - ${secretEnvarFiles} is missing.` }, '\n,')
|
||||
globalThis.wo?.ccwarn?.({ _from: 'mask_secret_envar', about: ` - ${secretEnvarFiles} is missing.` })
|
||||
secretEnvarFiles = undefined
|
||||
}
|
||||
}
|
||||
|
||||
console.info({ _at: new Date().toJSON(), _from: 'mask_secret_envar', about: ` - Parsing secretEnvarFiles...` }, '\n,')
|
||||
globalThis.wo?.ccinfo?.({ _from: 'mask_secret_envar', about: ` - Parsing secretEnvarFiles...` })
|
||||
if (Array.isArray(secretEnvarFiles)) {
|
||||
for (let secretFile of secretEnvarFiles) {
|
||||
if (fs.existsSync(path.resolve(secretFile))) {
|
||||
@ -170,9 +167,9 @@ module.exports = {
|
||||
} else if (typeof fileContent === 'function') {
|
||||
assign_deep(secretEnvar, fileContent())
|
||||
}
|
||||
console.info({ _at: new Date().toJSON(), _from: 'mask_secret_envar', about: ` - ${secretFile} is parsed.` }, '\n,')
|
||||
globalThis.wo?.ccinfo?.({ _from: 'mask_secret_envar', about: ` - ${secretFile} is parsed.` })
|
||||
} else {
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'mask_secret_envar', about: ` - ${secretFile} is missing.` }, '\n,')
|
||||
globalThis.wo?.ccwarn?.({ _from: 'mask_secret_envar', about: ` - ${secretFile} is missing.` })
|
||||
}
|
||||
}
|
||||
} else if (typeof secretEnvarFiles === 'object') {
|
||||
@ -180,7 +177,7 @@ module.exports = {
|
||||
} else if (typeof secretEnvarFiles === 'function') {
|
||||
dynamicEnvar = secretEnvarFiles()
|
||||
} else {
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'mask_secret_envar', about: ` - unrecognized secretEnvarFiles!` }, '\n,')
|
||||
globalThis.wo?.ccwarn?.({ _from: 'mask_secret_envar', about: ` - unrecognized secretEnvarFiles!` })
|
||||
}
|
||||
|
||||
for (let key in secretEnvar) {
|
||||
@ -194,14 +191,14 @@ module.exports = {
|
||||
envar_all ({ files = ['./envar-config.js', './envar-config.gitignore.js', './envar-secret.js', './envar-secret.gitignore.js'] } = {}) {
|
||||
let envar = this.get_envar({ inProcess: false, refresh: true, files })
|
||||
if (process.argv.length > 2 && Array.isArray(envar.commanderOptions)) {
|
||||
console.info({ _at: new Date().toJSON(), _from: 'envar_all', about: '- Loading Command Line Parameters (载入命令行参数)' }, '\n,')
|
||||
globalThis.wo?.ccinfo?.({ _from: 'envar_all', about: '- Loading Command Line Parameters (载入命令行参数)' })
|
||||
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({_at:new Date().toJSON(),_from:'envar_all', about: '- Merging Command Line Parameters into Configuration (把命令行参数值合并入配置)' },'\n,')
|
||||
// globalThis.wo?.cclog?.({_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 自带了一批 _开头的属性,过滤掉
|
||||
@ -239,7 +236,7 @@ module.exports = {
|
||||
if (fs.existsSync(path.resolve(files))) {
|
||||
files = require(path.resolve(files))
|
||||
} else {
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'merge_envar_files', about: ` - ${files} is missing.` }, '\n,')
|
||||
globalThis.wo?.ccwarn?.({ _from: 'merge_envar_files', about: ` - ${files} is missing.` })
|
||||
files = undefined
|
||||
}
|
||||
}
|
||||
@ -251,15 +248,15 @@ module.exports = {
|
||||
delete require.cache[require.resolve(path.resolve(envarFile))] // delete require.cache[fullpath] 不起作用,必须要加 require.resolve
|
||||
}
|
||||
assign_deep(envar, require(path.resolve(envarFile)))
|
||||
console.info({ _at: new Date().toJSON(), _from: 'merge_envar_files', about: ` - ${envarFile} is parsed.` }, '\n,')
|
||||
globalThis.wo?.ccinfo?.({ _from: 'merge_envar_files', about: ` - ${envarFile} is parsed.` })
|
||||
} else {
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'merge_envar_files', about: ` - ${envarFile} is missing.` }, '\n,')
|
||||
globalThis.wo?.ccwarn?.({ _from: 'merge_envar_files', about: ` - ${envarFile} is missing.` })
|
||||
}
|
||||
}
|
||||
} else if (typeof files === 'object') {
|
||||
envar = files
|
||||
} else {
|
||||
console.warn({ _at: new Date().toJSON(), _from: 'merge_envar_files', about: ` - unrecognized files!` }, '\n,')
|
||||
globalThis.wo?.ccwarn?.({ _from: 'merge_envar_files', about: ` - unrecognized files!` })
|
||||
}
|
||||
|
||||
return envar
|
||||
|
Loading…
Reference in New Issue
Block a user