Skip to content

Commit

Permalink
refactor: 配置相关代码重构,减少重复代码。
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Jan 23, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 95de18a commit 4f690ae
Showing 7 changed files with 183 additions and 266 deletions.
138 changes: 21 additions & 117 deletions packages/core/src/config.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,19 @@
const fs = require('node:fs')
const path = require('node:path')
const jsonApi = require('@docmirror/mitmproxy/src/json')
const lodash = require('lodash')
const request = require('request')
const defConfig = require('./config/index.js')
const mergeApi = require('./merge.js')
const Shell = require('./shell')
const log = require('./utils/util.log')
const configLoader = require('./config/local-config-loader')

let configTarget = lodash.cloneDeep(defConfig)

function get () {
return configTarget
}

const getDefaultConfigBasePath = function () {
return get().server.setting.userBasePath
}

function _getRemoteSavePath (suffix = '') {
const dir = getDefaultConfigBasePath()
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
return path.join(dir, `/remote_config${suffix}.json5`)
}

function _getConfigPath () {
const dir = getDefaultConfigBasePath()
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
return path.join(dir, '/config.json')
} else {
// 兼容1.7.3及以下版本的配置文件处理逻辑
const newFilePath = path.join(dir, '/config.json')
const oldFilePath = path.join(dir, '/config.json5')
if (!fs.existsSync(newFilePath) && fs.existsSync(oldFilePath)) {
return oldFilePath // 如果新文件不存在,且旧文件存在,则返回旧文件路径
}
return newFilePath
}
}

