diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-18 16:43:41 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | 32fe00138990627749da58ff9f845584013aa219 (patch) | |
tree | 3b9cc791614e87cf07bb62c8ef7a40690580ac39 /server/lib | |
parent | 60cfd4cb54138e7da76054aa163b33b3223b17ef (diff) | |
download | PeerTube-32fe00138990627749da58ff9f845584013aa219.tar.gz PeerTube-32fe00138990627749da58ff9f845584013aa219.tar.zst PeerTube-32fe00138990627749da58ff9f845584013aa219.zip |
Provide logger to plugins
Diffstat (limited to 'server/lib')
-rw-r--r-- | server/lib/plugins/plugin-manager.ts | 90 |
1 files changed, 51 insertions, 39 deletions
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index e76dbb53e..ac31b06dc 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts | |||
@@ -4,7 +4,6 @@ import { basename, join } from 'path' | |||
4 | import { CONFIG } from '../../initializers/config' | 4 | import { CONFIG } from '../../initializers/config' |
5 | import { isLibraryCodeValid, isPackageJSONValid } from '../../helpers/custom-validators/plugins' | 5 | import { isLibraryCodeValid, isPackageJSONValid } from '../../helpers/custom-validators/plugins' |
6 | import { ClientScript, PluginPackageJson } from '../../../shared/models/plugins/plugin-package-json.model' | 6 | import { ClientScript, PluginPackageJson } from '../../../shared/models/plugins/plugin-package-json.model' |
7 | import { PluginLibrary } from '../../../shared/models/plugins/plugin-library.model' | ||
8 | import { createReadStream, createWriteStream } from 'fs' | 7 | import { createReadStream, createWriteStream } from 'fs' |
9 | import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants' | 8 | import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants' |
10 | import { PluginType } from '../../../shared/models/plugins/plugin.type' | 9 | import { PluginType } from '../../../shared/models/plugins/plugin.type' |
@@ -14,10 +13,10 @@ import { RegisterSettingOptions } from '../../../shared/models/plugins/register- | |||
14 | import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model' | 13 | import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model' |
15 | import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' | 14 | import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' |
16 | import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model' | 15 | import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model' |
17 | import { ServerHookName, ServerHook } from '../../../shared/models/plugins/server-hook.model' | 16 | import { ServerHook, ServerHookName } from '../../../shared/models/plugins/server-hook.model' |
18 | import { isCatchable, isPromise } from '../../../shared/core-utils/miscs/miscs' | ||
19 | import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks' | 17 | import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks' |
20 | import { HookType } from '../../../shared/models/plugins/hook-type.enum' | 18 | import { RegisterOptions } from '../../typings/plugins/register-options.model' |
19 | import { PluginLibrary } from '../../typings/plugins' | ||
21 | 20 | ||
22 | export interface RegisteredPlugin { | 21 | export interface RegisteredPlugin { |
23 | npmName: string | 22 | npmName: string |
@@ -287,47 +286,15 @@ export class PluginManager implements ServerHook { | |||
287 | private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) { | 286 | private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) { |
288 | const npmName = PluginModel.buildNpmName(plugin.name, plugin.type) | 287 | const npmName = PluginModel.buildNpmName(plugin.name, plugin.type) |
289 | 288 | ||
290 | const registerHook = (options: RegisterHookOptions) => { | ||
291 | if (!this.hooks[options.target]) this.hooks[options.target] = [] | ||
292 | |||
293 | this.hooks[options.target].push({ | ||
294 | npmName, | ||
295 | pluginName: plugin.name, | ||
296 | handler: options.handler, | ||
297 | priority: options.priority || 0 | ||
298 | }) | ||
299 | } | ||
300 | |||
301 | const registerSetting = (options: RegisterSettingOptions) => { | ||
302 | if (!this.settings[npmName]) this.settings[npmName] = [] | ||
303 | |||
304 | this.settings[npmName].push(options) | ||
305 | } | ||
306 | |||
307 | const settingsManager: PluginSettingsManager = { | ||
308 | getSetting: (name: string) => PluginModel.getSetting(plugin.name, plugin.type, name), | ||
309 | |||
310 | setSetting: (name: string, value: string) => PluginModel.setSetting(plugin.name, plugin.type, name, value) | ||
311 | } | ||
312 | |||
313 | const storageManager: PluginStorageManager = { | ||
314 | getData: (key: string) => PluginModel.getData(plugin.name, plugin.type, key), | ||
315 | |||
316 | storeData: (key: string, data: any) => PluginModel.storeData(plugin.name, plugin.type, key, data) | ||
317 | } | ||
318 | |||
319 | const library: PluginLibrary = require(join(pluginPath, packageJSON.library)) | 289 | const library: PluginLibrary = require(join(pluginPath, packageJSON.library)) |
320 | 290 | ||
321 | if (!isLibraryCodeValid(library)) { | 291 | if (!isLibraryCodeValid(library)) { |
322 | throw new Error('Library code is not valid (miss register or unregister function)') | 292 | throw new Error('Library code is not valid (miss register or unregister function)') |
323 | } | 293 | } |
324 | 294 | ||
325 | library.register({ | 295 | const registerHelpers = this.getRegisterHelpers(npmName, plugin) |
326 | registerHook, | 296 | library.register(registerHelpers) |
327 | registerSetting, | 297 | .catch(err => logger.error('Cannot register plugin %s.', npmName, { err })) |
328 | settingsManager, | ||
329 | storageManager | ||
330 | }).catch(err => logger.error('Cannot register plugin %s.', npmName, { err })) | ||
331 | 298 | ||
332 | logger.info('Add plugin %s CSS to global file.', npmName) | 299 | logger.info('Add plugin %s CSS to global file.', npmName) |
333 | 300 | ||
@@ -407,6 +374,51 @@ export class PluginManager implements ServerHook { | |||
407 | return plugins | 374 | return plugins |
408 | } | 375 | } |
409 | 376 | ||
377 | // ###################### Generate register helpers ###################### | ||
378 | |||
379 | private getRegisterHelpers (npmName: string, plugin: PluginModel): RegisterOptions { | ||
380 | const registerHook = (options: RegisterHookOptions) => { | ||
381 | if (!this.hooks[options.target]) this.hooks[options.target] = [] | ||
382 | |||
383 | this.hooks[options.target].push({ | ||
384 | npmName, | ||
385 | pluginName: plugin.name, | ||
386 | handler: options.handler, | ||
387 | priority: options.priority || 0 | ||
388 | }) | ||
389 | } | ||
390 | |||
391 | const registerSetting = (options: RegisterSettingOptions) => { | ||
392 | if (!this.settings[npmName]) this.settings[npmName] = [] | ||
393 | |||
394 | this.settings[npmName].push(options) | ||
395 | } | ||
396 | |||
397 | const settingsManager: PluginSettingsManager = { | ||
398 | getSetting: (name: string) => PluginModel.getSetting(plugin.name, plugin.type, name), | ||
399 | |||
400 | setSetting: (name: string, value: string) => PluginModel.setSetting(plugin.name, plugin.type, name, value) | ||
401 | } | ||
402 | |||
403 | const storageManager: PluginStorageManager = { | ||
404 | getData: (key: string) => PluginModel.getData(plugin.name, plugin.type, key), | ||
405 | |||
406 | storeData: (key: string, data: any) => PluginModel.storeData(plugin.name, plugin.type, key, data) | ||
407 | } | ||
408 | |||
409 | const peertubeHelpers = { | ||
410 | logger | ||
411 | } | ||
412 | |||
413 | return { | ||
414 | registerHook, | ||
415 | registerSetting, | ||
416 | settingsManager, | ||
417 | storageManager, | ||
418 | peertubeHelpers | ||
419 | } | ||
420 | } | ||
421 | |||
410 | static get Instance () { | 422 | static get Instance () { |
411 | return this.instance || (this.instance = new this()) | 423 | return this.instance || (this.instance = new this()) |
412 | } | 424 | } |