]>
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' |
5e2b2e27 | 23 | import { RegisterHelpersStore } from './register-helpers-store' |
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 | |
7fed6375 | 43 | registerHelpersStore?: RegisterHelpersStore |
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, | |
112 | idAndPassAuths: p.registerHelpersStore.getIdAndPassAuths() | |
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, | |
123 | externalAuths: p.registerHelpersStore.getExternalAuths() | |
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 | |
7fed6375 | 132 | return result.registerHelpersStore.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 | |
7fed6375 | 139 | return result.registerHelpersStore.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 | ||
197 | for (const cb of registered.registerHelpersStore.getOnSettingsChangedCallbacks()) { | |
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 | |
7fed6375 | 271 | const store = plugin.registerHelpersStore |
5e2b2e27 C |
272 | store.reinitVideoConstants(plugin.npmName) |
273 | ||
b5f919ac C |
274 | logger.info('Regenerating registered plugin CSS to global file.') |
275 | await this.regeneratePluginGlobalCSS() | |
2c053942 | 276 | } |
345da516 C |
277 | } |
278 | ||
ad91e700 C |
279 | // ###################### Installation ###################### |
280 | ||
281 | async install (toInstall: string, version?: string, fromDisk = false) { | |
f023a19c | 282 | let plugin: PluginModel |
b5f919ac | 283 | let npmName: string |
f023a19c C |
284 | |
285 | logger.info('Installing plugin %s.', toInstall) | |
286 | ||
287 | try { | |
288 | fromDisk | |
289 | ? await installNpmPluginFromDisk(toInstall) | |
290 | : await installNpmPlugin(toInstall, version) | |
291 | ||
b5f919ac C |
292 | npmName = fromDisk ? basename(toInstall) : toInstall |
293 | const pluginType = PluginModel.getTypeFromNpmName(npmName) | |
294 | const pluginName = PluginModel.normalizePluginName(npmName) | |
f023a19c | 295 | |
09071200 | 296 | const packageJSON = await this.getPackageJSON(pluginName, pluginType) |
9157d598 C |
297 | |
298 | this.sanitizeAndCheckPackageJSONOrThrow(packageJSON, pluginType); | |
f023a19c C |
299 | |
300 | [ plugin ] = await PluginModel.upsert({ | |
301 | name: pluginName, | |
302 | description: packageJSON.description, | |
dba85a1e | 303 | homepage: packageJSON.homepage, |
f023a19c C |
304 | type: pluginType, |
305 | version: packageJSON.version, | |
306 | enabled: true, | |
307 | uninstalled: false, | |
308 | peertubeEngine: packageJSON.engine.peertube | |
309 | }, { returning: true }) | |
310 | } catch (err) { | |
311 | logger.error('Cannot install plugin %s, removing it...', toInstall, { err }) | |
312 | ||
313 | try { | |
b5f919ac | 314 | await removeNpmPlugin(npmName) |
f023a19c C |
315 | } catch (err) { |
316 | logger.error('Cannot remove plugin %s after failed installation.', toInstall, { err }) | |
317 | } | |
318 | ||
319 | throw err | |
320 | } | |
321 | ||
322 | logger.info('Successful installation of plugin %s.', toInstall) | |
323 | ||
324 | await this.registerPluginOrTheme(plugin) | |
b5f919ac C |
325 | |
326 | return plugin | |
327 | } | |
328 | ||
329 | async update (toUpdate: string, version?: string, fromDisk = false) { | |
330 | const npmName = fromDisk ? basename(toUpdate) : toUpdate | |
331 | ||
332 | logger.info('Updating plugin %s.', npmName) | |
333 | ||
334 | // Unregister old hooks | |
335 | await this.unregister(npmName) | |
336 | ||
337 | return this.install(toUpdate, version, fromDisk) | |
f023a19c C |
338 | } |
339 | ||
dba85a1e C |
340 | async uninstall (npmName: string) { |
341 | logger.info('Uninstalling plugin %s.', npmName) | |
2c053942 | 342 | |
2c053942 | 343 | try { |
b5f919ac | 344 | await this.unregister(npmName) |
2c053942 | 345 | } catch (err) { |
b5f919ac | 346 | logger.warn('Cannot unregister plugin %s.', npmName, { err }) |
2c053942 C |
347 | } |
348 | ||
dba85a1e | 349 | const plugin = await PluginModel.loadByNpmName(npmName) |
2c053942 | 350 | if (!plugin || plugin.uninstalled === true) { |
dba85a1e | 351 | logger.error('Cannot uninstall plugin %s: it does not exist or is already uninstalled.', npmName) |
2c053942 C |
352 | return |
353 | } | |
354 | ||
355 | plugin.enabled = false | |
356 | plugin.uninstalled = true | |
357 | ||
358 | await plugin.save() | |
f023a19c | 359 | |
dba85a1e | 360 | await removeNpmPlugin(npmName) |
2c053942 | 361 | |
dba85a1e | 362 | logger.info('Plugin %s uninstalled.', npmName) |
f023a19c C |
363 | } |
364 | ||
ad91e700 C |
365 | // ###################### Private register ###################### |
366 | ||
345da516 | 367 | private async registerPluginOrTheme (plugin: PluginModel) { |
b5f919ac C |
368 | const npmName = PluginModel.buildNpmName(plugin.name, plugin.type) |
369 | ||
370 | logger.info('Registering plugin or theme %s.', npmName) | |
345da516 | 371 | |
09071200 | 372 | const packageJSON = await this.getPackageJSON(plugin.name, plugin.type) |
f023a19c | 373 | const pluginPath = this.getPluginPath(plugin.name, plugin.type) |
345da516 | 374 | |
9157d598 | 375 | this.sanitizeAndCheckPackageJSONOrThrow(packageJSON, plugin.type) |
345da516 C |
376 | |
377 | let library: PluginLibrary | |
7fed6375 | 378 | let registerHelpersStore: RegisterHelpersStore |
345da516 | 379 | if (plugin.type === PluginType.PLUGIN) { |
7fed6375 C |
380 | const result = await this.registerPlugin(plugin, pluginPath, packageJSON) |
381 | library = result.library | |
382 | registerHelpersStore = result.registerStore | |
345da516 C |
383 | } |
384 | ||
2c053942 C |
385 | const clientScripts: { [id: string]: ClientScript } = {} |
386 | for (const c of packageJSON.clientScripts) { | |
387 | clientScripts[c.script] = c | |
388 | } | |
389 | ||
a1587156 | 390 | this.registeredPlugins[npmName] = { |
b5f919ac | 391 | npmName, |
345da516 C |
392 | name: plugin.name, |
393 | type: plugin.type, | |
394 | version: plugin.version, | |
395 | description: plugin.description, | |
396 | peertubeEngine: plugin.peertubeEngine, | |
397 | path: pluginPath, | |
398 | staticDirs: packageJSON.staticDirs, | |
2c053942 | 399 | clientScripts, |
345da516 | 400 | css: packageJSON.css, |
7fed6375 | 401 | registerHelpersStore: registerHelpersStore || undefined, |
345da516 C |
402 | unregister: library ? library.unregister : undefined |
403 | } | |
d75db01f C |
404 | |
405 | await this.addTranslations(plugin, npmName, packageJSON.translations) | |
345da516 C |
406 | } |
407 | ||
408 | private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) { | |
b5f919ac C |
409 | const npmName = PluginModel.buildNpmName(plugin.name, plugin.type) |
410 | ||
09071200 C |
411 | // Delete cache if needed |
412 | const modulePath = join(pluginPath, packageJSON.library) | |
413 | delete require.cache[modulePath] | |
414 | const library: PluginLibrary = require(modulePath) | |
f023a19c | 415 | |
345da516 C |
416 | if (!isLibraryCodeValid(library)) { |
417 | throw new Error('Library code is not valid (miss register or unregister function)') | |
418 | } | |
419 | ||
7fed6375 C |
420 | const { registerOptions, registerStore } = this.getRegisterHelpers(npmName, plugin) |
421 | library.register(registerOptions) | |
32fe0013 | 422 | .catch(err => logger.error('Cannot register plugin %s.', npmName, { err })) |
345da516 | 423 | |
b5f919ac | 424 | logger.info('Add plugin %s CSS to global file.', npmName) |
345da516 C |
425 | |
426 | await this.addCSSToGlobalFile(pluginPath, packageJSON.css) | |
427 | ||
7fed6375 | 428 | return { library, registerStore } |
345da516 C |
429 | } |
430 | ||
d75db01f C |
431 | // ###################### Translations ###################### |
432 | ||
433 | private async addTranslations (plugin: PluginModel, npmName: string, translationPaths: PackagePluginTranslations) { | |
434 | for (const locale of Object.keys(translationPaths)) { | |
435 | const path = translationPaths[locale] | |
436 | const json = await readJSON(join(this.getPluginPath(plugin.name, plugin.type), path)) | |
437 | ||
438 | if (!this.translations[locale]) this.translations[locale] = {} | |
439 | this.translations[locale][npmName] = json | |
440 | ||
441 | logger.info('Added locale %s of plugin %s.', locale, npmName) | |
442 | } | |
443 | } | |
444 | ||
445 | private deleteTranslations (npmName: string) { | |
446 | for (const locale of Object.keys(this.translations)) { | |
447 | delete this.translations[locale][npmName] | |
448 | ||
449 | logger.info('Deleted locale %s of plugin %s.', locale, npmName) | |
450 | } | |
451 | } | |
452 | ||
ad91e700 | 453 | // ###################### CSS ###################### |
345da516 | 454 | |
2c053942 | 455 | private resetCSSGlobalFile () { |
3e753302 C |
456 | ClientHtml.invalidCache() |
457 | ||
2c053942 C |
458 | return outputFile(PLUGIN_GLOBAL_CSS_PATH, '') |
459 | } | |
460 | ||
345da516 C |
461 | private async addCSSToGlobalFile (pluginPath: string, cssRelativePaths: string[]) { |
462 | for (const cssPath of cssRelativePaths) { | |
463 | await this.concatFiles(join(pluginPath, cssPath), PLUGIN_GLOBAL_CSS_PATH) | |
464 | } | |
a8b666e9 C |
465 | |
466 | ClientHtml.invalidCache() | |
345da516 C |
467 | } |
468 | ||
469 | private concatFiles (input: string, output: string) { | |
470 | return new Promise<void>((res, rej) => { | |
2c053942 C |
471 | const inputStream = createReadStream(input) |
472 | const outputStream = createWriteStream(output, { flags: 'a' }) | |
345da516 C |
473 | |
474 | inputStream.pipe(outputStream) | |
475 | ||
476 | inputStream.on('end', () => res()) | |
477 | inputStream.on('error', err => rej(err)) | |
478 | }) | |
479 | } | |
480 | ||
ad91e700 C |
481 | private async regeneratePluginGlobalCSS () { |
482 | await this.resetCSSGlobalFile() | |
483 | ||
1198edf4 | 484 | for (const plugin of this.getRegisteredPlugins()) { |
ad91e700 C |
485 | await this.addCSSToGlobalFile(plugin.path, plugin.css) |
486 | } | |
487 | } | |
488 | ||
489 | // ###################### Utils ###################### | |
490 | ||
491 | private sortHooksByPriority () { | |
492 | for (const hookName of Object.keys(this.hooks)) { | |
493 | this.hooks[hookName].sort((a, b) => { | |
494 | return b.priority - a.priority | |
495 | }) | |
496 | } | |
497 | } | |
498 | ||
f023a19c C |
499 | private getPackageJSON (pluginName: string, pluginType: PluginType) { |
500 | const pluginPath = join(this.getPluginPath(pluginName, pluginType), 'package.json') | |
501 | ||
09071200 | 502 | return readJSON(pluginPath) as Promise<PluginPackageJson> |
f023a19c C |
503 | } |
504 | ||
505 | private getPluginPath (pluginName: string, pluginType: PluginType) { | |
b5f919ac | 506 | const npmName = PluginModel.buildNpmName(pluginName, pluginType) |
f023a19c | 507 | |
b5f919ac | 508 | return join(CONFIG.STORAGE.PLUGINS_DIR, 'node_modules', npmName) |
f023a19c C |
509 | } |
510 | ||
e307e4fc C |
511 | private getAuth (npmName: string, authName: string) { |
512 | const plugin = this.getRegisteredPluginOrTheme(npmName) | |
513 | if (!plugin || plugin.type !== PluginType.PLUGIN) return null | |
514 | ||
9107d791 C |
515 | let auths: (RegisterServerAuthPassOptions | RegisterServerAuthExternalOptions)[] = plugin.registerHelpersStore.getIdAndPassAuths() |
516 | auths = auths.concat(plugin.registerHelpersStore.getExternalAuths()) | |
517 | ||
518 | return auths.find(a => a.authName === authName) | |
e307e4fc C |
519 | } |
520 | ||
ad91e700 | 521 | // ###################### Private getters ###################### |
2c053942 | 522 | |
7cd4d2ba C |
523 | private getRegisteredPluginsOrThemes (type: PluginType) { |
524 | const plugins: RegisteredPlugin[] = [] | |
525 | ||
b5f919ac | 526 | for (const npmName of Object.keys(this.registeredPlugins)) { |
a1587156 | 527 | const plugin = this.registeredPlugins[npmName] |
7cd4d2ba C |
528 | if (plugin.type !== type) continue |
529 | ||
530 | plugins.push(plugin) | |
531 | } | |
532 | ||
533 | return plugins | |
534 | } | |
535 | ||
32fe0013 C |
536 | // ###################### Generate register helpers ###################### |
537 | ||
7fed6375 C |
538 | private getRegisterHelpers ( |
539 | npmName: string, | |
540 | plugin: PluginModel | |
541 | ): { registerStore: RegisterHelpersStore, registerOptions: RegisterServerOptions } { | |
5e2b2e27 | 542 | const onHookAdded = (options: RegisterServerHookOptions) => { |
32fe0013 C |
543 | if (!this.hooks[options.target]) this.hooks[options.target] = [] |
544 | ||
545 | this.hooks[options.target].push({ | |
5e2b2e27 | 546 | npmName: npmName, |
32fe0013 C |
547 | pluginName: plugin.name, |
548 | handler: options.handler, | |
549 | priority: options.priority || 0 | |
550 | }) | |
551 | } | |
552 | ||
5e2b2e27 | 553 | const registerHelpersStore = new RegisterHelpersStore(npmName, plugin, onHookAdded.bind(this)) |
32fe0013 | 554 | |
7fed6375 C |
555 | return { |
556 | registerStore: registerHelpersStore, | |
557 | registerOptions: registerHelpersStore.buildRegisterHelpers() | |
558 | } | |
ee286591 C |
559 | } |
560 | ||
9157d598 C |
561 | private sanitizeAndCheckPackageJSONOrThrow (packageJSON: PluginPackageJson, pluginType: PluginType) { |
562 | if (!packageJSON.staticDirs) packageJSON.staticDirs = {} | |
563 | if (!packageJSON.css) packageJSON.css = [] | |
564 | if (!packageJSON.clientScripts) packageJSON.clientScripts = [] | |
565 | if (!packageJSON.translations) packageJSON.translations = {} | |
566 | ||
567 | const { result: packageJSONValid, badFields } = isPackageJSONValid(packageJSON, pluginType) | |
568 | if (!packageJSONValid) { | |
569 | const formattedFields = badFields.map(f => `"${f}"`) | |
a1587156 | 570 | .join(', ') |
9157d598 C |
571 | |
572 | throw new Error(`PackageJSON is invalid (invalid fields: ${formattedFields}).`) | |
573 | } | |
574 | } | |
575 | ||
345da516 C |
576 | static get Instance () { |
577 | return this.instance || (this.instance = new this()) | |
578 | } | |
579 | } |