From f0b5e81be680e47d86f2e507023ff3b6cda33069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Wed, 10 Apr 2024 16:15:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=87=BA=E5=8E=82=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=AE=8C=E6=88=90=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/config.js | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/packages/core/src/config.js b/packages/core/src/config.js index 4dbec30..19d61f1 100644 --- a/packages/core/src/config.js +++ b/packages/core/src/config.js @@ -161,7 +161,7 @@ const configApi = { const diffConfig = mergeApi.doDiff(defConfig, newConfig) const configPath = _getConfigPath() fs.writeFileSync(configPath, jsonApi.stringify(diffConfig)) - log.info('保存自定义配置文件成功:', configPath) + log.info('保存 config.json 自定义配置文件成功:', configPath) // 重载配置 const allConfig = configApi.set(diffConfig) @@ -203,15 +203,19 @@ const configApi = { log.warn('newConfig 为空,不做任何操作') return configTarget } - + return configApi.load(newConfig) + }, + load (newConfig) { const merged = lodash.cloneDeep(defConfig) const remoteConfig = configApi.readRemoteConfig() mergeApi.doMerge(merged, remoteConfig) - mergeApi.doMerge(merged, newConfig) + if (newConfig != null) { + mergeApi.doMerge(merged, newConfig) + } mergeApi.deleteNullItems(merged) configTarget = merged - log.info('加载配置完成') + log.info('加载及合并远程配置完成') return configTarget }, @@ -221,6 +225,29 @@ const configApi = { addDefault (key, defValue) { lodash.set(defConfig, key, defValue) }, + async removeUserConfig () { + const configPath = _getConfigPath() + if (fs.existsSync(configPath)) { + // 读取 config.json 文件内容 + const fileStr = fs.readFileSync(configPath).toString().replace(/\s/g, '') + + // 判断文件内容是否为空或空配置 + if (fileStr === '' || fileStr === '{}') { + fs.rmSync(configPath) + return false // config.json 内容为空,或为空json + } + + // 备份用户自定义配置文件 + fs.renameSync(configPath, configPath + '.bak' + new Date().getTime() + '.json') + + // 重新加载配置 + configApi.load(null) + + return true // 删除并重新加载配置成功 + } else { + return false // config.json 文件不存在或内容为配置 + } + }, resetDefault (key) { if (key) { let value = lodash.get(defConfig, key)