Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor - usage renderVersionBadge - part 2 [amo archlinux aur bower cdnjs chromewebstore cocoapods conan conda cookbook cpan cran crates ctan curseforge debian docker dub eclipsemarketplace elmpackage f-droid factorio fedora feedz flathub galaxytoolshed gem gitea github gitlab greasyfork hackage hexpm homebrew itunes jenkins jetbrains jitpack jsr mavenmetadata modrinth nexus npm nuget openvsx opm ore packagist piwheels polymart pub puppetforge pypi ros scoop snapcraft spack spiget thunderstore twitch ubuntu vaadindirectory vcpkg visualstudioappcenter visualstudiomarketplace vpm wordpress] #10615

Merged
merged 9 commits into from
Oct 20, 2024
Merged
5 changes: 3 additions & 2 deletions services/aur/aur.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import {
age as ageColor,
} from '../color-formatters.js'
import { renderLicenseBadge } from '../licenses.js'
import { addv, metric, formatDate } from '../text-formatters.js'
import { metric, formatDate } from '../text-formatters.js'
import { nonNegativeInteger } from '../validators.js'
import {
BaseJsonService,
NotFound,
InvalidResponse,
pathParams,
} from '../index.js'
import { renderVersionBadge } from '../version.js'

const aurSchema = Joi.object({
resultcount: nonNegativeInteger,
Expand Down Expand Up @@ -170,7 +171,7 @@ class AurVersion extends BaseAurService {

static render({ version, outOfDate }) {
const color = outOfDate === null ? 'blue' : 'orange'
return { message: addv(version), color }
return renderVersionBadge({ version, versionFormatter: () => color })
}

async handle({ packageName }) {
Expand Down
2 changes: 2 additions & 0 deletions services/aur/aur.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ describe('AurVersion', function () {
given({ version: '1:1.1.42.622-1', outOfDate: 1 }).expect({
color: 'orange',
message: 'v1:1.1.42.622-1',
label: undefined,
})

given({ version: '7', outOfDate: null }).expect({
color: 'blue',
message: 'v7',
label: undefined,
})
})
})
17 changes: 4 additions & 13 deletions services/conda/conda-version.service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { pathParams } from '../index.js'
import { addv as versionText } from '../text-formatters.js'
import { version as versionColor } from '../color-formatters.js'
import { renderVersionBadge } from '../version.js'
import BaseCondaService from './conda-base.js'

export default class CondaVersion extends BaseCondaService {
Expand Down Expand Up @@ -33,20 +32,12 @@ export default class CondaVersion extends BaseCondaService {
},
}

static render({ variant, channel, version }) {
return {
label: variant === 'vn' ? channel : `conda | ${channel}`,
message: versionText(version),
color: versionColor(version),
}
}

async handle({ variant, channel, packageName }) {
const json = await this.fetch({ channel, packageName })
return this.constructor.render({
variant,
channel,
const defaultLabel = variant === 'vn' ? channel : `conda | ${channel}`
return renderVersionBadge({
version: json.latest_version,
defaultLabel,
})
}
}
2 changes: 1 addition & 1 deletion services/npm/npm-base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('npm', function () {
await NpmVersion.invoke(defaultContext, config, { packageName: 'npm' }),
).to.deep.equal({
color: 'orange',
label: undefined,
label: 'npm',
message: 'v0.1.0',
})

Expand Down
10 changes: 8 additions & 2 deletions services/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ function rangeStart(v) {
* @param {string} options.version - The version number to display on the badge
* @param {string} [options.tag] - The tag to display on the badge, such as "alpha" or "beta"
* @param {string} [options.defaultLabel] - The default label to display on the badge, such as "npm" or "github"
* @param {string} [options.prefix] - The prefix to display on the message, such as ">=", "v", overrides the default behavior of using addv
* @param {string} [options.postfix] - The postfix to display on the message, such as "tested"
* @param {Function} [options.versionFormatter=versionColor] - The function to use to format the color of the badge based on the version number
* @returns {object} A badge object that has three properties: label, message, and color
* @example
Expand All @@ -245,11 +247,15 @@ function renderVersionBadge({
version,
tag,
defaultLabel,
prefix,
postfix,
versionFormatter = versionColor,
}) {
return {
label: tag ? `${defaultLabel}@${tag}` : undefined,
message: addv(version),
label: tag ? `${defaultLabel}@${tag}` : defaultLabel,
message:
(prefix ? `${prefix}${version}` : addv(version)) +
(postfix ? ` ${postfix}` : ''),
color: versionFormatter(version),
}
}
Expand Down
64 changes: 64 additions & 0 deletions services/version.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,69 @@ describe('Version helpers', function () {
message: 'v1.2.3',
color: 'blue',
})
given({ version: '1.2.3', defaultLabel: 'npm' }).expect({
label: 'npm',
message: 'v1.2.3',
color: 'blue',
})
given({ version: '1.2.3', postfix: 'tested' }).expect({
label: undefined,
message: 'v1.2.3 tested',
color: 'blue',
})
given({
version: '1.2.3',
tag: 'beta',
defaultLabel: 'github',
postfix: 'tested',
}).expect({
label: 'github@beta',
message: 'v1.2.3 tested',
color: 'blue',
})
given({ version: '1.2.3', prefix: '^' }).expect({
label: undefined,
message: '^1.2.3',
color: 'blue',
})
given({
version: '1.2.3',
tag: 'alpha',
defaultLabel: 'npm',
prefix: '^',
}).expect({
label: 'npm@alpha',
message: '^1.2.3',
color: 'blue',
})
given({
version: '1.2.3',
defaultLabel: 'npm',
prefix: '^',
}).expect({
label: 'npm',
message: '^1.2.3',
color: 'blue',
})
given({
version: '1.2.3',
prefix: '^',
postfix: 'tested',
}).expect({
label: undefined,
message: '^1.2.3 tested',
color: 'blue',
})
given({
version: '1.2.3',
tag: 'beta',
defaultLabel: 'github',
prefix: '^',
postfix: 'tested',
}).expect({
label: 'github@beta',
message: '^1.2.3 tested',
color: 'blue',
})
})
})
41 changes: 10 additions & 31 deletions services/wordpress/wordpress-platform.service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NotFound, pathParams } from '../index.js'
import { addv } from '../text-formatters.js'
import { version as versionColor } from '../color-formatters.js'
import { renderVersionBadge } from '../version.js'
import { description, BaseWordpress } from './wordpress-base.js'
import { versionColorForWordpressVersion } from './wordpress-version-color.js'

