diff --git a/packages/core/src/config/index.js b/packages/core/src/config/index.js index ed7e244..1c8ad34 100644 --- a/packages/core/src/config/index.js +++ b/packages/core/src/config/index.js @@ -1,4 +1,8 @@ +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 || '/' @@ -13,7 +17,7 @@ function getRootCaKeyPath () { return path.join(getUserBasePath(), '/dev-sidecar.ca.key.pem') } -module.exports = { +const defaultConfig = { app: { mode: 'default', autoStart: { @@ -37,6 +41,10 @@ module.exports = { }, closeStrategy: 0, showShutdownTip: true, + + // 日志相关配置 + logFileSavePath: path.join(getUserBasePath(), '/logs'), // 日志文件保存路径 + keepLogFileCount: 15, // 保留日志文件数 }, server: { enabled: true, @@ -69,10 +77,6 @@ module.exports = { // 慢速IP延迟时间:测速超过该值时,则视为延迟高,显示为橙色 lowSpeedDelay: 200, - - // 日志相关配置 - logFileSavePath: path.join(getUserBasePath(), '/logs'), // 日志文件保存路径 - keepLogFileCount: 15, // 保留日志文件数 }, compatible: { // **** 自定义兼容配置 **** // @@ -442,3 +446,108 @@ module.exports = { ], }, } + +// 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) && fs.existsSync(oldFilePath)) { + return oldFilePath // 如果新文件不存在,且旧文件存在,则返回旧文件路径 + } + return newFilePath +} + +function _getConfig () { + const configFilePath = _getConfigPath() + if (configFilePath == null) { + return {} + } + + return jsonApi.parse(fs.readFileSync(configFilePath)) +} + +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 = '') { + return jsonApi.parse(_readRemoteConfigStr(suffix)) +} + +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() + +module.exports = defaultConfig diff --git a/packages/core/src/utils/util.logger.js b/packages/core/src/utils/util.logger.js index 275739b..bc90321 100644 --- a/packages/core/src/utils/util.logger.js +++ b/packages/core/src/utils/util.logger.js @@ -1,19 +1,19 @@ const path = require('node:path') const log4js = require('log4js') -const config = require('../config/index') +const configFromFiles = require('../config/index').configFromFiles const level = process.env.NODE_ENV === 'development' ? 'debug' : 'info' function getDefaultConfigBasePath () { - if (config.server.setting.logFileSavePath) { - let logFileSavePath = config.server.setting.logFileSavePath + if (configFromFiles.app.logFileSavePath) { + let logFileSavePath = configFromFiles.app.logFileSavePath if (logFileSavePath.endsWith('/') || logFileSavePath.endsWith('\\')) { logFileSavePath = logFileSavePath.slice(0, -1) } // eslint-disable-next-line no-template-curly-in-string - return logFileSavePath.replace('${userBasePath}', config.server.setting.userBasePath) + return logFileSavePath.replace('${userBasePath}', configFromFiles.server.setting.userBasePath) } else { - return path.join(config.server.setting.userBasePath, '/logs') + return path.join(configFromFiles.server.setting.userBasePath, '/logs') } } @@ -23,7 +23,7 @@ const guiLogFilename = path.join(getDefaultConfigBasePath(), '/gui.log') const serverLogFilename = path.join(getDefaultConfigBasePath(), '/server.log') // 日志相关配置 -const backups = config.server.setting.keepLogFileCount // 保留日志文件数 +const backups = configFromFiles.app.keepLogFileCount // 保留日志文件数 const appenderConfig = { type: 'file', pattern: 'yyyy-MM-dd', diff --git a/packages/gui/src/bridge/api/backend.js b/packages/gui/src/bridge/api/backend.js index e8582af..6cf37bf 100644 --- a/packages/gui/src/bridge/api/backend.js +++ b/packages/gui/src/bridge/api/backend.js @@ -6,6 +6,7 @@ import lodash from 'lodash' const jsonApi = require('@docmirror/mitmproxy/src/json') const pk = require('../../../package.json') +const configFromFiles = require('@docmirror/dev-sidecar/src/config/index.js').configFromFiles const log = require('../../utils/util.log') const mitmproxyPath = path.join(__dirname, 'mitmproxy.js') @@ -50,6 +51,9 @@ const localApi = { getConfigDir () { return getDefaultConfigBasePath() }, + getLogDir () { + return configFromFiles.app.logFileSavePath || path.join(getDefaultConfigBasePath(), '/logs/') + }, getSystemPlatform (throwIfUnknown = false) { return DevSidecar.api.shell.getSystemPlatform(throwIfUnknown) }, diff --git a/packages/gui/src/view/mixins/plugin.js b/packages/gui/src/view/mixins/plugin.js index 65cd8d1..afd685e 100644 --- a/packages/gui/src/view/mixins/plugin.js +++ b/packages/gui/src/view/mixins/plugin.js @@ -139,8 +139,8 @@ export default { return this.systemPlatform === 'linux' }, async openLog () { - const dir = await this.$api.info.getConfigDir() - this.$api.ipc.openPath(`${dir}/logs/`) + const dir = await this.$api.info.getLogDir() + this.$api.ipc.openPath(dir) }, }, } diff --git a/packages/gui/src/view/pages/setting.vue b/packages/gui/src/view/pages/setting.vue index 0942ab1..60e1f6f 100644 --- a/packages/gui/src/view/pages/setting.vue +++ b/packages/gui/src/view/pages/setting.vue @@ -451,11 +451,18 @@ export default {