From 860946a44b8789aa108458893e82c5c6bc1e4abf Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Sun, 26 Apr 2020 10:19:44 +0800 Subject: [PATCH] feat: merge stage exec & catch error Signed-off-by: MiaoWoo --- packages/i18n/languages/en.yml | 1 + packages/i18n/languages/zh_cn.yml | 1 + packages/plugin/src/manager.ts | 44 ++++++++++++++++++------------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/packages/i18n/languages/en.yml b/packages/i18n/languages/en.yml index bd10124c..ee47e53b 100644 --- a/packages/i18n/languages/en.yml +++ b/packages/i18n/languages/en.yml @@ -34,6 +34,7 @@ ms.plugin.manager.scan: "Scanning Plugins in {folder} ..." ms.plugin.manager.initialize.error: "§6Plugin §b{name} §6initialize error §4{ex}" ms.plugin.manager.stage: "{stage} {plugin} version {version} by {author}" ms.plugin.manager.stage.exec: "[{plugin}] Exec {name} Stage {stage} When servers is {servers}..." +ms.plugin.manager.stage.exec.error: "§6Plugin §b{plugin} §6exec §d{executor} §6function error §4{error}" ms.plugin.manager.stage.load: "Loading" ms.plugin.manager.stage.enable: "Enabling" ms.plugin.manager.stage.disable: "Disabling" diff --git a/packages/i18n/languages/zh_cn.yml b/packages/i18n/languages/zh_cn.yml index e68bafcc..962b2863 100644 --- a/packages/i18n/languages/zh_cn.yml +++ b/packages/i18n/languages/zh_cn.yml @@ -34,6 +34,7 @@ ms.plugin.manager.scan: "扫描 {folder} 文件夹中插件..." ms.plugin.manager.initialize.error: "§6插件 §b{name} §6初始化错误 §4{ex}" ms.plugin.manager.stage: "{stage} {plugin} 版本 {version} 作者 {author}" ms.plugin.manager.stage.exec: "[{plugin}] 执行 {stage} 阶段函数 {name} 匹配类型 {servers}..." +ms.plugin.manager.stage.exec.error: "§6插件 §b{plugin} §6执行 §d{executor} §6函数发生异常 错误: §4{error}" ms.plugin.manager.stage.load: "加载" ms.plugin.manager.stage.enable: "启用" ms.plugin.manager.stage.disable: "关闭" diff --git a/packages/plugin/src/manager.ts b/packages/plugin/src/manager.ts index 264c9978..73999e44 100644 --- a/packages/plugin/src/manager.ts +++ b/packages/plugin/src/manager.ts @@ -52,35 +52,36 @@ export class PluginManagerImpl implements plugin.PluginManager { console.i18n("ms.plugin.manager.stage", { stage, plugin: plugin.description.name, version: plugin.description.version, author: plugin.description.author }) } + private runPluginStage(plugin: interfaces.Plugin, stage: string, ext: Function) { + this.logStage(plugin, i18n.translate(`ms.plugin.manager.stage.${stage}`)) + this.runCatch(plugin, stage) + this.runCatch(plugin, `${this.serverType}${stage}`) + this.execPluginStage(plugin, stage) + } + load(...args: any[]): void { this.checkAndGet(args[0]).forEach((plugin: interfaces.Plugin) => { - this.logStage(plugin, i18n.translate("ms.plugin.manager.stage.load")) - this.loadConfig(plugin) - this.runCatch(plugin, 'load') - this.runCatch(plugin, `${this.serverType}load`) - this.execPluginStage(plugin, 'load') + this.runPluginStage(plugin, 'load', () => { + this.loadConfig(plugin) + }) }) } enable(...args: any[]): void { this.checkAndGet(args[0]).forEach((plugin: interfaces.Plugin) => { - this.logStage(plugin, i18n.translate("ms.plugin.manager.stage.enable")) - this.registryCommand(plugin) - this.registryListener(plugin) - this.runCatch(plugin, 'enable') - this.runCatch(plugin, `${this.serverType}enable`) - this.execPluginStage(plugin, 'enable') + this.runPluginStage(plugin, 'enable', () => { + this.registryCommand(plugin) + this.registryListener(plugin) + }) }) } disable(...args: any[]): void { this.checkAndGet(args[0]).forEach((plugin: interfaces.Plugin) => { - this.logStage(plugin, i18n.translate("ms.plugin.manager.stage.disable")) - this.unregistryCommand(plugin) - this.unregistryListener(plugin) - this.runCatch(plugin, 'disable') - this.runCatch(plugin, `${this.serverType}disable`) - this.execPluginStage(plugin, 'disable') + this.runPluginStage(plugin, 'disable', () => { + this.unregistryCommand(plugin) + this.unregistryListener(plugin) + }) }) } @@ -102,7 +103,7 @@ export class PluginManagerImpl implements plugin.PluginManager { try { if (pl[func]) pl[func].call(pl) } catch (ex) { - console.console(`§6Plugin §b${pl.description.name} §6exec §d${func} §6function error §4${ex}`) + console.i18n("ms.plugin.manager.stage.exec.error", { plugin: pl.description.name, executor: func, error: ex }) console.ex(ex) } } @@ -269,7 +270,12 @@ export class PluginManagerImpl implements plugin.PluginManager { for (const stage of stages) { if (!this.allowProcess(stage.servers)) { continue } console.i18n("ms.plugin.manager.stage.exec", { plugin: pluginInstance.description.name, name: stage.executor, stage: stageName, servers: stage.servers }) - pluginInstance[stage.executor].apply(pluginInstance) + try { + pluginInstance[stage.executor].apply(pluginInstance) + } catch (error) { + console.i18n("ms.plugin.manager.stage.exec.error", { plugin: pluginInstance.description.name, executor: stage.executor, error }) + console.ex(error) + } } } }