From ed4bc5be47b2fa6ebccf65a2bbcfbbb358e82ed7 Mon Sep 17 00:00:00 2001 From: Jerry Date: Wed, 23 Apr 2025 14:00:39 +0800 Subject: [PATCH] =?UTF-8?q?config=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/json.js | 22 +++++++++++----------- config/default.json | 0 constants/path.js | 4 ++-- lib/config/configControl.js | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 config/default.json create mode 100644 lib/config/configControl.js diff --git a/components/json.js b/components/json.js index 89a185a..7854371 100644 --- a/components/json.js +++ b/components/json.js @@ -25,7 +25,7 @@ let fc = { * @example * fc.createDir("config/deepseek", "root") // 在 Yunzai 根目录创建 config/deepseek 目录 */ - createDir(path = '', root = '', includeFile = false) { + async createDir(path = '', root = '', includeFile = false) { root = getRoot(root); let pathList = path.split('/'); let nowPath = root; @@ -50,7 +50,7 @@ let fc = { * @example * const config = fc.readJSON("config.json", "root") */ - readJSON(file = '', root = '') { + async readJSON(file = '', root = '') { root = getRoot(root); if (fs.existsSync(`${root}/${file}`)) { try { @@ -62,7 +62,7 @@ let fc = { return {}; }, - statSync(file = '', root = '') { + async statSync(file = '', root = '') { root = getRoot(root); try { return fs.statSync(`${root}/${file}`); @@ -82,8 +82,8 @@ let fc = { * @example * fc.writeJSON("config.json", {key: "value"}, "root", 4) */ - writeJSON(file, data, root = '', space = 4) { - fc.createDir(file, root, true); + async writeJSON(file, data, root = '', space = 4) { + await fc.createDir(file, root, true); root = getRoot(root); try { fs.writeFileSync(`${root}/${file}`, JSON.stringify(data, null, space)); @@ -108,8 +108,8 @@ let fc = { * @example * fc.safewriteJSON("config.json", {newKey: "value"}) */ - safeWriteJSON(file, data, root = '', space = 4) { - fc.createDir(file, root, true); + async safeWriteJSON(file, data, root = '', space = 4) { + await fc.createDir(file, root, true); root = getRoot(root); const filePath = `${root}/${file}`; @@ -146,7 +146,7 @@ let fc = { * const merged = fc.deepMerge({a: 1}, {b: {c: 2}}) * // 返回 {a: 1, b: {c: 2}} */ - deepMerge(target, source) { + async deepMerge(target, source) { for (const key in source) { if (source.hasOwnProperty(key)) { if ( @@ -155,7 +155,7 @@ let fc = { target[key] && typeof target[key] === 'object' ) { - this.deepMerge(target[key], source[key]); + await this.deepMerge(target[key], source[key]); } else { target[key] = source[key]; } @@ -176,7 +176,7 @@ let fc = { * @example * const jsFiles = fc.readDirRecursive("./plugins", "js", "node_modules") */ - readDirRecursive(directory, extension, excludeDir) { + async readDirRecursive(directory, extension, excludeDir) { let files = fs.readdirSync(directory); let jsFiles = files.filter( @@ -213,7 +213,7 @@ let fc = { * const obj = { a: 1, b: [2, 3] }; * const cloned = fc.deepClone(obj); */ - deepClone(source) { + async deepClone(source) { const cache = new WeakMap(); const clone = (value) => { diff --git a/config/default.json b/config/default.json new file mode 100644 index 0000000..e69de29 diff --git a/constants/path.js b/constants/path.js index 5dd4fab..9a14336 100644 --- a/constants/path.js +++ b/constants/path.js @@ -3,14 +3,14 @@ import url from 'url'; const __filename = url.fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); - const rootDir = path.join(__dirname, '..'); const Path = { root: rootDir, apps: path.join(rootDir, 'apps'), components: path.join(rootDir, 'components'), - config: path.join(rootDir, 'config'), + defaultConfig: path.join(rootDir, 'config/default.json'), + config: path.join(rootDir, './../data/crystelf'), constants: path.join(rootDir, 'constants'), lib: path.join(rootDir, 'lib'), models: path.join(rootDir, 'models'), diff --git a/lib/config/configControl.js b/lib/config/configControl.js new file mode 100644 index 0000000..4f0eff5 --- /dev/null +++ b/lib/config/configControl.js @@ -0,0 +1,33 @@ +import Path from '../../constants/path.js'; +import fc from '../../components/json.js'; +import path from 'path'; + +let configCache = null; +let lastModified = 0; +const configPath = Path.config; +const configFile = path.join(configPath, 'config.json'); +const configControl = { + async getConfig() { + try { + const stats = await fc.statSync(configFile, 'root'); + if (!configCache || stats.mtimeMs > lastModified) { + configCache = await fc.readJSON(configFile, 'root'); + lastModified = stats.mtimeMs; + } + return configCache; + } catch (err) { + console.error('读取配置失败:', err); + return {}; + } + }, + async updateConfig(updater) { + try { + const config = this.getConfig(); + configCache = { ...config, ...updater }; + fc.safeWriteJSON(configFile, configCache, 'root', 4); + } catch (err) { + console.error('更新配置失败:', err); + } + }, +}; +export default configControl;