* register a plugin * @param pluginConfigCreator - a creator function which returns the plugin config * @param options - the plugin options * @param registerOptions - the plugin register options
(pluginModel: Plugin, registerOptions?: PluginRegisterOptions)
| 107 | * @param registerOptions - the plugin register options |
| 108 | */ |
| 109 | async register(pluginModel: Plugin, registerOptions?: PluginRegisterOptions): Promise<void> { |
| 110 | const config = pluginModel |
| 111 | const { name: pluginName, deps = [], eventPrefix } = config |
| 112 | if (!pluginName) { |
| 113 | logger.error('pluginConfigCreator.pluginName required', pluginModel) |
| 114 | return |
| 115 | } |
| 116 | const meta: PluginMeta = { dependencies: deps, eventPrefix } |
| 117 | const allowOverride = registerOptions?.override === true |
| 118 | |
| 119 | if (this.pluginsMap.has(pluginName)) { |
| 120 | if (allowOverride) { |
| 121 | const originalPlugin = this.pluginsMap.get(pluginName) |
| 122 | logger.log( |
| 123 | 'plugin override, originalPlugin with name ', |
| 124 | pluginName, |
| 125 | ' will be destroyed, config:', |
| 126 | originalPlugin?.config, |
| 127 | ) |
| 128 | originalPlugin?.destroy() |
| 129 | this.pluginsMap.delete(pluginName) |
| 130 | } else { |
| 131 | throw new Error(`Plugin with name ${pluginName} exists`) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | const plugin = new PluginRuntime(pluginName, this, config) |
| 136 | if (registerOptions?.autoInit) { |
| 137 | await plugin.init() |
| 138 | } |
| 139 | this.plugins.push(plugin) |
| 140 | this.pluginsMap.set(pluginName, plugin) |
| 141 | logger.log(`plugin registered with pluginName: ${pluginName}, config: `, config, 'meta:', meta) |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * batch register plugins |