mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-16 13:27:08 +00:00
fix: read old config
This commit is contained in:
parent
7fe9673dce
commit
fcbdf51eb8
2 changed files with 45 additions and 3 deletions
|
|
@ -254,11 +254,42 @@ class ChatGPTConfig {
|
||||||
loadFromFile () {
|
loadFromFile () {
|
||||||
try {
|
try {
|
||||||
const content = fs.readFileSync(this.configPath, 'utf8')
|
const content = fs.readFileSync(this.configPath, 'utf8')
|
||||||
const config = this.configPath.endsWith('.json')
|
const loadedConfig = this.configPath.endsWith('.json')
|
||||||
? JSON.parse(content)
|
? JSON.parse(content)
|
||||||
: yaml.load(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) {
|
} catch (error) {
|
||||||
console.error('Failed to load config:', error)
|
console.error('Failed to load config:', error)
|
||||||
}
|
}
|
||||||
|
|
@ -287,6 +318,17 @@ class ChatGPTConfig {
|
||||||
console.error('Failed to save config:', error)
|
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()
|
export default new ChatGPTConfig()
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"author": "ikechan8370",
|
"author": "ikechan8370",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"chaite": "^1.3.0",
|
"chaite": "^1.3.8",
|
||||||
"js-yaml": "^4.1.0",
|
"js-yaml": "^4.1.0",
|
||||||
"keyv": "^5.3.1",
|
"keyv": "^5.3.1",
|
||||||
"keyv-file": "^5.1.2",
|
"keyv-file": "^5.1.2",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue