Skip to content

Commit

Permalink
Add latest version link to plugin details page to display latest plug…
Browse files Browse the repository at this point in the history
…in details
  • Loading branch information
BenSurgisonGDS committed Oct 13, 2023
1 parent b807740 commit 6254bbd
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
19 changes: 13 additions & 6 deletions lib/manage-prototype-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ function getTemplatesPostInstallHandler (req, res) {
}))
}

function buildPluginData (pluginData) {
function buildPluginData (pluginData, isLatest) {
if (pluginData === undefined) {
return
}
Expand All @@ -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,
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions lib/manage-prototype-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion lib/nunjucks/views/manage-prototype/plugin-details.njk
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
<div class="govuk-grid-column-one-quarter">

{% if updateLink %}
<p>Latest version: {{ latestVersion }}</p>
{% if latestLink %}
<p><a href="{{ latestLink }}">Latest version: {{ latestVersion }}</a></p>
{% endif %}
{% if installedLink %}
<p><a href="{{ installedLink }}">Installed version: {{ installedVersion }}</a></p>
{% endif %}
{% endif %}

<div class="govuk-prototype-kit-manage-prototype-plugin-list-plugin-list__item-buttons">
Expand Down
8 changes: 6 additions & 2 deletions lib/plugins/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -135,6 +137,8 @@ async function refreshPackageInfo (packageName, version) {
packageJson,
pluginConfig,
pluginDependencies,
latestPluginConfig,
latestPackageJson,
localVersion,
updateAvailable,
installedPackageVersion
Expand Down
9 changes: 9 additions & 0 deletions lib/plugins/packages.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ describe('packages', () => {
local: true,
version: '1.0.0'
},
latestPackageJson: {
version: '1.0.1'
},
pluginConfig: {
loaded: true
},
Expand All @@ -216,6 +219,9 @@ describe('packages', () => {
packageJson: {
version: '1.0.0'
},
latestPackageJson: {
version: '1.0.0'
},
versions: [
'1.0.0'
]
Expand All @@ -238,6 +244,9 @@ describe('packages', () => {
packageJson: {
version: '2.0.0'
},
latestPackageJson: {
version: '2.0.0'
},
pluginConfig: {
assets: [
'/dist'
Expand Down

0 comments on commit 6254bbd

Please sign in to comment.