From 6254bbd6e3154ff7be7abf441598947897c2be05 Mon Sep 17 00:00:00 2001 From: Ben Surgison Date: Fri, 13 Oct 2023 11:53:11 +0100 Subject: [PATCH] Add latest version link to plugin details page to display latest plugin details --- lib/manage-prototype-handlers.js | 19 +++++++++++++------ lib/manage-prototype-routes.js | 1 + .../views/manage-prototype/plugin-details.njk | 7 ++++++- lib/plugins/packages.js | 8 ++++++-- lib/plugins/packages.spec.js | 9 +++++++++ 5 files changed, 35 insertions(+), 9 deletions(-) diff --git a/lib/manage-prototype-handlers.js b/lib/manage-prototype-handlers.js index 9f7a8503be..47f6b16cec 100644 --- a/lib/manage-prototype-handlers.js +++ b/lib/manage-prototype-handlers.js @@ -404,7 +404,7 @@ function getTemplatesPostInstallHandler (req, res) { })) } -function buildPluginData (pluginData) { +function buildPluginData (pluginData, isLatest) { if (pluginData === undefined) { return } @@ -417,12 +417,14 @@ function buildPluginData (pluginData) { installedVersion, required, localVersion, - pluginConfig = {} + pluginConfig = {}, + latestPluginConfig } = pluginData + const meta = isLatest ? latestPluginConfig?.meta || pluginConfig?.meta : pluginConfig?.meta const preparedPackageNameForDisplay = plugins.preparePackageNameForDisplay(packageName) return { ...preparedPackageNameForDisplay, - ...pluginConfig.meta, + ...meta, packageName, latestVersion, installedLocally, @@ -433,7 +435,7 @@ function buildPluginData (pluginData) { uninstallLink: installed && !required ? `${contextPath}/plugins/uninstall?package=${encodeURIComponent(packageName)}${installedLocally ? `&version=${encodeURIComponent(localVersion)}` : ''}` : undefined, uninstallCommand: `npm uninstall ${packageName}`, installedVersion, - inThisPlugin: getInThisPluginDetails(pluginConfig) + inThisPlugin: getInThisPluginDetails(isLatest ? latestPluginConfig || pluginConfig : pluginConfig) } } @@ -764,13 +766,18 @@ async function postPluginsModeHandler (req, res) { async function getPluginDetailsHandler (req, res) { const packageName = req.query.package + const isLatest = req.route.path.split('/').pop() === 'latest' + const latestLink = isLatest ? '' : req.originalUrl.replace('?', '/latest?') + const installedLink = isLatest ? req.originalUrl.replace('/latest?', '?') : '' const plugin = await lookupPackageInfo(packageName) - const { name, scope, installedVersion, latestVersion, ...pluginData } = buildPluginData(plugin) + const { name, scope, installedVersion, latestVersion, ...pluginData } = buildPluginData(plugin, isLatest) const viewData = { ...pluginData, installedVersion, latestVersion, - plugin: { name, scope, version: installedVersion || latestVersion } + latestLink, + installedLink, + plugin: { name, scope, version: latestLink ? installedVersion || latestVersion : latestVersion } } res.render(getManagementView('plugin-details.njk'), viewData) } diff --git a/lib/manage-prototype-routes.js b/lib/manage-prototype-routes.js index e3147d1880..5841933aee 100644 --- a/lib/manage-prototype-routes.js +++ b/lib/manage-prototype-routes.js @@ -80,6 +80,7 @@ router.post('/plugins/:mode', postPluginsModeMiddleware) router.post('/plugins/:mode', csrfProtection, postPluginsModeHandler) +router.get('/plugin-details/latest', getPluginDetailsHandler) router.get('/plugin-details', getPluginDetailsHandler) // Find GOV.UK Frontend (via internal package, project fallback) diff --git a/lib/nunjucks/views/manage-prototype/plugin-details.njk b/lib/nunjucks/views/manage-prototype/plugin-details.njk index 5445adc355..c9cad76965 100644 --- a/lib/nunjucks/views/manage-prototype/plugin-details.njk +++ b/lib/nunjucks/views/manage-prototype/plugin-details.njk @@ -14,7 +14,12 @@
{% if updateLink %} -

Latest version: {{ latestVersion }}

+ {% if latestLink %} +

Latest version: {{ latestVersion }}

+ {% endif %} + {% if installedLink %} +

Installed version: {{ installedVersion }}

+ {% endif %} {% endif %}
diff --git a/lib/plugins/packages.js b/lib/plugins/packages.js index 95b80327b8..d545036f98 100644 --- a/lib/plugins/packages.js +++ b/lib/plugins/packages.js @@ -89,14 +89,16 @@ async function refreshPackageInfo (packageName, version) { const installedLocally = installedPackageVersion?.startsWith('file:') const installedFromGithub = installedPackageVersion?.startsWith('github:') const installedVersion = installed ? packageJson?.version : undefined + const latestPackageJson = registryInfo?.versions ? registryInfo?.versions[latestVersion] : undefined + const latestPluginConfig = registryInfo ? await getConfigForPackage(packageName) : undefined let localVersion if (!installed) { // Retrieve the packageJson and pluginConfig from the registry if possible if (registryInfo) { - packageJson = registryInfo?.versions ? registryInfo?.versions[latestVersion] : undefined - pluginConfig = await getConfigForPackage(packageName) + packageJson = latestPackageJson + pluginConfig = latestPluginConfig } else if (version) { packageJson = await readJson(path.join(path.relative(projectDir, version), 'package.json')) pluginConfig = await readJson(path.join(path.relative(projectDir, version), 'govuk-prototype-kit.config.json')) @@ -135,6 +137,8 @@ async function refreshPackageInfo (packageName, version) { packageJson, pluginConfig, pluginDependencies, + latestPluginConfig, + latestPackageJson, localVersion, updateAvailable, installedPackageVersion diff --git a/lib/plugins/packages.spec.js b/lib/plugins/packages.spec.js index 87ab98a9a9..f5487c3273 100644 --- a/lib/plugins/packages.spec.js +++ b/lib/plugins/packages.spec.js @@ -190,6 +190,9 @@ describe('packages', () => { local: true, version: '1.0.0' }, + latestPackageJson: { + version: '1.0.1' + }, pluginConfig: { loaded: true }, @@ -216,6 +219,9 @@ describe('packages', () => { packageJson: { version: '1.0.0' }, + latestPackageJson: { + version: '1.0.0' + }, versions: [ '1.0.0' ] @@ -238,6 +244,9 @@ describe('packages', () => { packageJson: { version: '2.0.0' }, + latestPackageJson: { + version: '2.0.0' + }, pluginConfig: { assets: [ '/dist'