fix: add range specifier for simple versions

This commit is contained in:
Amin Yahyaabadi 2024-09-22 22:11:55 -07:00
parent 5e9dd36c6f
commit ed3b715dd5
No known key found for this signature in database
GPG Key ID: F52AF77F636088F0
7 changed files with 38 additions and 8 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -130,7 +130,7 @@ export async function getMinGWPackageInfo(
&& (threadModel === undefined || threadModel === assetThreadModel) && (threadModel === undefined || threadModel === assetThreadModel)
&& (assetExceptionModel === undefined || assetExceptionModel === exceptionModel) && (assetExceptionModel === undefined || assetExceptionModel === exceptionModel)
}, },
versionSatisfies: (assetVersion) => { versionSatisfies: (assetVersion, _version) => {
// extract the base version by coercing the version // extract the base version by coercing the version
const assetCoerce = semverCoerce(assetVersion) const assetCoerce = semverCoerce(assetVersion)
if (assetCoerce === null) { if (assetCoerce === null) {

View File

@ -1,5 +1,6 @@
import { readFile } from "fs/promises" import { readFile } from "fs/promises"
import semverSatisfies from "semver/functions/satisfies.js" import semverSatisfies from "semver/functions/satisfies.js"
import { semverCoercedRangeIfInvalid } from "../setup/version.ts"
/** /**
* The list of assets * The list of assets
@ -38,11 +39,12 @@ export type MatchAssetOpts = {
/** /**
* Custom version compare function * Custom version compare function
* @param candidate The candidate version * @param candidate The candidate version
* @param coeredVersion The coerced version to compare against
* @returns true if the candidate version satisfies the version * @returns true if the candidate version satisfies the version
* *
* @default semverSatisfies * @default semverSatisfies
*/ */
versionSatisfies?: (candidate: string) => boolean versionSatisfies?: (candidate: string, coeredVersion: string) => boolean
/** /**
* Custom tag filter and map function * Custom tag filter and map function
* @param tag The tag to filter and map * @param tag The tag to filter and map
@ -93,13 +95,16 @@ export function matchAsset(
} }
// Assume the version is a semver version if a custom version compare function is not given // Assume the version is a semver version if a custom version compare function is not given
const versionSatisfies = opts.versionSatisfies ?? semverSatisfies const versionSatisfies: (c: string, v: string) => boolean = opts.versionSatisfies ?? semverSatisfies
// If not a valid semver version, coerce it to a semver version range
const versionRange = semverCoercedRangeIfInvalid(opts.version)
// find the first tag that starts with the version // find the first tag that starts with the version
// loop over the versions starting with the latest // loop over the versions starting with the latest
const candidateTags: string[] = [] const candidateTags: string[] = []
for (const [version, origTag] of versionMap.entries()) { for (const [version, origTag] of versionMap.entries()) {
if (versionSatisfies(version, opts.version)) { if (versionSatisfies(version, versionRange)) {
candidateTags.push(origTag) candidateTags.push(origTag)
} }
} }

View File

@ -137,6 +137,31 @@ export function semverCoerceIfInvalid(version: string) {
return version return version
} }
/**
* Coerce the given version to a semver range if it is invalid
*/
export function semverCoercedRangeIfInvalid(version: string) {
if (semverValid(version) === null) {
// version coercion
try {
// find the semver version of an integer
const coercedVersion = semverCoerce(version)
if (coercedVersion !== null) {
// if the versions doesn't specify a range specifier (e.g. ^, ~, >, <, etc.), add a ^ to the version
const versionRange = /^[<=>^~]/.test(coercedVersion.version)
? coercedVersion.version
: `^${coercedVersion.version}`
info(`Coerced version '${version}' to '${versionRange}'`)
return versionRange
}
} catch (err) {
// handled below
}
}
return version
}
export function removeVPrefix(version: string) { export function removeVPrefix(version: string) {
return Number.parseInt(version.replace(/^v/, ""), 10) return Number.parseInt(version.replace(/^v/, ""), 10)
} }