1
1
import { readFile } from "fs/promises"
2
+ import semverSatisfies from "semver/functions/satisfies.js"
2
3
3
4
/**
4
5
* The list of assets
@@ -15,16 +16,56 @@ export async function loadAssetList(path: string): Promise<Assets> {
15
16
return JSON . parse ( data )
16
17
}
17
18
18
- type MatchAssetOpts = {
19
+ /**
20
+ * The options to match the asset
21
+ */
22
+ export type MatchAssetOpts = {
23
+ /**
24
+ * The version to match
25
+ */
19
26
version : string
27
+ /**
28
+ * The keywords that must be in the asset name
29
+ * @default []
30
+ */
20
31
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
+ */
21
37
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
+ */
22
54
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
+ */
23
61
filterName ?: ( asset : string ) => boolean
24
62
}
25
63
26
64
/**
27
65
* 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
28
69
*/
29
70
export function matchAsset (
30
71
assets : Assets ,
@@ -52,11 +93,14 @@ export function matchAsset(
52
93
return undefined
53
94
}
54
95
96
+ // Assume the version is a semver version if a custom version compare function is not given
97
+ const versionSatisfies = opts . versionSatisfies ?? semverSatisfies
98
+
55
99
// find the first tag that starts with the version
56
100
// loop over the versions starting with the latest
57
101
const candidateTags : string [ ] = [ ]
58
102
for ( const [ version , origTag ] of versionMap . entries ( ) ) {
59
- if ( version . startsWith ( opts . version ) ) {
103
+ if ( versionSatisfies ( version , opts . version ) ) {
60
104
candidateTags . push ( origTag )
61
105
}
62
106
}
0 commit comments