100 lines
4.8 KiB
JavaScript
100 lines
4.8 KiB
JavaScript
const fs = require('fs')
|
||
const path = require('path')
|
||
const commander = require('commander')
|
||
const deepmerge = require('deepmerge')
|
||
|
||
module.exports = {
|
||
/** 合并 envar files 和 commander parameters 中的环境变量。
|
||
* @param envarFiles:
|
||
* - 字符串: 导入文件,内容应当是字符串数组,或者对象。
|
||
* - 字符串数组: 按顺序导入导入每个文件,后面文件里的变量覆盖前面的。
|
||
* - 对象: 直接添加到 global.envar 上。
|
||
*/
|
||
merge_envar ({ rawEnvar = {}, envarFiles = ['./envar-base-basic.js', './envar-base-custom.js', './envar-base-secret.js'] } = {}) {
|
||
if (!global.envar) {
|
||
global.envar = rawEnvar // 不知为何,必须定义成全局变量,才能保证多次require只执行一次。
|
||
|
||
// process.env.NODE_ENV 不是天然就有的,而是由 cross-env 或 vue/uniapp 配置的。如果通过 vscode 的 launch.json 直接启动,就不存在 process.env.NODE_ENV。因此默认设置一个 prodev,默认值 development 因为在 launch.json 时显然是开发环境。
|
||
// 在 uniapp 里,在 main.js/App.vue 里都可以访问 process.env.NODE_ENV。在 页面.vue 的代码区域,可以访问,但在模版区域,不能访问。在浏览器中时,不能访问。
|
||
// 在 uniCloud 里,存在 process.env,但不存在 process.env.NODE_ENV
|
||
global.envar.prodev = global.envar.prodev || process.env.NODE_ENV || 'development' // server = require('express')(); server.get('env') === server.settings.env === process.env.NODE_ENV
|
||
global.inDev = global.envar.prodev !== 'production' // 为了方便从配置文件里,根据 inDev 来设置不同参数,例如 port: inDev ? 8080 : undefined // 用 production 测试,防止 NODE_ENV 未定义
|
||
console.info(`<<<<<<<< Configuring [${global.envar.prodev}] Environment <<<<<<<<`)
|
||
|
||
console.info('- Loading Configuration Files (读取配置文件)')
|
||
if (typeof envarFiles === 'string') {
|
||
// 例如当输入参数为 envarFiles = 'envar-base.js'
|
||
if (fs.existsSync(path.resolve(envarFiles))) {
|
||
envarFiles = require(path.resolve(envarFiles))
|
||
} else {
|
||
console.error(` - ${envarFiles} Not Found!`)
|
||
envarFiles = undefined
|
||
}
|
||
}
|
||
if (Array.isArray(envarFiles)) {
|
||
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.`)
|
||
} else {
|
||
console.warn(` - ${configFile} is missing.`)
|
||
}
|
||
}
|
||
} else if (typeof envarFiles === 'object') {
|
||
global.envar = deepmerge(global.envar, envarFiles)
|
||
} else {
|
||
console.warn(` - unrecognized envarFiles!`)
|
||
}
|
||
|
||
if (Array.isArray(global.envar.Commander_Option_List)) {
|
||
console.info('- Loading Command Line Parameters (载入命令行参数)')
|
||
commander.version(global.envar.Base_Version || '0.0.1', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
|
||
for (let [key, param, desc] of global.envar.Commander_Option_List || []) {
|
||
commander.option(param, `${desc} Default = "${global.envar[key]}"`)
|
||
}
|
||
commander.parse(process.argv)
|
||
delete global.envar.Commander_Option_List
|
||
|
||
console.log('- Merging Command Line Parameters into Configuration (把命令行参数值合并入配置)')
|
||
for (let key in commander) {
|
||
if (!/^_/.test(key) && typeof commander[key] === 'string') {
|
||
// commander 自带了一批 _开头的属性,过滤掉
|
||
global.envar[key] = commander[key]
|
||
}
|
||
}
|
||
}
|
||
|
||
console.log(`>>>>>>>> Configured [${global.envar.prodev}] Variables >>>>>>>>`)
|
||
}
|
||
|
||
return global.envar
|
||
},
|
||
|
||
/* 读取动态配置文件中的环境变量。
|
||
*/
|
||
get_dynamic_envar ({ dynamicEnvarFile = 'envar-base-dynamic.js' } = {}) {
|
||
// dynamicEnvarFile should be absolute or relative to the node process's dir.
|
||
const fullpath = path.resolve(dynamicEnvarFile)
|
||
if (fs.existsSync(fullpath)) {
|
||
delete require.cache[require.resolve(fullpath)] // delete require.cache[fullpath] 不起作用
|
||
return require(fullpath)
|
||
} else {
|
||
return {}
|
||
}
|
||
},
|
||
|
||
/* 隐藏机密配置文件中的环境变量。
|
||
* 需要输出当前环境变量时,必须调用本函数,避免机密信息被输出。
|
||
*/
|
||
mask_secret_envar ({ secretEnvarFile = './envar-base-secret.js' } = {}) {
|
||
let envar = JSON.parse(JSON.stringify(global.envar)) // 复制一份,避免污染
|
||
if (fs.existsSync(path.resolve(secretEnvarFile))) {
|
||
const secretEnvar = require(path.resolve(secretEnvarFile))
|
||
for (let key in secretEnvar) {
|
||
envar[key] = '****** confidential ******'
|
||
}
|
||
}
|
||
return envar
|
||
},
|
||
}
|