rename base.enviconfig to base-envar-config
This commit is contained in:
parent
f37c431c62
commit
454e106826
26
index.js
26
index.js
@ -4,7 +4,10 @@ const commander = require('commander')
|
||||
const deepmerge = require('deepmerge')
|
||||
|
||||
module.exports = {
|
||||
mergeEnvar({rawEnvar={}, envarFiles=['./envar-base-basic.js', './envar-base-custom.js', './envar-base-secret.js'], hasCommander=true} = {}) {
|
||||
/** 合并 envar config files 和 command line parameters 中的环境变量。
|
||||
* @param envarFiles: 存放 环境变量 的 文件列表。应当 按顺序导入,后面文件里的变量 覆盖前面的。
|
||||
*/
|
||||
merge_envar ({ rawEnvar = {}, envarFiles = ['./envar-base-basic.js', './envar-base-custom.js', './envar-base-secret.js'], hasCommander = true } = {}) {
|
||||
if (!global.envar) {
|
||||
global.envar = rawEnvar
|
||||
// 不知为何,必须定义成全局变量,才能保证多次require只执行一次。
|
||||
@ -13,7 +16,7 @@ module.exports = {
|
||||
|
||||
console.info('- Loading Configuration Files (读取配置文件)')
|
||||
|
||||
for (let configFile of 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.`)
|
||||
@ -22,7 +25,11 @@ module.exports = {
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
if (global.envar.prodev === 'production' && global.envar.ENV_PRODUCTION) {
|
||||
console.info('- Applying Production Configuration (加载生产环境配置)')
|
||||
global.envar = deepmerge(global.envar, global.envar.ENV_PRODUCTION) // 注意,objectMerge后,产生了一个新的对象,而不是在原来的Config里添加
|
||||
@ -40,7 +47,8 @@ module.exports = {
|
||||
|
||||
console.log('- Merging Command Line Parameters into Configuration (把命令行参数值合并入配置)')
|
||||
for (let key in commander) {
|
||||
if (!/^_/.test(key) && typeof commander[key] === 'string') { // commander 自带了一批 _开头的属性,过滤掉
|
||||
if (!/^_/.test(key) && typeof commander[key] === 'string') {
|
||||
// commander 自带了一批 _开头的属性,过滤掉
|
||||
global.envar[key] = commander[key]
|
||||
}
|
||||
}
|
||||
@ -51,7 +59,10 @@ module.exports = {
|
||||
return global.envar
|
||||
},
|
||||
|
||||
getDynamicEnvar({dynamicEnvarFile='envar-base-dynamic.js'}={}) { // dynamicEnvarFile should be absolute or relative to the node process's dir.
|
||||
/* 读取动态配置文件中的环境变量。
|
||||
*/
|
||||
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] 不起作用
|
||||
@ -61,7 +72,10 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
maskSecret({secretEnvarFile='./envar-base-secret.js'}={}) {
|
||||
/* 隐藏机密配置文件中的环境变量。
|
||||
* 需要输出当前环境变量时,必须调用本函数,避免机密信息被输出。
|
||||
*/
|
||||
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))
|
||||
@ -70,5 +84,5 @@ module.exports = {
|
||||
}
|
||||
}
|
||||
return envar
|
||||
}
|
||||
},
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
{
|
||||
"name": "base.enviconfig",
|
||||
"name": "base-envar-config",
|
||||
"description": "环境变量配置",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
|
Loading…
Reference in New Issue
Block a user