Skip to content

Commit

Permalink
Simplify PackagistVersion
Browse files Browse the repository at this point in the history
* Extract latest version string logics from
  BasePackagistService.findLatestRelease into findLatestVersion.

* Reuse BasePackagistService.findLatestVersion in PackagistVersion
  to find the latest release. Remove duplicated logics.
  • Loading branch information
yookoala committed Feb 9, 2022
1 parent 1d7af3a commit 8e97937
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
39 changes: 36 additions & 3 deletions services/packagist/packagist-base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Joi from 'joi'
import { BaseJsonService } from '../index.js'
import { BaseJsonService, NotFound } from '../index.js'
import { isStable, latest } from '../php-version.js'

const packageSchema = Joi.array().items(
Expand Down Expand Up @@ -125,9 +125,42 @@ class BasePackagistService extends BaseJsonService {
return expanded
}

/**
* From an array of package, derive the latest release
* version string.
*
* @param {Array} versions An array of package versions.
* @param {boolean} includePrereleases Should pre-release version id be included.
*
* @throws {NotFound} If the packages array is empty or not an array.
* @returns {string} The version string of the latest version.
*/
findLatestVersion(versions, includePrereleases = false) {
if (!Array.isArray(versions) || versions.length === 0) {
throw new NotFound({ prettyMessage: 'no released version found' })
}

const versionStrings = versions
.map(version => version.version)
.filter(
versionString =>
typeof versionString === 'string' || versionString instanceof String
)

let version = latest(versionStrings)
if (!includePrereleases) {
version =
latest(versionStrings.filter(isStable)) || latest(versionStrings)
}

if (version === undefined) {
throw new NotFound({ prettyMessage: 'no released version found' })
}
return version
}

findLatestRelease(json) {
const versions = json.map(version => version.version)
const release = latest(versions.filter(isStable)) || latest(versions)
const release = this.findLatestVersion(json)
return json.filter(version => version.version === release)[0]
}
}
Expand Down
16 changes: 2 additions & 14 deletions services/packagist/packagist-version.service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Joi from 'joi'
import { renderVersionBadge } from '../version.js'
import { isStable, latest } from '../php-version.js'
import { optionalUrl } from '../validators.js'
import { NotFound, redirector } from '../index.js'
import {
Expand Down Expand Up @@ -77,18 +76,6 @@ class PackagistVersion extends BasePackagistService {
return renderVersionBadge({ version })
}

transform({ includePrereleases, json, user, repo }) {
const versionsData = json.packages[this.getPackageName(user, repo)]
const versions = versionsData.map(version => version.version)

if (includePrereleases) {
return { version: latest(versions) }
} else {
const stableVersion = latest(versions.filter(isStable))
return { version: stableVersion || latest(versions) }
}
}

async handle(
{ user, repo },
{ include_prereleases: includePrereleases, server }
Expand All @@ -100,7 +87,8 @@ class PackagistVersion extends BasePackagistService {
schema: includePrereleases ? schema : allVersionsSchema,
server,
})
const { version } = this.transform({ includePrereleases, json, user, repo })
const packageJson = json.packages[this.getPackageName(user, repo)]
const version = this.findLatestVersion(packageJson, includePrereleases)
if (version === undefined) {
throw new NotFound({ prettyMessage: 'no released version found' })
}
Expand Down

0 comments on commit 8e97937

Please sign in to comment.