From fcbdf51eb85b771d3c4ae06e710df58bb9bfc5b8 Mon Sep 17 00:00:00 2001 From: ikechan8370 Date: Wed, 26 Mar 2025 12:01:00 +0800 Subject: [PATCH] fix: read old config --- config/config.js | 46 ++++++++++++++++++++++++++++++++++++++++++++-- package.json | 2 +- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/config/config.js b/config/config.js index 50ee27f..d52bdee 100644 --- a/config/config.js +++ b/config/config.js @@ -254,11 +254,42 @@ class ChatGPTConfig { loadFromFile () { try { const content = fs.readFileSync(this.configPath, 'utf8') - const config = this.configPath.endsWith('.json') + const loadedConfig = this.configPath.endsWith('.json') ? JSON.parse(content) : yaml.load(content) - Object.assign(this, config) + // Deep merge function that preserves default values + const deepMerge = (target, source) => { + // Skip non-object properties or special properties + if (!source || typeof source !== 'object' || + !target || typeof target !== 'object') { + return + } + + for (const key in source) { + // Skip internal properties + if (key === 'watcher' || key === 'configPath') continue + + if (typeof source[key] === 'object' && source[key] !== null && + typeof target[key] === 'object' && target[key] !== null) { + // Recursively merge nested objects + deepMerge(target[key], source[key]) + } else if (source[key] !== undefined) { + // Only update if the value exists in the loaded config + target[key] = source[key] + } + // If source[key] is undefined, keep the default value in target + } + } + + // Apply loaded config to this object, preserving defaults + deepMerge(this, loadedConfig) + + // Save the file to persist any new default values + const hasChanges = JSON.stringify(this.toJSON()) !== content + if (hasChanges) { + this.saveToFile() + } } catch (error) { console.error('Failed to load config:', error) } @@ -287,6 +318,17 @@ class ChatGPTConfig { console.error('Failed to save config:', error) } } + + toJSON () { + return { + version: this.version, + basic: this.basic, + bym: this.bym, + llm: this.llm, + management: this.management, + chaite: this.chaite + } + } } export default new ChatGPTConfig() diff --git a/package.json b/package.json index 8102143..3f563ae 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "type": "module", "author": "ikechan8370", "dependencies": { - "chaite": "^1.3.0", + "chaite": "^1.3.8", "js-yaml": "^4.1.0", "keyv": "^5.3.1", "keyv-file": "^5.1.2",