Fix: Enhance asset filtering with dry run support and update platform architecture patterns

This commit is contained in:
2026-04-07 06:55:24 +02:00
parent d2f1cb9c1a
commit e7e7ee53f6
3 changed files with 22 additions and 9 deletions

View File

@@ -180,7 +180,7 @@ async function run() {
console.log(`Fetching latest release for ${repository}...`); console.log(`Fetching latest release for ${repository}...`);
const release = await fetchLatestRelease(repository, token); const release = await fetchLatestRelease(repository, token);
const asset = getMatchingAsset(release.assets, platformInfo, options.fileName, options.fileType); const asset = getMatchingAsset(release.assets, platformInfo, options.fileName, options.fileType, options.dryRun);
const version = release.tag_name.replace(/^v/i, ''); const version = release.tag_name.replace(/^v/i, '');
const downloadUrl = asset.browser_download_url; const downloadUrl = asset.browser_download_url;

View File

@@ -11,9 +11,22 @@ const knownFileTypes: Record<string, string> = {
targz: '*.{tgz,tar.gz}', targz: '*.{tgz,tar.gz}',
}; };
function filterByRegex(assets: ReleaseAsset[], pattern: string): ReleaseAsset[] { function debugAssetList(label: string, assets: ReleaseAsset[]): void {
console.debug(label);
for (const asset of assets) {
console.debug(`- ${asset.name}`);
}
}
function filterByRegex(assets: ReleaseAsset[], pattern: string, dryRun?: boolean): ReleaseAsset[] {
const regex = new RegExp(pattern, 'i'); const regex = new RegExp(pattern, 'i');
return assets.filter((asset) => regex.test(asset.name)); const filtered = assets.filter((asset) => regex.test(asset.name));
if (dryRun) {
console.debug(`Filtering assets with regex: ${pattern}`);
debugAssetList('Assets:', assets);
debugAssetList('Matched assets:', filtered);
}
return filtered;
} }
function replacePlatformPlaceholders(pattern: string, platform: PlatformInfo): string { function replacePlatformPlaceholders(pattern: string, platform: PlatformInfo): string {
@@ -22,7 +35,7 @@ function replacePlatformPlaceholders(pattern: string, platform: PlatformInfo): s
.replace(/{{ARCH}}/g, platform.archPattern); .replace(/{{ARCH}}/g, platform.archPattern);
} }
export function getMatchingAsset(assets: ReleaseAsset[], platform: PlatformInfo, fileName?: string, fileType?: string): ReleaseAsset { export function getMatchingAsset(assets: ReleaseAsset[], platform: PlatformInfo, fileName?: string, fileType?: string, dryRun?: boolean): ReleaseAsset {
// Filename provided as literal string (no ~): exact match. // Filename provided as literal string (no ~): exact match.
if (fileName && !fileName.startsWith('~')) { if (fileName && !fileName.startsWith('~')) {
const exactMatches = assets.filter((asset) => asset.name === fileName); const exactMatches = assets.filter((asset) => asset.name === fileName);
@@ -42,7 +55,7 @@ export function getMatchingAsset(assets: ReleaseAsset[], platform: PlatformInfo,
} else if (fileType.startsWith('~')) { } else if (fileType.startsWith('~')) {
// 3. Custom regex fileType: match regex at end of string. // 3. Custom regex fileType: match regex at end of string.
const fileTypeRegex = `${fileType.substring(1)}$`; const fileTypeRegex = `${fileType.substring(1)}$`;
fileTypeFilteredAssets = filterByRegex(assets, fileTypeRegex); fileTypeFilteredAssets = filterByRegex(assets, fileTypeRegex, dryRun);
} else { } else {
// 4. Custom extension fileType: treat as plain extension glob. // 4. Custom extension fileType: treat as plain extension glob.
const extension = fileType.replace(/^\./, ''); const extension = fileType.replace(/^\./, '');
@@ -54,7 +67,7 @@ export function getMatchingAsset(assets: ReleaseAsset[], platform: PlatformInfo,
// 4. Filename provided with ~: platform placeholder expansion and regex filtering. // 4. Filename provided with ~: platform placeholder expansion and regex filtering.
if (fileName && fileName.startsWith('~')) { if (fileName && fileName.startsWith('~')) {
const fileNamePattern = replacePlatformPlaceholders(fileName.substring(1), platform); const fileNamePattern = replacePlatformPlaceholders(fileName.substring(1), platform);
const fileNameFilteredAssets = filterByRegex(fileTypeFilteredAssets, fileNamePattern); const fileNameFilteredAssets = filterByRegex(fileTypeFilteredAssets, fileNamePattern, dryRun);
if (fileNameFilteredAssets.length !== 1) { if (fileNameFilteredAssets.length !== 1) {
throw new Error(`Expected exactly one asset to match the filename regex, matched: ${fileNameFilteredAssets.length}`); throw new Error(`Expected exactly one asset to match the filename regex, matched: ${fileNameFilteredAssets.length}`);
} }
@@ -63,7 +76,7 @@ export function getMatchingAsset(assets: ReleaseAsset[], platform: PlatformInfo,
// 5. No filename: use default {{SYSTEM}}-{{ARCH}} regex. // 5. No filename: use default {{SYSTEM}}-{{ARCH}} regex.
const defaultPattern = replacePlatformPlaceholders('{{SYSTEM}}[_-]{{ARCH}}', platform); const defaultPattern = replacePlatformPlaceholders('{{SYSTEM}}[_-]{{ARCH}}', platform);
const defaultFilteredAssets = filterByRegex(fileTypeFilteredAssets, defaultPattern); const defaultFilteredAssets = filterByRegex(fileTypeFilteredAssets, defaultPattern, dryRun);
// 6. Zero or multiple matches are errors. // 6. Zero or multiple matches are errors.
if (defaultFilteredAssets.length !== 1) { if (defaultFilteredAssets.length !== 1) {

View File

@@ -19,8 +19,8 @@ export const systemPatterns: Record<string, string> = {
}; };
export const archPatterns: Record<string, string> = { export const archPatterns: Record<string, string> = {
x64: '(x86_64|x64|amd64)', x64: '(x86_64|x64|amd64|universal)',
arm64: '(aarch64|arm64)' arm64: '(aarch64|arm64|universal)'
}; };
export function getPlatformInfo(overrides?: PlatformOverrides): PlatformInfo { export function getPlatformInfo(overrides?: PlatformOverrides): PlatformInfo {