From 09071200c73f5358e1d0bfb61a274e4f2c4ec52b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 19 Jul 2019 14:36:04 +0200 Subject: Add plugin API tests --- server/lib/plugins/plugin-index.ts | 2 +- server/lib/plugins/plugin-manager.ts | 13 ++++++++----- server/lib/plugins/yarn.ts | 6 ++++-- 3 files changed, 13 insertions(+), 8 deletions(-) (limited to 'server/lib') diff --git a/server/lib/plugins/plugin-index.ts b/server/lib/plugins/plugin-index.ts index 6b7810618..25b4f3c61 100644 --- a/server/lib/plugins/plugin-index.ts +++ b/server/lib/plugins/plugin-index.ts @@ -21,7 +21,7 @@ async function listAvailablePluginsFromIndex (options: PeertubePluginIndexList) sort, pluginType, search, - currentPeerTubeEngine: PEERTUBE_VERSION + currentPeerTubeEngine: options.currentPeerTubeEngine || PEERTUBE_VERSION } const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins' diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index ac31b06dc..9afda97ea 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts @@ -8,7 +8,7 @@ import { createReadStream, createWriteStream } from 'fs' import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants' import { PluginType } from '../../../shared/models/plugins/plugin.type' import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn' -import { outputFile } from 'fs-extra' +import { outputFile, readJSON } from 'fs-extra' import { RegisterSettingOptions } from '../../../shared/models/plugins/register-setting.model' import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model' import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' @@ -174,7 +174,7 @@ export class PluginManager implements ServerHook { const pluginType = PluginModel.getTypeFromNpmName(npmName) const pluginName = PluginModel.normalizePluginName(npmName) - const packageJSON = this.getPackageJSON(pluginName, pluginType) + const packageJSON = await this.getPackageJSON(pluginName, pluginType) if (!isPackageJSONValid(packageJSON, pluginType)) { throw new Error('PackageJSON is invalid.') } @@ -251,7 +251,7 @@ export class PluginManager implements ServerHook { logger.info('Registering plugin or theme %s.', npmName) - const packageJSON = this.getPackageJSON(plugin.name, plugin.type) + const packageJSON = await this.getPackageJSON(plugin.name, plugin.type) const pluginPath = this.getPluginPath(plugin.name, plugin.type) if (!isPackageJSONValid(packageJSON, plugin.type)) { @@ -286,7 +286,10 @@ export class PluginManager implements ServerHook { private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) { const npmName = PluginModel.buildNpmName(plugin.name, plugin.type) - const library: PluginLibrary = require(join(pluginPath, packageJSON.library)) + // Delete cache if needed + const modulePath = join(pluginPath, packageJSON.library) + delete require.cache[modulePath] + const library: PluginLibrary = require(modulePath) if (!isLibraryCodeValid(library)) { throw new Error('Library code is not valid (miss register or unregister function)') @@ -350,7 +353,7 @@ export class PluginManager implements ServerHook { private getPackageJSON (pluginName: string, pluginType: PluginType) { const pluginPath = join(this.getPluginPath(pluginName, pluginType), 'package.json') - return require(pluginPath) as PluginPackageJson + return readJSON(pluginPath) as Promise } private getPluginPath (pluginName: string, pluginType: PluginType) { diff --git a/server/lib/plugins/yarn.ts b/server/lib/plugins/yarn.ts index 74c67653c..e40351b6e 100644 --- a/server/lib/plugins/yarn.ts +++ b/server/lib/plugins/yarn.ts @@ -13,7 +13,9 @@ async function installNpmPlugin (npmName: string, version?: string) { let toInstall = npmName if (version) toInstall += `@${version}` - await execYarn('add ' + toInstall) + const { stdout } = await execYarn('add ' + toInstall) + + logger.debug('Added a yarn package.', { yarnStdout: stdout }) } async function installNpmPluginFromDisk (path: string) { @@ -46,7 +48,7 @@ async function execYarn (command: string) { await outputJSON(pluginPackageJSON, {}) } - await execShell(`yarn ${command}`, { cwd: pluginDirectory }) + return execShell(`yarn ${command}`, { cwd: pluginDirectory }) } catch (result) { logger.error('Cannot exec yarn.', { command, err: result.err, stderr: result.stderr }) -- cgit v1.2.3