dev-sidecar/packages/core/src/config.js

175 lines
4.3 KiB
JavaScript
Raw Normal View History

const fs = require('fs')
2020-10-27 13:14:12 +00:00
const Shell = require('./shell')
2020-10-24 13:22:44 +00:00
const lodash = require('lodash')
const defConfig = require('./config/index.js')
const JSON5 = require('json5').default
2020-12-29 16:14:33 +00:00
console.log('JSON5', JSON5)
2020-10-26 17:12:40 +00:00
let configTarget = lodash.cloneDeep(defConfig)
function get () {
return configTarget
}
2020-11-06 10:59:54 +00:00
function _deleteDisabledItem (target) {
lodash.forEach(target, (item, key) => {
if (item == null) {
delete target[key]
2020-10-25 13:04:28 +00:00
}
2020-11-06 10:59:54 +00:00
if (lodash.isObject(item)) {
_deleteDisabledItem(item)
}
})
2020-10-25 13:04:28 +00:00
}
const getDefaultConfigBasePath = function () {
return get().server.setting.userBasePath
}
function _getConfigPath () {
const dir = getDefaultConfigBasePath()
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
return dir + '/config.json5'
}
function doMerge (defObj, newObj) {
const defObj2 = { ...defObj }
const newObj2 = {}
for (const key in newObj) {
const newValue = newObj[key]
const defValue = defObj[key]
if (newValue != null && defValue == null) {
newObj2[key] = newValue
continue
}
if (lodash.isEqual(newValue, defValue)) {
delete defObj2[key]
continue
}
if (lodash.isArray(newValue)) {
delete defObj2[key]
newObj2[key] = newValue
continue
}
if (lodash.isObject(newValue)) {
newObj2[key] = doMerge(defValue, newValue)
delete defObj2[key]
continue
} else {
// 基础类型,直接覆盖
delete defObj2[key]
newObj2[key] = newValue
continue
}
}
// defObj 里面剩下的是被删掉的
lodash.forEach(defObj2, (defValue, key) => {
newObj2[key] = null
})
return newObj2
}
2020-10-27 13:14:12 +00:00
const configApi = {
/**
* 保存自定义的 config
* @param newConfig
*/
save (newConfig) {
// 对比默认config的异同
// configApi.set(newConfig)
const defConfig = configApi.getDefault()
const saveConfig = doMerge(defConfig, newConfig)
fs.writeFileSync(_getConfigPath(), JSON5.stringify(saveConfig, null, 2))
configApi.reload()
return saveConfig
},
doMerge,
/**
* 读取后合并配置
* @returns {*}
*/
reload () {
const path = _getConfigPath()
if (!fs.existsSync(path)) {
return configApi.get()
}
const file = fs.readFileSync(path)
const userConfig = JSON5.parse(file.toString())
configApi.set(userConfig)
const config = configApi.get()
return config || {}
2020-10-24 13:22:44 +00:00
},
get,
2020-10-24 13:22:44 +00:00
set (newConfig) {
2020-10-26 14:15:54 +00:00
if (newConfig == null) {
2020-10-25 13:04:28 +00:00
return
}
2020-10-26 17:12:40 +00:00
const merged = lodash.cloneDeep(newConfig)
2020-10-24 13:22:44 +00:00
const clone = lodash.cloneDeep(defConfig)
2021-03-24 17:00:45 +00:00
function customizer (objValue, srcValue) {
if (lodash.isArray(objValue)) {
return srcValue
}
}
lodash.mergeWith(merged, clone, customizer)
lodash.mergeWith(merged, newConfig, customizer)
2020-11-06 10:59:54 +00:00
_deleteDisabledItem(merged)
2020-10-26 17:12:40 +00:00
configTarget = merged
2020-10-24 13:22:44 +00:00
return configTarget
},
getDefault () {
2020-10-26 17:12:40 +00:00
return lodash.cloneDeep(defConfig)
2020-10-24 13:22:44 +00:00
},
2020-10-30 09:46:45 +00:00
addDefault (key, defValue) {
lodash.set(defConfig, key, defValue)
},
2020-11-06 10:59:54 +00:00
resetDefault (key) {
if (key) {
let value = lodash.get(defConfig, key)
value = lodash.cloneDeep(value)
lodash.set(configTarget, key, value)
} else {
configTarget = lodash.cloneDeep(defConfig)
}
return configTarget
2020-10-27 13:14:12 +00:00
},
2020-10-27 17:41:04 +00:00
async getVariables (type) {
const method = type === 'npm' ? Shell.getNpmEnv : Shell.getSystemEnv
const currentMap = await method()
2020-10-27 13:14:12 +00:00
const list = []
2020-10-27 17:41:04 +00:00
const map = configTarget.variables[type]
for (const key in map) {
const exists = currentMap[key] != null
2020-10-27 13:14:12 +00:00
list.push({
key,
2020-10-27 17:41:04 +00:00
value: map[key],
2020-10-27 13:14:12 +00:00
exists
})
}
return list
},
2020-10-27 17:41:04 +00:00
async setVariables (type) {
const list = await configApi.getVariables(type)
2020-10-27 13:14:12 +00:00
const noSetList = list.filter(item => {
return !item.exists
})
if (list.length > 0) {
2020-10-29 08:14:12 +00:00
const context = {
root_ca_cert_path: configApi.get().server.setting.rootCaFile.certPath
2020-10-29 08:14:12 +00:00
}
for (const item of noSetList) {
if (item.value.indexOf('${') >= 0) {
for (const key in context) {
item.value = item.value.replcace(new RegExp('${' + key + '}', 'g'), context[key])
}
}
}
2020-10-27 17:41:04 +00:00
const method = type === 'npm' ? Shell.setNpmEnv : Shell.setSystemEnv
return method({ list: noSetList })
2020-10-27 13:14:12 +00:00
}
2020-10-24 13:22:44 +00:00
}
}
2020-10-27 13:14:12 +00:00
module.exports = configApi