feat: new mjs scanner

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2022-04-01 17:18:32 +08:00
parent 30fd065bc5
commit 2ff57d1c0b
28 changed files with 458 additions and 96 deletions

View File

@@ -78,7 +78,10 @@ export class PluginConfigManager {
'save': { value: () => this.saveConfig0(plugin, metadata) },
'reload': { value: () => this.loadConfig0(plugin, metadata) }
})
Object.defineProperty(plugin, metadata.variable, { value })
Object.defineProperty(plugin, metadata.variable, {
value,
configurable: true
})
}
private loadConfig0(plugin: plugin.Plugin, metadata: interfaces.ConfigMetadata) {
@@ -86,37 +89,50 @@ export class PluginConfigManager {
let defaultValue = metadata.default ?? plugin[metadata.variable]
let configValue = defaultValue || {}
if (defaultValue) {
metadata.file = fs.concat(fs.file(plugin.description.loadMetadata.file).parent, plugin.description.name, metadata.filename)
metadata.file = fs.concat(
fs.file(plugin.description.loadMetadata.file).parent,
plugin.description.name,
metadata.filename
)
let configLoader = this.getConfigLoader(metadata.format)
if (!fs.exists(metadata.file)) {
base.save(metadata.file, configLoader.dump(defaultValue))
console.i18n("ms.plugin.manager.config.save.default", { plugin: plugin.description.name, name: metadata.name, format: metadata.format })
console.i18n("ms.plugin.manager.config.save.default", {
plugin: plugin.description.name,
name: metadata.name,
format: metadata.format
})
} else {
configValue = configLoader.load(base.read(metadata.file)) || {}
if (defaultValue && this.setDefaultValue(configValue, defaultValue)) {
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} =>\n${JSON.stringify(configValue, undefined, 4).substr(0, 500)}`)
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)
} catch (error: any) {
console.i18n("ms.plugin.manager.config.load.error", { plugin: plugin.description.name, name: metadata.name, format: metadata.format, error })
console.i18n("ms.plugin.manager.config.load.error", {
plugin: plugin.description.name,
name: metadata.name,
format: metadata.format,
error
})
console.ex(error)
}
}
private setDefaultValue(configValue, defaultValue) {
private setDefaultValue(configValue, defaultValue, deepCopy) {
let needSave = false
for (const key of Object.keys(defaultValue)) {
// 当配置文件不存在当前属性时才进行赋值
if (!Object.prototype.hasOwnProperty.call(configValue, key) && key != '____deep_copy____') {
if (!Object.prototype.hasOwnProperty.call(configValue, key)) {
configValue[key] = defaultValue[key]
needSave = true
} else if (Object.prototype.toString.call(configValue[key]) == "[object Object]"
&& Object.prototype.hasOwnProperty.call(defaultValue[key], '____deep_copy____')) {
// 对象需要递归检测 如果对象内存在 ____deep_copy____ 那就忽略设置
needSave ||= this.setDefaultValue(configValue[key], defaultValue[key])
} else if (Object.prototype.toString.call(configValue[key]) == "[object Object]" && deepCopy) {
// 对象需要递归检测
needSave ||= this.setDefaultValue(configValue[key], defaultValue[key], deepCopy)
}
}
return needSave
@@ -127,10 +143,16 @@ export class PluginConfigManager {
metadata.file = fs.concat(fs.file(plugin.description.loadMetadata.file).parent, plugin.description.name, metadata.filename)
let result = this.getConfigLoader(metadata.format).dump(plugin[metadata.variable])
base.save(metadata.file, result)
console.debug(`[${plugin.description.name}] Save Config ${metadata.variable} to file ${metadata.file} =>\n${result.substr(0, 500)}`)
console.debug(`[${plugin.description.name}] Save Config ${metadata.variable} to file ${metadata.file} =>
${result.substring(0, 500)}`)
return true
} catch (error: any) {
console.i18n("ms.plugin.manager.config.save.error", { plugin: plugin.description.name, name: metadata.name, format: metadata.format, error })
console.i18n("ms.plugin.manager.config.save.error", {
plugin: plugin.description.name,
name: metadata.name,
format: metadata.format,
error
})
console.ex(error)
return false
}