fix: migrate config error

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2022-07-07 00:51:10 +08:00
parent 30846cdc87
commit 67fe13deac

View File

@ -68,30 +68,25 @@ export class PluginConfigManager {
private loadConfig0(plugin: plugin.Plugin, metadata: interfaces.ConfigMetadata) { private loadConfig0(plugin: plugin.Plugin, metadata: interfaces.ConfigMetadata) {
try { try {
let defaultValue = metadata.default ?? plugin[metadata.variable] let defaultValue = metadata.default ?? plugin[metadata.variable]
let configValue = defaultValue || {} metadata.file = fs.concat(
if (defaultValue) { fs.file(plugin.description.loadMetadata.file).parent,
metadata.file = fs.concat( plugin.description.name,
fs.file(plugin.description.loadMetadata.file).parent, metadata.filename
plugin.description.name, )
metadata.filename let configLoader = this.getConfigLoader(metadata.format)
) if (!fs.exists(metadata.file) && defaultValue) {
let configLoader = this.getConfigLoader(metadata.format) base.save(metadata.file, configLoader.dump(defaultValue))
if (!fs.exists(metadata.file)) { console.i18n("ms.plugin.manager.config.save.default", {
base.save(metadata.file, configLoader.dump(defaultValue)) plugin: plugin.description.name,
console.i18n("ms.plugin.manager.config.save.default", { name: metadata.name,
plugin: plugin.description.name, format: metadata.format
name: metadata.name, })
format: metadata.format
})
} else if (metadata.migrate) {
configValue = configLoader.load(base.read(metadata.file)) || {}
if (defaultValue && this.setDefaultValue(configValue, defaultValue, !!metadata.default)) {
base.save(metadata.file, configLoader.dump(configValue))
}
console.debug(`[${plugin.description.name}] Load Config ${metadata.variable} from file ${metadata.file} =>
${JSON.stringify(configValue, undefined, 4).substring(0, 500)}`)
}
} }
let configValue = configLoader.load(base.read(metadata.file)) || {}
if (metadata.migrate && defaultValue && this.setDefaultValue(configValue, defaultValue, !!metadata.default)) {
base.save(metadata.file, configLoader.dump(configValue))
}
console.debug(`[${plugin.description.name}] Load Config ${metadata.variable} from file ${metadata.file}`)
this.defienConfigProp(plugin, metadata, configValue) this.defienConfigProp(plugin, metadata, configValue)
} catch (error: any) { } catch (error: any) {
console.i18n("ms.plugin.manager.config.load.error", { console.i18n("ms.plugin.manager.config.load.error", {