From b5f919ac8eb2a1c20e26582fdfd377d687710d8f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 12 Jul 2019 11:39:58 +0200 Subject: WIP plugins: update plugin --- server/controllers/api/plugins.ts | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'server/controllers/api') diff --git a/server/controllers/api/plugins.ts b/server/controllers/api/plugins.ts index 8e59f27cf..14675fdf3 100644 --- a/server/controllers/api/plugins.ts +++ b/server/controllers/api/plugins.ts @@ -13,13 +13,13 @@ import { PluginModel } from '../../models/server/plugin' import { UserRight } from '../../../shared/models/users' import { existingPluginValidator, - installPluginValidator, + installOrUpdatePluginValidator, listPluginsValidator, uninstallPluginValidator, updatePluginSettingsValidator } from '../../middlewares/validators/plugins' import { PluginManager } from '../../lib/plugins/plugin-manager' -import { InstallPlugin } from '../../../shared/models/plugins/install-plugin.model' +import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/install-plugin.model' import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model' import { logger } from '../../helpers/logger' @@ -61,10 +61,17 @@ pluginRouter.put('/:npmName/settings', pluginRouter.post('/install', authenticate, ensureUserHasRight(UserRight.MANAGE_PLUGINS), - installPluginValidator, + installOrUpdatePluginValidator, asyncMiddleware(installPlugin) ) +pluginRouter.post('/update', + authenticate, + ensureUserHasRight(UserRight.MANAGE_PLUGINS), + installOrUpdatePluginValidator, + asyncMiddleware(updatePlugin) +) + pluginRouter.post('/uninstall', authenticate, ensureUserHasRight(UserRight.MANAGE_PLUGINS), @@ -100,18 +107,33 @@ function getPlugin (req: express.Request, res: express.Response) { } async function installPlugin (req: express.Request, res: express.Response) { - const body: InstallPlugin = req.body + const body: InstallOrUpdatePlugin = req.body const fromDisk = !!body.path const toInstall = body.npmName || body.path try { - await PluginManager.Instance.install(toInstall, undefined, fromDisk) + const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk) + + return res.json(plugin.toFormattedJSON()) } catch (err) { logger.warn('Cannot install plugin %s.', toInstall, { err }) return res.sendStatus(400) } +} - return res.sendStatus(204) +async function updatePlugin (req: express.Request, res: express.Response) { + const body: InstallOrUpdatePlugin = req.body + + const fromDisk = !!body.path + const toUpdate = body.npmName || body.path + try { + const plugin = await PluginManager.Instance.update(toUpdate, undefined, fromDisk) + + return res.json(plugin.toFormattedJSON()) + } catch (err) { + logger.warn('Cannot update plugin %s.', toUpdate, { err }) + return res.sendStatus(400) + } } async function uninstallPlugin (req: express.Request, res: express.Response) { @@ -123,9 +145,7 @@ async function uninstallPlugin (req: express.Request, res: express.Response) { } function getPluginRegisteredSettings (req: express.Request, res: express.Response) { - const plugin = res.locals.plugin - - const settings = PluginManager.Instance.getSettings(plugin.name) + const settings = PluginManager.Instance.getRegisteredSettings(req.params.npmName) return res.json({ settings -- cgit v1.2.3