]>
Commit | Line | Data |
---|---|---|
74fd2643 | 1 | import * as express from 'express' |
67ed6552 C |
2 | import { createReadStream, createWriteStream } from 'fs' |
3 | import { outputFile, readJSON } from 'fs-extra' | |
f023a19c | 4 | import { basename, join } from 'path' |
67ed6552 C |
5 | import { MOAuthTokenUser, MUser } from '@server/types/models' |
6 | import { RegisterServerHookOptions } from '@shared/models/plugins/register-server-hook.model' | |
7 | import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks' | |
d75db01f C |
8 | import { |
9 | ClientScript, | |
10 | PluginPackageJson, | |
11 | PluginTranslationPaths as PackagePluginTranslations | |
12 | } from '../../../shared/models/plugins/plugin-package-json.model' | |
67ed6552 | 13 | import { PluginTranslation } from '../../../shared/models/plugins/plugin-translation.model' |
345da516 | 14 | import { PluginType } from '../../../shared/models/plugins/plugin.type' |
5e2b2e27 | 15 | import { ServerHook, ServerHookName } from '../../../shared/models/plugins/server-hook.model' |
67ed6552 C |
16 | import { isLibraryCodeValid, isPackageJSONValid } from '../../helpers/custom-validators/plugins' |
17 | import { logger } from '../../helpers/logger' | |
18 | import { CONFIG } from '../../initializers/config' | |
19 | import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants' | |
20 | import { PluginModel } from '../../models/server/plugin' | |
21 | import { PluginLibrary, RegisterServerAuthExternalOptions, RegisterServerAuthPassOptions, RegisterServerOptions } from '../../types/plugins' | |
a8b666e9 | 22 | import { ClientHtml } from '../client-html' |
1896bca0 | 23 | import { RegisterHelpers } from './register-helpers' |
67ed6552 | 24 | import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn' |
345da516 C |
25 | |
26 | export interface RegisteredPlugin { | |
b5f919ac | 27 | npmName: string |
345da516 C |
28 | name: string |
29 | version: string | |
30 | description: string | |
31 | peertubeEngine: string | |
32 | ||
33 | type: PluginType | |
34 | ||
35 | path: string | |
36 | ||
37 | staticDirs: { [name: string]: string } | |
2c053942 | 38 | clientScripts: { [name: string]: ClientScript } |
345da516 C |
39 | |
40 | css: string[] | |
41 | ||
42 | // Only if this is a plugin | |
1896bca0 | 43 | registerHelpers?: RegisterHelpers |
345da516 C |
44 | unregister?: Function |
45 | } | |
46 | ||
47 | export interface HookInformationValue { | |
b5f919ac | 48 | npmName: string |
345da516 C |
49 | pluginName: string |
50 | handler: Function | |
51 | priority: number | |
52 | } | |
53 | ||
d75db01f | 54 | type PluginLocalesTranslations = { |
a1587156 | 55 | [locale: string]: PluginTranslation |
d75db01f C |
56 | } |
57 | ||
b4055e1c | 58 | export class PluginManager implements ServerHook { |
345da516 C |
59 | |
60 | private static instance: PluginManager | |
61 | ||
a1587156 | 62 | private registeredPlugins: { [name: string]: RegisteredPlugin } = {} |
7fed6375 | 63 | |
a1587156 | 64 | private hooks: { [name: string]: HookInformationValue[] } = {} |
d75db01f | 65 | private translations: PluginLocalesTranslations = {} |
345da516 C |
66 | |
67 | private constructor () { | |
68 | } | |
69 | ||
ad91e700 | 70 | // ###################### Getters ###################### |
345da516 | 71 | |
6702a1b2 C |
72 | isRegistered (npmName: string) { |
73 | return !!this.getRegisteredPluginOrTheme(npmName) | |
74 | } | |
75 | ||
b5f919ac C |
76 | getRegisteredPluginOrTheme (npmName: string) { |
77 | return this.registeredPlugins[npmName] | |
7cd4d2ba C |
78 | } |
79 | ||
e1c55031 | 80 | getRegisteredPluginByShortName (name: string) { |
b5f919ac C |
81 | const npmName = PluginModel.buildNpmName(name, PluginType.PLUGIN) |
82 | const registered = this.getRegisteredPluginOrTheme(npmName) | |
7cd4d2ba C |
83 | |
84 | if (!registered || registered.type !== PluginType.PLUGIN) return undefined | |
85 | ||
86 | return registered | |
345da516 C |
87 | } |
88 | ||
e1c55031 | 89 | getRegisteredThemeByShortName (name: string) { |
b5f919ac C |
90 | const npmName = PluginModel.buildNpmName(name, PluginType.THEME) |
91 | const registered = this.getRegisteredPluginOrTheme(npmName) | |
345da516 C |
92 | |
93 | if (!registered || registered.type !== PluginType.THEME) return undefined | |
94 | ||
95 | return registered | |
96 | } | |
97 | ||
18a6f04c | 98 | getRegisteredPlugins () { |
7cd4d2ba C |
99 | return this.getRegisteredPluginsOrThemes(PluginType.PLUGIN) |
100 | } | |
101 | ||
102 | getRegisteredThemes () { | |
103 | return this.getRegisteredPluginsOrThemes(PluginType.THEME) | |
18a6f04c C |
104 | } |
105 | ||
7fed6375 C |
106 | getIdAndPassAuths () { |
107 | return this.getRegisteredPlugins() | |
ebefc902 C |
108 | .map(p => ({ |
109 | npmName: p.npmName, | |
110 | name: p.name, | |
111 | version: p.version, | |
1896bca0 | 112 | idAndPassAuths: p.registerHelpers.getIdAndPassAuths() |
ebefc902 | 113 | })) |
7fed6375 C |
114 | .filter(v => v.idAndPassAuths.length !== 0) |
115 | } | |
116 | ||
117 | getExternalAuths () { | |
118 | return this.getRegisteredPlugins() | |
ebefc902 C |
119 | .map(p => ({ |
120 | npmName: p.npmName, | |
121 | name: p.name, | |
122 | version: p.version, | |
1896bca0 | 123 | externalAuths: p.registerHelpers.getExternalAuths() |
ebefc902 C |
124 | })) |
125 | .filter(v => v.externalAuths.length !== 0) | |
7fed6375 C |
126 | } |
127 | ||
b5f919ac | 128 | getRegisteredSettings (npmName: string) { |
7fed6375 C |
129 | const result = this.getRegisteredPluginOrTheme(npmName) |
130 | if (!result || result.type !== PluginType.PLUGIN) return [] | |
5e2b2e27 | 131 | |
1896bca0 | 132 | return result.registerHelpers.getSettings() |
5e2b2e27 C |
133 | } |
134 | ||
135 | getRouter (npmName: string) { | |
7fed6375 C |
136 | const result = this.getRegisteredPluginOrTheme(npmName) |
137 | if (!result || result.type !== PluginType.PLUGIN) return null | |
5e2b2e27 | 138 | |
1896bca0 | 139 | return result.registerHelpers.getRouter() |
ad91e700 C |
140 | } |
141 | ||
d75db01f C |
142 | getTranslations (locale: string) { |
143 | return this.translations[locale] || {} | |
144 | } | |
145 | ||
e307e4fc C |
146 | async isTokenValid (token: MOAuthTokenUser, type: 'access' | 'refresh') { |
147 | const auth = this.getAuth(token.User.pluginAuth, token.authName) | |
148 | if (!auth) return true | |
149 | ||
150 | if (auth.hookTokenValidity) { | |
151 | try { | |
152 | const { valid } = await auth.hookTokenValidity({ token, type }) | |
153 | ||
154 | if (valid === false) { | |
155 | logger.info('Rejecting %s token validity from auth %s of plugin %s', type, token.authName, token.User.pluginAuth) | |
156 | } | |
157 | ||
158 | return valid | |
159 | } catch (err) { | |
160 | logger.warn('Cannot run check token validity from auth %s of plugin %s.', token.authName, token.User.pluginAuth, { err }) | |
161 | return true | |
162 | } | |
163 | } | |
164 | ||
165 | return true | |
166 | } | |
167 | ||
a5896799 C |
168 | // ###################### External events ###################### |
169 | ||
74fd2643 | 170 | async onLogout (npmName: string, authName: string, user: MUser, req: express.Request) { |
a5896799 C |
171 | const auth = this.getAuth(npmName, authName) |
172 | ||
173 | if (auth?.onLogout) { | |
174 | logger.info('Running onLogout function from auth %s of plugin %s', authName, npmName) | |
175 | ||
176 | try { | |
74fd2643 C |
177 | // Force await, in case or onLogout returns a promise |
178 | const result = await auth.onLogout(user, req) | |
179 | ||
180 | return typeof result === 'string' | |
181 | ? result | |
182 | : undefined | |
a5896799 C |
183 | } catch (err) { |
184 | logger.warn('Cannot run onLogout function from auth %s of plugin %s.', authName, npmName, { err }) | |
185 | } | |
186 | } | |
74fd2643 C |
187 | |
188 | return undefined | |
a5896799 C |
189 | } |
190 | ||
191 | onSettingsChanged (name: string, settings: any) { | |
192 | const registered = this.getRegisteredPluginByShortName(name) | |
193 | if (!registered) { | |
194 | logger.error('Cannot find plugin %s to call on settings changed.', name) | |
195 | } | |
196 | ||
1896bca0 | 197 | for (const cb of registered.registerHelpers.getOnSettingsChangedCallbacks()) { |
a5896799 C |
198 | try { |
199 | cb(settings) | |
200 | } catch (err) { | |
201 | logger.error('Cannot run on settings changed callback for %s.', registered.npmName, { err }) | |
202 | } | |
203 | } | |
204 | } | |
205 | ||
ad91e700 C |
206 | // ###################### Hooks ###################### |
207 | ||
a1587156 | 208 | async runHook<T> (hookName: ServerHookName, result?: T, params?: any): Promise<T> { |
89cd1275 | 209 | if (!this.hooks[hookName]) return Promise.resolve(result) |
dba85a1e | 210 | |
b4055e1c | 211 | const hookType = getHookType(hookName) |
18a6f04c C |
212 | |
213 | for (const hook of this.hooks[hookName]) { | |
89cd1275 C |
214 | logger.debug('Running hook %s of plugin %s.', hookName, hook.npmName) |
215 | ||
216 | result = await internalRunHook(hook.handler, hookType, result, params, err => { | |
18a6f04c | 217 | logger.error('Cannot run hook %s of plugin %s.', hookName, hook.pluginName, { err }) |
b4055e1c | 218 | }) |
18a6f04c C |
219 | } |
220 | ||
221 | return result | |
222 | } | |
223 | ||
ad91e700 C |
224 | // ###################### Registration ###################### |
225 | ||
226 | async registerPluginsAndThemes () { | |
227 | await this.resetCSSGlobalFile() | |
228 | ||
229 | const plugins = await PluginModel.listEnabledPluginsAndThemes() | |
230 | ||
231 | for (const plugin of plugins) { | |
232 | try { | |
233 | await this.registerPluginOrTheme(plugin) | |
234 | } catch (err) { | |
587568e1 C |
235 | // Try to unregister the plugin |
236 | try { | |
237 | await this.unregister(PluginModel.buildNpmName(plugin.name, plugin.type)) | |
238 | } catch { | |
239 | // we don't care if we cannot unregister it | |
240 | } | |
241 | ||
ad91e700 C |
242 | logger.error('Cannot register plugin %s, skipping.', plugin.name, { err }) |
243 | } | |
244 | } | |
245 | ||
246 | this.sortHooksByPriority() | |
247 | } | |
248 | ||
b5f919ac C |
249 | // Don't need the plugin type since themes cannot register server code |
250 | async unregister (npmName: string) { | |
251 | logger.info('Unregister plugin %s.', npmName) | |
252 | ||
253 | const plugin = this.getRegisteredPluginOrTheme(npmName) | |
345da516 C |
254 | |
255 | if (!plugin) { | |
b5f919ac | 256 | throw new Error(`Unknown plugin ${npmName} to unregister`) |
345da516 C |
257 | } |
258 | ||
60cfd4cb C |
259 | delete this.registeredPlugins[plugin.npmName] |
260 | ||
d75db01f C |
261 | this.deleteTranslations(plugin.npmName) |
262 | ||
b5f919ac C |
263 | if (plugin.type === PluginType.PLUGIN) { |
264 | await plugin.unregister() | |
345da516 | 265 | |
b5f919ac C |
266 | // Remove hooks of this plugin |
267 | for (const key of Object.keys(this.hooks)) { | |
98da1a7b | 268 | this.hooks[key] = this.hooks[key].filter(h => h.npmName !== npmName) |
b5f919ac | 269 | } |
2c053942 | 270 | |
1896bca0 | 271 | const store = plugin.registerHelpers |
5e2b2e27 | 272 | store.reinitVideoConstants(plugin.npmName) |
1896bca0 | 273 | store.reinitTranscodingProfilesAndEncoders(plugin.npmName) |
5e2b2e27 | 274 | |
b5f919ac C |
275 | logger.info('Regenerating registered plugin CSS to global file.') |
276 | await this.regeneratePluginGlobalCSS() | |
2c053942 | 277 | } |
345da516 C |
278 | } |
279 | ||
ad91e700 C |
280 | // ###################### Installation ###################### |
281 | ||
282 | async install (toInstall: string, version?: string, fromDisk = false) { | |
f023a19c | 283 | let plugin: PluginModel |
b5f919ac | 284 | let npmName: string |
f023a19c C |
285 | |
286 | logger.info('Installing plugin %s.', toInstall) | |
287 | ||
288 | try { | |
289 | fromDisk | |
290 | ? await installNpmPluginFromDisk(toInstall) | |
291 | : await installNpmPlugin(toInstall, version) | |
292 | ||
b5f919ac C |
293 | npmName = fromDisk ? basename(toInstall) : toInstall |
294 | const pluginType = PluginModel.getTypeFromNpmName(npmName) | |
295 | const pluginName = PluginModel.normalizePluginName(npmName) | |
f023a19c | 296 | |
09071200 | 297 | const packageJSON = await this.getPackageJSON(pluginName, pluginType) |
9157d598 C |
298 | |
299 | this.sanitizeAndCheckPackageJSONOrThrow(packageJSON, pluginType); | |
f023a19c C |
300 | |
301 | [ plugin ] = await PluginModel.upsert({ | |
302 | name: pluginName, | |
303 | description: packageJSON.description, | |
dba85a1e | 304 | homepage: packageJSON.homepage, |
f023a19c C |
305 | type: pluginType, |
306 | version: packageJSON.version, | |
307 | enabled: true, | |
308 | uninstalled: false, | |
309 | peertubeEngine: packageJSON.engine.peertube | |
310 | }, { returning: true }) | |
311 | } catch (err) { | |
312 | logger.error('Cannot install plugin %s, removing it...', toInstall, { err }) | |
313 | ||
314 | try { | |
b5f919ac | 315 | await removeNpmPlugin(npmName) |
f023a19c C |
316 | } catch (err) { |
317 | logger.error('Cannot remove plugin %s after failed installation.', toInstall, { err }) | |
318 | } | |
319 | ||
320 | throw err | |
321 | } | |
322 | ||
323 | logger.info('Successful installation of plugin %s.', toInstall) | |
324 | ||
325 | await this.registerPluginOrTheme(plugin) | |
b5f919ac C |
326 | |
327 | return plugin | |
328 | } | |
329 | ||
330 | async update (toUpdate: string, version?: string, fromDisk = false) { | |
331 | const npmName = fromDisk ? basename(toUpdate) : toUpdate | |
332 | ||
333 | logger.info('Updating plugin %s.', npmName) | |
334 | ||
335 | // Unregister old hooks | |
336 | await this.unregister(npmName) | |
337 | ||
338 | return this.install(toUpdate, version, fromDisk) | |
f023a19c C |
339 | } |
340 | ||
dba85a1e C |
341 | async uninstall (npmName: string) { |
342 | logger.info('Uninstalling plugin %s.', npmName) | |
2c053942 | 343 | |
2c053942 | 344 | try { |
b5f919ac | 345 | await this.unregister(npmName) |
2c053942 | 346 | } catch (err) { |
b5f919ac | 347 | logger.warn('Cannot unregister plugin %s.', npmName, { err }) |
2c053942 C |
348 | } |
349 | ||
dba85a1e | 350 | const plugin = await PluginModel.loadByNpmName(npmName) |
2c053942 | 351 | if (!plugin || plugin.uninstalled === true) { |
dba85a1e | 352 | logger.error('Cannot uninstall plugin %s: it does not exist or is already uninstalled.', npmName) |
2c053942 C |
353 | return |
354 | } | |
355 | ||
356 | plugin.enabled = false | |
357 | plugin.uninstalled = true | |
358 | ||
359 | await plugin.save() | |
f023a19c | 360 | |
dba85a1e | 361 | await removeNpmPlugin(npmName) |
2c053942 | 362 | |
dba85a1e | 363 | logger.info('Plugin %s uninstalled.', npmName) |
f023a19c C |
364 | } |
365 | ||
ad91e700 C |
366 | // ###################### Private register ###################### |
367 | ||
345da516 | 368 | private async registerPluginOrTheme (plugin: PluginModel) { |
b5f919ac C |
369 | const npmName = PluginModel.buildNpmName(plugin.name, plugin.type) |
370 | ||
371 | logger.info('Registering plugin or theme %s.', npmName) | |
345da516 | 372 | |
09071200 | 373 | const packageJSON = await this.getPackageJSON(plugin.name, plugin.type) |
f023a19c | 374 | const pluginPath = this.getPluginPath(plugin.name, plugin.type) |
345da516 | 375 | |
9157d598 | 376 | this.sanitizeAndCheckPackageJSONOrThrow(packageJSON, plugin.type) |
345da516 C |
377 | |
378 | let library: PluginLibrary | |
1896bca0 | 379 | let registerHelpers: RegisterHelpers |
345da516 | 380 | if (plugin.type === PluginType.PLUGIN) { |
7fed6375 C |
381 | const result = await this.registerPlugin(plugin, pluginPath, packageJSON) |
382 | library = result.library | |
1896bca0 | 383 | registerHelpers = result.registerStore |
345da516 C |
384 | } |
385 | ||
2c053942 C |
386 | const clientScripts: { [id: string]: ClientScript } = {} |
387 | for (const c of packageJSON.clientScripts) { | |
388 | clientScripts[c.script] = c | |
389 | } | |
390 | ||
a1587156 | 391 | this.registeredPlugins[npmName] = { |
b5f919ac | 392 | npmName, |
345da516 C |
393 | name: plugin.name, |
394 | type: plugin.type, | |
395 | version: plugin.version, | |
396 | description: plugin.description, | |
397 | peertubeEngine: plugin.peertubeEngine, | |
398 | path: pluginPath, | |
399 | staticDirs: packageJSON.staticDirs, | |
2c053942 | 400 | clientScripts, |
345da516 | 401 | css: packageJSON.css, |
1896bca0 | 402 | registerHelpers: registerHelpers || undefined, |
345da516 C |
403 | unregister: library ? library.unregister : undefined |
404 | } | |
d75db01f C |
405 | |
406 | await this.addTranslations(plugin, npmName, packageJSON.translations) | |
345da516 C |
407 | } |
408 | ||
409 | private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) { | |
b5f919ac C |
410 | const npmName = PluginModel.buildNpmName(plugin.name, plugin.type) |
411 | ||
09071200 C |
412 | // Delete cache if needed |
413 | const modulePath = join(pluginPath, packageJSON.library) | |
414 | delete require.cache[modulePath] | |
415 | const library: PluginLibrary = require(modulePath) | |
f023a19c | 416 | |
345da516 C |
417 | if (!isLibraryCodeValid(library)) { |
418 | throw new Error('Library code is not valid (miss register or unregister function)') | |
419 | } | |
420 | ||
7fed6375 C |
421 | const { registerOptions, registerStore } = this.getRegisterHelpers(npmName, plugin) |
422 | library.register(registerOptions) | |
32fe0013 | 423 | .catch(err => logger.error('Cannot register plugin %s.', npmName, { err })) |
345da516 | 424 | |
b5f919ac | 425 | logger.info('Add plugin %s CSS to global file.', npmName) |
345da516 C |
426 | |
427 | await this.addCSSToGlobalFile(pluginPath, packageJSON.css) | |
428 | ||
7fed6375 | 429 | return { library, registerStore } |
345da516 C |
430 | } |
431 | ||
d75db01f C |
432 | // ###################### Translations ###################### |
433 | ||
434 | private async addTranslations (plugin: PluginModel, npmName: string, translationPaths: PackagePluginTranslations) { | |
435 | for (const locale of Object.keys(translationPaths)) { | |
436 | const path = translationPaths[locale] | |
437 | const json = await readJSON(join(this.getPluginPath(plugin.name, plugin.type), path)) | |
438 | ||
439 | if (!this.translations[locale]) this.translations[locale] = {} | |
440 | this.translations[locale][npmName] = json | |
441 | ||
442 | logger.info('Added locale %s of plugin %s.', locale, npmName) | |
443 | } | |
444 | } | |
445 | ||
446 | private deleteTranslations (npmName: string) { | |
447 | for (const locale of Object.keys(this.translations)) { | |
448 | delete this.translations[locale][npmName] | |
449 | ||
450 | logger.info('Deleted locale %s of plugin %s.', locale, npmName) | |
451 | } | |
452 | } | |
453 | ||
ad91e700 | 454 | // ###################### CSS ###################### |
345da516 | 455 | |
2c053942 | 456 | private resetCSSGlobalFile () { |
3e753302 C |
457 | ClientHtml.invalidCache() |
458 | ||
2c053942 C |
459 | return outputFile(PLUGIN_GLOBAL_CSS_PATH, '') |
460 | } | |
461 | ||
345da516 C |
462 | private async addCSSToGlobalFile (pluginPath: string, cssRelativePaths: string[]) { |
463 | for (const cssPath of cssRelativePaths) { | |
464 | await this.concatFiles(join(pluginPath, cssPath), PLUGIN_GLOBAL_CSS_PATH) | |
465 | } | |
a8b666e9 C |
466 | |
467 | ClientHtml.invalidCache() | |
345da516 C |
468 | } |
469 | ||
470 | private concatFiles (input: string, output: string) { | |
471 | return new Promise<void>((res, rej) => { | |
2c053942 C |
472 | const inputStream = createReadStream(input) |
473 | const outputStream = createWriteStream(output, { flags: 'a' }) | |
345da516 C |
474 | |
475 | inputStream.pipe(outputStream) | |
476 | ||
477 | inputStream.on('end', () => res()) | |
478 | inputStream.on('error', err => rej(err)) | |
479 | }) | |
480 | } | |
481 | ||
ad91e700 C |
482 | private async regeneratePluginGlobalCSS () { |
483 | await this.resetCSSGlobalFile() | |
484 | ||
1198edf4 | 485 | for (const plugin of this.getRegisteredPlugins()) { |
ad91e700 C |
486 | await this.addCSSToGlobalFile(plugin.path, plugin.css) |
487 | } | |
488 | } | |
489 | ||
490 | // ###################### Utils ###################### | |
491 | ||
492 | private sortHooksByPriority () { | |
493 | for (const hookName of Object.keys(this.hooks)) { | |
494 | this.hooks[hookName].sort((a, b) => { | |
495 | return b.priority - a.priority | |
496 | }) | |
497 | } | |
498 | } | |
499 | ||
f023a19c C |
500 | private getPackageJSON (pluginName: string, pluginType: PluginType) { |
501 | const pluginPath = join(this.getPluginPath(pluginName, pluginType), 'package.json') | |
502 | ||
09071200 | 503 | return readJSON(pluginPath) as Promise<PluginPackageJson> |
f023a19c C |
504 | } |
505 | ||
506 | private getPluginPath (pluginName: string, pluginType: PluginType) { | |
b5f919ac | 507 | const npmName = PluginModel.buildNpmName(pluginName, pluginType) |
f023a19c | 508 | |
b5f919ac | 509 | return join(CONFIG.STORAGE.PLUGINS_DIR, 'node_modules', npmName) |
f023a19c C |
510 | } |
511 | ||
e307e4fc C |
512 | private getAuth (npmName: string, authName: string) { |
513 | const plugin = this.getRegisteredPluginOrTheme(npmName) | |
514 | if (!plugin || plugin.type !== PluginType.PLUGIN) return null | |
515 | ||
1896bca0 C |
516 | let auths: (RegisterServerAuthPassOptions | RegisterServerAuthExternalOptions)[] = plugin.registerHelpers.getIdAndPassAuths() |
517 | auths = auths.concat(plugin.registerHelpers.getExternalAuths()) | |
9107d791 C |
518 | |
519 | return auths.find(a => a.authName === authName) | |
e307e4fc C |
520 | } |
521 | ||
ad91e700 | 522 | // ###################### Private getters ###################### |
2c053942 | 523 | |
7cd4d2ba C |
524 | private getRegisteredPluginsOrThemes (type: PluginType) { |
525 | const plugins: RegisteredPlugin[] = [] | |
526 | ||
b5f919ac | 527 | for (const npmName of Object.keys(this.registeredPlugins)) { |
a1587156 | 528 | const plugin = this.registeredPlugins[npmName] |
7cd4d2ba C |
529 | if (plugin.type !== type) continue |
530 | ||
531 | plugins.push(plugin) | |
532 | } | |
533 | ||
534 | return plugins | |
535 | } | |
536 | ||
32fe0013 C |
537 | // ###################### Generate register helpers ###################### |
538 | ||
7fed6375 C |
539 | private getRegisterHelpers ( |
540 | npmName: string, | |
541 | plugin: PluginModel | |
1896bca0 | 542 | ): { registerStore: RegisterHelpers, registerOptions: RegisterServerOptions } { |
5e2b2e27 | 543 | const onHookAdded = (options: RegisterServerHookOptions) => { |
32fe0013 C |
544 | if (!this.hooks[options.target]) this.hooks[options.target] = [] |
545 | ||
546 | this.hooks[options.target].push({ | |
5e2b2e27 | 547 | npmName: npmName, |
32fe0013 C |
548 | pluginName: plugin.name, |
549 | handler: options.handler, | |
550 | priority: options.priority || 0 | |
551 | }) | |
552 | } | |
553 | ||
1896bca0 | 554 | const registerHelpers = new RegisterHelpers(npmName, plugin, onHookAdded.bind(this)) |
32fe0013 | 555 | |
7fed6375 | 556 | return { |
1896bca0 C |
557 | registerStore: registerHelpers, |
558 | registerOptions: registerHelpers.buildRegisterHelpers() | |
7fed6375 | 559 | } |
ee286591 C |
560 | } |
561 | ||
9157d598 C |
562 | private sanitizeAndCheckPackageJSONOrThrow (packageJSON: PluginPackageJson, pluginType: PluginType) { |
563 | if (!packageJSON.staticDirs) packageJSON.staticDirs = {} | |
564 | if (!packageJSON.css) packageJSON.css = [] | |
565 | if (!packageJSON.clientScripts) packageJSON.clientScripts = [] | |
566 | if (!packageJSON.translations) packageJSON.translations = {} | |
567 | ||
568 | const { result: packageJSONValid, badFields } = isPackageJSONValid(packageJSON, pluginType) | |
569 | if (!packageJSONValid) { | |
570 | const formattedFields = badFields.map(f => `"${f}"`) | |
a1587156 | 571 | .join(', ') |
9157d598 C |
572 | |
573 | throw new Error(`PackageJSON is invalid (invalid fields: ${formattedFields}).`) | |
574 | } | |
575 | } | |
576 | ||
345da516 C |
577 | static get Instance () { |
578 | return this.instance || (this.instance = new this()) | |
579 | } | |
580 | } |