let timer
const configApi = {
async startAutoDownloadRemoteConfig () {
@@ -68,13 +40,10 @@ const configApi = {
}

const remoteConfig = get().app.remoteConfig
await configApi.doDownloadCommonRemoteConfig(remoteConfig.url)
await configApi.doDownloadCommonRemoteConfig(remoteConfig.personalUrl, '_personal')
await configApi.doDownloadRemoteConfig(remoteConfig.url)
await configApi.doDownloadRemoteConfig(remoteConfig.personalUrl, '_personal')
},
doDownloadCommonRemoteConfig (remoteConfigUrl, suffix = '') {
if (get().app.remoteConfig.enabled !== true) {
return
}
doDownloadRemoteConfig (remoteConfigUrl, suffix = '') {
if (!remoteConfigUrl) {
// 删除保存的远程配置文件
configApi.deleteRemoteConfigFile(suffix)
@@ -116,7 +85,7 @@ const configApi = {
}

if (remoteConfig != null) {
const remoteSavePath = _getRemoteSavePath(suffix)
const remoteSavePath = configLoader.getRemoteConfigPath(suffix)
fs.writeFileSync(remoteSavePath, body)
log.info('保存远程配置文件成功:', remoteSavePath)
} else {
@@ -139,35 +108,15 @@ const configApi = {
})
},
deleteRemoteConfigFile (suffix = '') {
const remoteSavePath = _getRemoteSavePath(suffix)
const remoteSavePath = configLoader.getRemoteConfigPath(suffix)
if (fs.existsSync(remoteSavePath)) {
fs.unlinkSync(remoteSavePath)
log.info('删除远程配置文件成功:', remoteSavePath)
}
},
readRemoteConfig (suffix = '') {
try {
return jsonApi.parse(configApi.readRemoteConfigStr(suffix))
} catch (e) {
log.error(`读取远程配置失败,suffix: ${suffix}`, e)
return {}
}
},
readRemoteConfigStr (suffix = '') {
if (get().app.remoteConfig.enabled !== true) {
if (suffix === '_personal') {
if (!get().app.remoteConfig.personalUrl) {
return '{}'
}
} else if (suffix === '') {
if (!get().app.remoteConfig.url) {
return '{}'
}
}
return '{}'
}
try {
const path = _getRemoteSavePath(suffix)
const path = configLoader.getRemoteConfigPath(suffix)
if (fs.existsSync(path)) {
const file = fs.readFileSync(path)
log.info('读取远程配置文件内容成功:', path)
@@ -187,21 +136,23 @@ const configApi = {
*/
save (newConfig) {
// 对比默认config的异同
let defConfig = configApi.getDefault()
const defConfig = configApi.cloneDefault()

// 如果开启了远程配置,则读取远程配置,合并到默认配置中
if (get().app.remoteConfig.enabled === true) {
if (get().app.remoteConfig.url) {
defConfig = mergeApi.doMerge(defConfig, configApi.readRemoteConfig())
mergeApi.doMerge(defConfig, configLoader.getRemoteConfig())
}
if (get().app.remoteConfig.personalUrl) {
defConfig = mergeApi.doMerge(defConfig, configApi.readRemoteConfig('_personal'))
mergeApi.doMerge(defConfig, configLoader.getRemoteConfig('_personal'))
}
}

// 计算新配置与默认配置(启用远程配置时,含远程配置)的差异,并保存到 config.json 中
// 计算新配置与默认配置(启用远程配置时,含远程配置)的差异
const diffConfig = mergeApi.doDiff(defConfig, newConfig)
const configPath = _getConfigPath()

// 将差异作为用户配置保存到 config.json 中
const configPath = configLoader.getUserConfigPath()
fs.writeFileSync(configPath, jsonApi.stringify(diffConfig))
log.info('保存 config.json 自定义配置文件成功:', configPath)

@@ -219,25 +170,8 @@ const configApi = {
* 读取 config.json 后,合并配置
*/
reload () {
const configPath = _getConfigPath()
let userConfig
if (!fs.existsSync(configPath)) {
userConfig = {}
log.info('config.json 文件不存在:', configPath)
} else {
const file = fs.readFileSync(configPath)
log.info('读取 config.json 成功:', configPath)
const fileStr = file.toString()
try {
userConfig = jsonApi.parse(fileStr)
} catch (e) {
log.error(`config.json 文件内容格式不正确,文件路径:${configPath},文件内容: ${fileStr}, error:`, e)
userConfig = {}
}
}

const config = configApi.set(userConfig)
return config || {}
const userConfig = configLoader.getUserConfig()
return configApi.set(userConfig) || {}
},
update (partConfig) {
const newConfig = lodash.merge(configApi.get(), partConfig)
@@ -252,49 +186,19 @@ const configApi = {
return configApi.load(newConfig)
},
load (newConfig) {
// 以用户配置作为基准配置,是为了保证用户配置的顺序在前
const merged = newConfig != null ? lodash.cloneDeep(newConfig) : {}

if (get().app.remoteConfig.enabled === true) {
let personalRemoteConfig = null
let shareRemoteConfig = null

if (get().app.remoteConfig.personalUrl) {
personalRemoteConfig = configApi.readRemoteConfig('_personal')
mergeApi.doMerge(merged, personalRemoteConfig) // 先合并一次个人远程配置,使配置顺序在前
}
if (get().app.remoteConfig.url) {
shareRemoteConfig = configApi.readRemoteConfig()
mergeApi.doMerge(merged, shareRemoteConfig) // 先合并一次共享远程配置,使配置顺序在前
}
mergeApi.doMerge(merged, defConfig) // 合并默认配置,顺序排在最后
if (get().app.remoteConfig.url) {
mergeApi.doMerge(merged, shareRemoteConfig) // 再合并一次共享远程配置,使配置生效
}
if (get().app.remoteConfig.personalUrl) {
mergeApi.doMerge(merged, personalRemoteConfig) // 再合并一次个人远程配置,使配置生效
}
} else {
mergeApi.doMerge(merged, defConfig) // 合并默认配置
}
if (newConfig != null) {
mergeApi.doMerge(merged, newConfig) // 再合并一次用户配置,使用户配置重新生效
}
mergeApi.deleteNullItems(merged) // 删除为null及[delete]的项
configTarget = merged
log.info('加载及合并远程配置完成')

return configTarget
const config = configLoader.getConfigFromFiles(newConfig, defConfig)
configTarget = config
return config
},
getDefault () {
cloneDefault () {
return lodash.cloneDeep(defConfig)
},
addDefault (key, defValue) {
lodash.set(defConfig, key, defValue)
},
// 移除用户配置,用于恢复出厂设置功能
async removeUserConfig () {
const configPath = _getConfigPath()
const configPath = configLoader.getUserConfigPath()
if (fs.existsSync(configPath)) {
// 读取 config.json 文件内容
const fileOriginalStr = fs.readFileSync(configPath).toString()
139 changes: 6 additions & 133 deletions packages/core/src/config/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
const fs = require('node:fs')
const path = require('node:path')
const lodash = require('lodash')
const jsonApi = require('@docmirror/mitmproxy/src/json')
const mergeApi = require('../merge')

function getUserBasePath () {
const userHome = process.env.USERPROFILE || process.env.HOME || '/'
return path.resolve(userHome, './.dev-sidecar')
}
const configLoader = require('./local-config-loader')

function getRootCaCertPath () {
return path.join(getUserBasePath(), '/dev-sidecar.ca.crt')
return path.join(configLoader.getUserBasePath(), '/dev-sidecar.ca.crt')
}

function getRootCaKeyPath () {
return path.join(getUserBasePath(), '/dev-sidecar.ca.key.pem')
return path.join(configLoader.getUserBasePath(), '/dev-sidecar.ca.key.pem')
}

const defaultConfig = {
@@ -43,7 +35,7 @@ const defaultConfig = {
showShutdownTip: true,

// 日志相关配置
logFileSavePath: path.join(getUserBasePath(), '/logs'), // 日志文件保存路径
logFileSavePath: path.join(configLoader.getUserBasePath(), '/logs'), // 日志文件保存路径
keepLogFileCount: 15, // 保留日志文件数
},
server: {
@@ -57,7 +49,7 @@ const defaultConfig = {
enabled: true,
defaultDir: './extra/scripts/',
},
userBasePath: getUserBasePath(),
userBasePath: configLoader.getUserBasePath(),
rootCaFile: {
certPath: getRootCaCertPath(),
keyPath: getRootCaKeyPath(),
@@ -447,126 +439,7 @@ const defaultConfig = {
},
}

// region 加载本地配置文件所需的方法

function _getConfigPath () {
const dir = defaultConfig.server.setting.userBasePath
if (!fs.existsSync(dir)) {
return null
}

// 兼容1.7.3及以下版本的配置文件处理逻辑
const newFilePath = path.join(dir, '/config.json')
const oldFilePath = path.join(dir, '/config.json5')
if (!fs.existsSync(newFilePath)) {
if (fs.existsSync(oldFilePath)) {
return oldFilePath // 如果新文件不存在,且旧文件存在,则返回旧文件路径
} else {
return null // 两个文件都不存在
}
}

return newFilePath
}

function _getConfig () {
const configFilePath = _getConfigPath()
if (configFilePath == null) {
return {}
}

if (!fs.existsSync(configFilePath)) {
return {}
}

try {
return jsonApi.parse(fs.readFileSync(configFilePath))
} catch (e) {
console.error('读取配置文件失败:', configFilePath, e)
return {}
}
}

function _getRemoteSavePath (suffix = '') {
const dir = defaultConfig.server.setting.userBasePath
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
return path.join(dir, `/remote_config${suffix}.json5`)
}

function _readRemoteConfigStr (suffix = '') {
if (defaultConfig.app.remoteConfig.enabled !== true) {
if (suffix === '_personal') {
if (!defaultConfig.app.remoteConfig.personalUrl) {
return '{}'
}
} else if (suffix === '') {
if (!defaultConfig.app.remoteConfig.url) {
return '{}'
}
}
return '{}'
}
try {
const path = _getRemoteSavePath(suffix)
if (fs.existsSync(path)) {
const file = fs.readFileSync(path)
return file.toString()
}
} catch {
}

return '{}'
}

function _readRemoteConfig (suffix = '') {
try {
return jsonApi.parse(_readRemoteConfigStr(suffix))
} catch (e) {
console.error(`读取远程配置失败,suffix: ${suffix}`, e)
return {}
}
}

function _getConfigFromFiles () {
const newConfig = _getConfig()

const merged = newConfig != null ? lodash.cloneDeep(newConfig) : {}

if (defaultConfig.app.remoteConfig.enabled === true) {
let personalRemoteConfig = null
let shareRemoteConfig = null

if (defaultConfig.app.remoteConfig.personalUrl) {
personalRemoteConfig = _readRemoteConfig('_personal')
mergeApi.doMerge(merged, personalRemoteConfig) // 先合并一次个人远程配置,使配置顺序在前
}
if (defaultConfig.app.remoteConfig.url) {
shareRemoteConfig = _readRemoteConfig()
mergeApi.doMerge(merged, shareRemoteConfig) // 先合并一次共享远程配置,使配置顺序在前
}
mergeApi.doMerge(merged, defaultConfig) // 合并默认配置,顺序排在最后
if (defaultConfig.app.remoteConfig.url) {
mergeApi.doMerge(merged, shareRemoteConfig) // 再合并一次共享远程配置,使配置生效
}
if (defaultConfig.app.remoteConfig.personalUrl) {
mergeApi.doMerge(merged, personalRemoteConfig) // 再合并一次个人远程配置,使配置生效
}
} else {
mergeApi.doMerge(merged, defaultConfig) // 合并默认配置
}
if (newConfig != null) {
mergeApi.doMerge(merged, newConfig) // 再合并一次用户配置,使用户配置重新生效
}
mergeApi.deleteNullItems(merged) // 删除为null及[delete]的项

return merged
}

// endregion

// 从本地文件中加载配置
defaultConfig.configFromFiles = _getConfigFromFiles()
defaultConfig.configFromFiles = configLoader.getConfigFromFiles(configLoader.getUserConfig(), defaultConfig)

module.exports = defaultConfig
Loading

0 comments on commit 4f690ae

Please sign in to comment.