diff options
Diffstat (limited to 'server/lib/plugins/plugin-manager.ts')
-rw-r--r-- | server/lib/plugins/plugin-manager.ts | 55 |
1 files changed, 37 insertions, 18 deletions
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index 37fb07716..f78b989f5 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts | |||
@@ -39,6 +39,7 @@ export interface RegisteredPlugin { | |||
39 | css: string[] | 39 | css: string[] |
40 | 40 | ||
41 | // Only if this is a plugin | 41 | // Only if this is a plugin |
42 | registerHelpersStore?: RegisterHelpersStore | ||
42 | unregister?: Function | 43 | unregister?: Function |
43 | } | 44 | } |
44 | 45 | ||
@@ -58,11 +59,10 @@ export class PluginManager implements ServerHook { | |||
58 | private static instance: PluginManager | 59 | private static instance: PluginManager |
59 | 60 | ||
60 | private registeredPlugins: { [name: string]: RegisteredPlugin } = {} | 61 | private registeredPlugins: { [name: string]: RegisteredPlugin } = {} |
62 | |||
61 | private hooks: { [name: string]: HookInformationValue[] } = {} | 63 | private hooks: { [name: string]: HookInformationValue[] } = {} |
62 | private translations: PluginLocalesTranslations = {} | 64 | private translations: PluginLocalesTranslations = {} |
63 | 65 | ||
64 | private registerHelpersStore: { [npmName: string]: RegisterHelpersStore } = {} | ||
65 | |||
66 | private constructor () { | 66 | private constructor () { |
67 | } | 67 | } |
68 | 68 | ||
@@ -102,18 +102,30 @@ export class PluginManager implements ServerHook { | |||
102 | return this.getRegisteredPluginsOrThemes(PluginType.THEME) | 102 | return this.getRegisteredPluginsOrThemes(PluginType.THEME) |
103 | } | 103 | } |
104 | 104 | ||
105 | getIdAndPassAuths () { | ||
106 | return this.getRegisteredPlugins() | ||
107 | .map(p => ({ npmName: p.npmName, idAndPassAuths: p.registerHelpersStore.getIdAndPassAuths() })) | ||
108 | .filter(v => v.idAndPassAuths.length !== 0) | ||
109 | } | ||
110 | |||
111 | getExternalAuths () { | ||
112 | return this.getRegisteredPlugins() | ||
113 | .map(p => ({ npmName: p.npmName, externalAuths: p.registerHelpersStore.getExternalAuths() })) | ||
114 | .filter(v => v.externalAuths.length !== 0) | ||
115 | } | ||
116 | |||
105 | getRegisteredSettings (npmName: string) { | 117 | getRegisteredSettings (npmName: string) { |
106 | const store = this.registerHelpersStore[npmName] | 118 | const result = this.getRegisteredPluginOrTheme(npmName) |
107 | if (store) return store.getSettings() | 119 | if (!result || result.type !== PluginType.PLUGIN) return [] |
108 | 120 | ||
109 | return [] | 121 | return result.registerHelpersStore.getSettings() |
110 | } | 122 | } |
111 | 123 | ||
112 | getRouter (npmName: string) { | 124 | getRouter (npmName: string) { |
113 | const store = this.registerHelpersStore[npmName] | 125 | const result = this.getRegisteredPluginOrTheme(npmName) |
114 | if (!store) return null | 126 | if (!result || result.type !== PluginType.PLUGIN) return null |
115 | 127 | ||
116 | return store.getRouter() | 128 | return result.registerHelpersStore.getRouter() |
117 | } | 129 | } |
118 | 130 | ||
119 | getTranslations (locale: string) { | 131 | getTranslations (locale: string) { |
@@ -185,11 +197,9 @@ export class PluginManager implements ServerHook { | |||
185 | this.hooks[key] = this.hooks[key].filter(h => h.npmName !== npmName) | 197 | this.hooks[key] = this.hooks[key].filter(h => h.npmName !== npmName) |
186 | } | 198 | } |
187 | 199 | ||
188 | const store = this.registerHelpersStore[plugin.npmName] | 200 | const store = plugin.registerHelpersStore |
189 | store.reinitVideoConstants(plugin.npmName) | 201 | store.reinitVideoConstants(plugin.npmName) |
190 | 202 | ||
191 | delete this.registerHelpersStore[plugin.npmName] | ||
192 | |||
193 | logger.info('Regenerating registered plugin CSS to global file.') | 203 | logger.info('Regenerating registered plugin CSS to global file.') |
194 | await this.regeneratePluginGlobalCSS() | 204 | await this.regeneratePluginGlobalCSS() |
195 | } | 205 | } |
@@ -294,8 +304,11 @@ export class PluginManager implements ServerHook { | |||
294 | this.sanitizeAndCheckPackageJSONOrThrow(packageJSON, plugin.type) | 304 | this.sanitizeAndCheckPackageJSONOrThrow(packageJSON, plugin.type) |
295 | 305 | ||
296 | let library: PluginLibrary | 306 | let library: PluginLibrary |
307 | let registerHelpersStore: RegisterHelpersStore | ||
297 | if (plugin.type === PluginType.PLUGIN) { | 308 | if (plugin.type === PluginType.PLUGIN) { |
298 | library = await this.registerPlugin(plugin, pluginPath, packageJSON) | 309 | const result = await this.registerPlugin(plugin, pluginPath, packageJSON) |
310 | library = result.library | ||
311 | registerHelpersStore = result.registerStore | ||
299 | } | 312 | } |
300 | 313 | ||
301 | const clientScripts: { [id: string]: ClientScript } = {} | 314 | const clientScripts: { [id: string]: ClientScript } = {} |
@@ -314,6 +327,7 @@ export class PluginManager implements ServerHook { | |||
314 | staticDirs: packageJSON.staticDirs, | 327 | staticDirs: packageJSON.staticDirs, |
315 | clientScripts, | 328 | clientScripts, |
316 | css: packageJSON.css, | 329 | css: packageJSON.css, |
330 | registerHelpersStore: registerHelpersStore || undefined, | ||
317 | unregister: library ? library.unregister : undefined | 331 | unregister: library ? library.unregister : undefined |
318 | } | 332 | } |
319 | 333 | ||
@@ -332,15 +346,15 @@ export class PluginManager implements ServerHook { | |||
332 | throw new Error('Library code is not valid (miss register or unregister function)') | 346 | throw new Error('Library code is not valid (miss register or unregister function)') |
333 | } | 347 | } |
334 | 348 | ||
335 | const registerHelpers = this.getRegisterHelpers(npmName, plugin) | 349 | const { registerOptions, registerStore } = this.getRegisterHelpers(npmName, plugin) |
336 | library.register(registerHelpers) | 350 | library.register(registerOptions) |
337 | .catch(err => logger.error('Cannot register plugin %s.', npmName, { err })) | 351 | .catch(err => logger.error('Cannot register plugin %s.', npmName, { err })) |
338 | 352 | ||
339 | logger.info('Add plugin %s CSS to global file.', npmName) | 353 | logger.info('Add plugin %s CSS to global file.', npmName) |
340 | 354 | ||
341 | await this.addCSSToGlobalFile(pluginPath, packageJSON.css) | 355 | await this.addCSSToGlobalFile(pluginPath, packageJSON.css) |
342 | 356 | ||
343 | return library | 357 | return { library, registerStore } |
344 | } | 358 | } |
345 | 359 | ||
346 | // ###################### Translations ###################### | 360 | // ###################### Translations ###################### |
@@ -440,7 +454,10 @@ export class PluginManager implements ServerHook { | |||
440 | 454 | ||
441 | // ###################### Generate register helpers ###################### | 455 | // ###################### Generate register helpers ###################### |
442 | 456 | ||
443 | private getRegisterHelpers (npmName: string, plugin: PluginModel): RegisterServerOptions { | 457 | private getRegisterHelpers ( |
458 | npmName: string, | ||
459 | plugin: PluginModel | ||
460 | ): { registerStore: RegisterHelpersStore, registerOptions: RegisterServerOptions } { | ||
444 | const onHookAdded = (options: RegisterServerHookOptions) => { | 461 | const onHookAdded = (options: RegisterServerHookOptions) => { |
445 | if (!this.hooks[options.target]) this.hooks[options.target] = [] | 462 | if (!this.hooks[options.target]) this.hooks[options.target] = [] |
446 | 463 | ||
@@ -453,9 +470,11 @@ export class PluginManager implements ServerHook { | |||
453 | } | 470 | } |
454 | 471 | ||
455 | const registerHelpersStore = new RegisterHelpersStore(npmName, plugin, onHookAdded.bind(this)) | 472 | const registerHelpersStore = new RegisterHelpersStore(npmName, plugin, onHookAdded.bind(this)) |
456 | this.registerHelpersStore[npmName] = registerHelpersStore | ||
457 | 473 | ||
458 | return registerHelpersStore.buildRegisterHelpers() | 474 | return { |
475 | registerStore: registerHelpersStore, | ||
476 | registerOptions: registerHelpersStore.buildRegisterHelpers() | ||
477 | } | ||
459 | } | 478 | } |
460 | 479 | ||
461 | private sanitizeAndCheckPackageJSONOrThrow (packageJSON: PluginPackageJson, pluginType: PluginType) { | 480 | private sanitizeAndCheckPackageJSONOrThrow (packageJSON: PluginPackageJson, pluginType: PluginType) { |