From 9ed695dca2779a79f10627ec429b5dcc2293e8a5 Mon Sep 17 00:00:00 2001 From: Jerryplusy Date: Thu, 25 Sep 2025 00:06:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E9=85=8D=E7=BD=AE=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=83=AD=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/config/configControl.js | 38 ++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/config/configControl.js b/lib/config/configControl.js index 2d4694f..85e0847 100644 --- a/lib/config/configControl.js +++ b/lib/config/configControl.js @@ -7,7 +7,8 @@ const fsp = fs.promises; const pluginConfigPath = Path.defaultConfigPath; const dataConfigPath = Path.config; const configFile = path.join(dataConfigPath, 'config.json'); -let configCache = {}; // 缓存 +let configCache = {}; +let watchers = []; /** * 初始化配置 @@ -80,9 +81,43 @@ async function init() { } } +/** + * 配置文件热更新 + */ +function watchConfigs() { + for (const w of watchers) { + w.close(); + } + watchers = []; + + fsp.readdir(dataConfigPath).then((files) => { + files + .filter((f) => f.endsWith('.json')) + .forEach((file) => { + const filePath = path.join(dataConfigPath, file); + const watcher = fs.watch(filePath, async (eventType) => { + if (eventType === 'change') { + try { + const data = await fc.readJSON(filePath); + const name = path.basename(file, '.json'); + configCache[name] = data; + if (configCache.debug) { + logger.info(`[crystelf-admin] 配置热更新: ${file}`); + } + } catch (e) { + logger.warn(`[crystelf-admin] 热更新读取失败 ${file}:`, e); + } + } + }); + watchers.push(watcher); + }); + }); +} + const configControl = { async init() { await init(); + watchConfigs(); }, get(key) { @@ -126,6 +161,7 @@ const configControl = { async reload() { await init(); + watchConfigs(); return true; }, };