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