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 || {}
if (defaultValue) {
metadata.file = fs.concat( metadata.file = fs.concat(
fs.file(plugin.description.loadMetadata.file).parent, fs.file(plugin.description.loadMetadata.file).parent,
plugin.description.name, plugin.description.name,
metadata.filename metadata.filename
) )
let configLoader = this.getConfigLoader(metadata.format) let configLoader = this.getConfigLoader(metadata.format)
if (!fs.exists(metadata.file)) { if (!fs.exists(metadata.file) && defaultValue) {
base.save(metadata.file, configLoader.dump(defaultValue)) base.save(metadata.file, configLoader.dump(defaultValue))
console.i18n("ms.plugin.manager.config.save.default", { console.i18n("ms.plugin.manager.config.save.default", {
plugin: plugin.description.name, plugin: plugin.description.name,
name: metadata.name, name: metadata.name,
format: metadata.format format: metadata.format
}) })
} else if (metadata.migrate) { }
configValue = configLoader.load(base.read(metadata.file)) || {} let configValue = configLoader.load(base.read(metadata.file)) || {}
if (defaultValue && this.setDefaultValue(configValue, defaultValue, !!metadata.default)) { if (metadata.migrate && defaultValue && this.setDefaultValue(configValue, defaultValue, !!metadata.default)) {
base.save(metadata.file, configLoader.dump(configValue)) base.save(metadata.file, configLoader.dump(configValue))
} }
console.debug(`[${plugin.description.name}] Load Config ${metadata.variable} from file ${metadata.file} => console.debug(`[${plugin.description.name}] Load Config ${metadata.variable} from file ${metadata.file}`)
${JSON.stringify(configValue, undefined, 4).substring(0, 500)}`)
}
}
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", {