( pluginClass: new (...args: any[]) => BasePlugin, name?: string, ...args: any[] )
| 90 | * me.plugin.cache.testPlugin.myfunction (); |
| 91 | */ |
| 92 | export function register( |
| 93 | pluginClass: new (...args: any[]) => BasePlugin, |
| 94 | name?: string, |
| 95 | ...args: any[] |
| 96 | ): void { |
| 97 | // derive name from class if not provided |
| 98 | const pluginName = |
| 99 | name || pluginClass.name || pluginClass.toString().match(/ (\w+)/)![1]; |
| 100 | |
| 101 | // ensure me.plugins[name] is not already "used" |
| 102 | if (cache[pluginName]) { |
| 103 | throw new Error(`plugin ${pluginName} already registered`); |
| 104 | } |
| 105 | |
| 106 | // try to instantiate the plugin |
| 107 | const instance = new pluginClass(...args); |
| 108 | |
| 109 | // inheritance check |
| 110 | if (typeof instance === "undefined" || !(instance instanceof BasePlugin)) { |
| 111 | throw new Error("Plugin should extend the BasePlugin Class !"); |
| 112 | } |
| 113 | |
| 114 | // compatibility testing |
| 115 | if (checkVersion(instance.version, version) > 0) { |
| 116 | throw new Error( |
| 117 | `Plugin version mismatch, expected: ${instance.version}, got: ${version}`, |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | // create a reference to the new plugin |
| 122 | cache[pluginName] = instance; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * returns the plugin instance with the specified class type or registered name |
nothing calls this directly
no test coverage detected