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