Expand Down Expand Up @@ -46,13 +45,6 @@ function WordpressRequiresVersion(extensionType) {

static defaultBadgeData = { label: 'wordpress' }

static render({ wordpressVersion }) {
return {
message: addv(wordpressVersion),
color: versionColor(wordpressVersion),
}
}

async handle({ slug }) {
const { requires: wordpressVersion } = await this.fetch({
extensionType,
Expand All @@ -65,7 +57,7 @@ function WordpressRequiresVersion(extensionType) {
})
}

return this.constructor.render({ wordpressVersion })
return renderVersionBadge({ version: wordpressVersion })
}
}
}
Expand Down Expand Up @@ -93,21 +85,18 @@ class WordpressPluginTestedVersion extends BaseWordpress {

static defaultBadgeData = { label: 'wordpress' }

static async render({ testedVersion }) {
// Atypically, the `render()` function of this badge is `async` because it needs to pull
// data from the server.
return {
message: `${addv(testedVersion)} tested`,
color: await versionColorForWordpressVersion(testedVersion),
}
}

async handle({ slug }) {
const { tested: testedVersion } = await this.fetch({
extensionType: 'plugin',
slug,
})
return this.constructor.render({ testedVersion })
// Atypically, pulling color data from the server with async operation.
const color = await versionColorForWordpressVersion(testedVersion)
return renderVersionBadge({
version: testedVersion,
postfix: 'tested',
versionFormatter: () => color,
})
}
}

Expand Down Expand Up @@ -142,14 +131,6 @@ function RequiresPHPVersionForType(extensionType) {

static defaultBadgeData = { label: 'php' }

static render({ version }) {
return {
label: 'php',
message: `>=${version}`,
color: versionColor(version),
}
}

async handle({ slug }) {
const { requires_php: requiresPhp } = await this.fetch({
extensionType,
Expand All @@ -162,9 +143,7 @@ function RequiresPHPVersionForType(extensionType) {
})
}

return this.constructor.render({
version: requiresPhp,
})
return renderVersionBadge({ version: requiresPhp, prefix: '>=' })
}
}
}
Expand Down
Loading