Skip to content

Commit 526968f

Browse files
committed
feat: use semver for matching binary assets
1 parent 2653c43 commit 526968f

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

src/utils/asset/load-assets.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { readFile } from "fs/promises"
2+
import semverSatisfies from "semver/functions/satisfies.js"
23

34
/**
45
* The list of assets
@@ -15,16 +16,56 @@ export async function loadAssetList(path: string): Promise<Assets> {
1516
return JSON.parse(data)
1617
}
1718

18-
type MatchAssetOpts = {
19+
/**
20+
* The options to match the asset
21+
*/
22+
export type MatchAssetOpts = {
23+
/**
24+
* The version to match
25+
*/
1926
version: string
27+
/**
28+
* The keywords that must be in the asset name
29+
* @default []
30+
*/
2031
keywords?: string[]
32+
/**
33+
* Optional keywords that are not required to be in the asset name
34+
* but increase the score of the asset if they are present
35+
* @default []
36+
*/
2137
optionalKeywords?: string[]
38+
/**
39+
* Custom version compare function
40+
* @param candidate The candidate version
41+
* @param version The version to compare against
42+
* @returns true if the candidate version satisfies the version
43+
*
44+
* @default semverSatisfies
45+
*/
46+
versionSatisfies?: (candidate: string, version: string) => boolean
47+
/**
48+
* Custom tag filter and map function
49+
* @param tag The tag to filter and map
50+
* @returns The mapped tag or undefined if the tag should be
51+
* excluded from the search
52+
* @default undefined
53+
*/
2254
filterMapTag?: (tag: string) => string | undefined
55+
/**
56+
* Custom asset name filter function
57+
* @param asset The asset name to filter
58+
* @returns true if the asset should be included in the search
59+
* @default undefined
60+
*/
2361
filterName?: (asset: string) => boolean
2462
}
2563

2664
/**
2765
* Match the asset that matches the version and given keywords
66+
* @param assets The list of assets
67+
* @param opts The options to match the asset
68+
* @returns The tag and name of the asset that matches the version and keywords
2869
*/
2970
export function matchAsset(
3071
assets: Assets,
@@ -52,11 +93,14 @@ export function matchAsset(
5293
return undefined
5394
}
5495

96+
// Assume the version is a semver version if a custom version compare function is not given
97+
const versionSatisfies = opts.versionSatisfies ?? semverSatisfies
98+
5599
// find the first tag that starts with the version
56100
// loop over the versions starting with the latest
57101
const candidateTags: string[] = []
58102
for (const [version, origTag] of versionMap.entries()) {
59-
if (version.startsWith(opts.version)) {
103+
if (versionSatisfies(version, opts.version)) {
60104
candidateTags.push(origTag)
61105
}
62106
}

0 commit comments

Comments
 (0)