Skip to content

Commit

Permalink
Fix plugin update detection
Browse files Browse the repository at this point in the history
  • Loading branch information
BenSurgisonGDS committed Oct 25, 2023
1 parent 9c13ca5 commit f38c2a2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixes

- [#2364: Fix plugin update detection](https://github.com/alphagov/govuk-prototype-kit/pull/2364)

## 13.13.5

### Fixes
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const projectPackage = require(path.join(projectDir, 'package.json'))
const config = require('../config')
const { getConfigForPackage } = require('../utils/requestHttps')
const { getProxyPluginConfig } = require('./plugin-utils')
const { sortByObjectKey } = require('../utils')
const { sortByObjectKey, hasNewVersion } = require('../utils')

let packageTrackerInterval

Expand Down Expand Up @@ -119,7 +119,7 @@ async function refreshPackageInfo (packageName, version) {
localVersion = path.resolve(installedPackageVersion.replace('file:', ''))
}

const updateAvailable = installed && !installedLocally && !installedFromGithub && installedVersion !== latestVersion
const updateAvailable = installed && !installedLocally && !installedFromGithub && hasNewVersion(installedVersion, latestVersion)

const pluginDependencies = pluginConfig?.pluginDependencies ? normaliseDependencies(pluginConfig.pluginDependencies) : undefined

Expand Down
21 changes: 20 additions & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,24 @@ function sortByObjectKey (key) {
}
}

function hasNewVersion (installedVersion, latestVersion) {
const matcher = /^(\d+)\.(\d+)\.(\d+)(.+)?/
const [, installedMajor, installedMinor, installedPatch, installedSuffix] = installedVersion.match(matcher)
const [, latestMajor, latestMinor, latestPatch, latestSuffix] = latestVersion.match(matcher)

if (installedMajor < latestMajor) return true
if (installedMajor > latestMajor) return false
if (installedMinor < latestMinor) return true
if (installedMinor > latestMinor) return false
if (installedPatch < latestPatch) return true
if (installedPatch > latestPatch) return false

if (installedSuffix && latestSuffix) {
return installedSuffix < latestSuffix
}
return !!installedSuffix
}

module.exports = {
prototypeAppScripts: scripts,
addNunjucksFilters,
Expand All @@ -282,5 +300,6 @@ module.exports = {
sessionFileStoreQuietLogFn,
searchAndReplaceFiles,
recursiveDirectoryContentsSync,
sortByObjectKey
sortByObjectKey,
hasNewVersion
}
29 changes: 28 additions & 1 deletion lib/utils/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-env jest */

const { sessionFileStoreQuietLogFn, hasNewVersion } = require('./index')
afterEach(() => {
jest.useRealTimers()
jest.restoreAllMocks()
Expand All @@ -10,7 +11,6 @@ describe('sessionFileStoreQuietLogFn', () => {
jest.spyOn(global.console, 'log').mockImplementation()

const SessionFileStore = require('../session-file-store')
const { sessionFileStoreQuietLogFn } = require('./index')

jest.useFakeTimers({ doNotFake: ['performance'] })

Expand All @@ -26,3 +26,30 @@ describe('sessionFileStoreQuietLogFn', () => {
expect(console.log).not.toHaveBeenCalled()
})
})

describe('hasNewVersion', () => {
[
{ installed: '13.1.0', latest: '13.1.0', result: false },
{ installed: '13.1.1', latest: '13.1.0', result: false },
{ installed: '13.2.0', latest: '13.1.0', result: false },
{ installed: '14.1.0', latest: '13.1.0', result: false },
{ installed: '13.1.0', latest: '13.1.1', result: true },
{ installed: '13.1.0', latest: '13.2.0', result: true },
{ installed: '13.1.0', latest: '14.1.0', result: true },
{ installed: '13.2.4-rc1', latest: '13.2.4-rc1', result: false },
{ installed: '13.2.3-rc1', latest: '13.2.4-rc1', result: true },
{ installed: '13.2.4-rc1', latest: '13.2.4-rc2', result: true },
{ installed: '13.2.4-rc1', latest: '13.2.4', result: true },
{ installed: '13.2.4', latest: '13.2.4-rc1', result: false },
{ installed: '5.0.0-internal.0', latest: '4.7.0', result: false },
{ installed: '4.7.0', latest: '5.0.0-internal.0', result: true },
{ installed: '4.7.0-internal.0', latest: '5.0.0', result: true },
{ installed: '5.0.0', latest: '4.7.0-internal.0', result: false }
].forEach(({
installed,
latest,
result
}) => it(`should return ${result} when installed version is ${installed} and latest version is ${latest}`, () => {
expect(hasNewVersion(installed, latest)).toEqual(result)
}))
})

0 comments on commit f38c2a2

Please sign in to comment.