Refactor: Consolidate asset matching logic into a single function
This commit is contained in:
@@ -77,34 +77,40 @@ function matchFilenameString(re: string, pi: PlatformInfo, extRe: string): strin
|
|||||||
.replace(/{{EXT_PATTERN}}/g, extRe);
|
.replace(/{{EXT_PATTERN}}/g, extRe);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function matchSingleAssetByRegex(assets: any[], pattern: string, noMatchError: string, multipleMatchErrorPrefix: string): any {
|
||||||
|
const regex = new RegExp(pattern, 'i');
|
||||||
|
const matchingAssets = assets.filter((a: any) => regex.test(a.name));
|
||||||
|
if (matchingAssets.length === 0) {
|
||||||
|
throw new Error(noMatchError);
|
||||||
|
}
|
||||||
|
if (matchingAssets.length > 1) {
|
||||||
|
throw new Error(`${multipleMatchErrorPrefix}: ${matchingAssets.map((a: any) => a.name).join(', ')}`);
|
||||||
|
}
|
||||||
|
return matchingAssets[0];
|
||||||
|
}
|
||||||
|
|
||||||
export function getMatchingAsset(assets: any[], platform: PlatformInfo, options: MatchOptions): any {
|
export function getMatchingAsset(assets: any[], platform: PlatformInfo, options: MatchOptions): any {
|
||||||
const { fileName, fileType } = options;
|
const { fileName, fileType } = options;
|
||||||
const extPattern = getExtPattern(fileType, platform.system);
|
const extPattern = getExtPattern(fileType, platform.system);
|
||||||
|
|
||||||
if (!fileName) {
|
if (!fileName || fileName.startsWith('~')) {
|
||||||
// Rule 1: Default matching rule
|
// Rule 1 + Rule 3: Regex-based matching rules
|
||||||
const pattern = `${platform.systemPattern}[_-]${platform.archPattern}.*${extPattern}`;
|
const pattern = !fileName
|
||||||
const regex = new RegExp(pattern, 'i');
|
? `${platform.systemPattern}[_-]${platform.archPattern}.*${extPattern}`
|
||||||
const matchingAssets = assets.filter((a: any) => regex.test(a.name));
|
: matchFilenameString(fileName.substring(1), platform, extPattern);
|
||||||
if (matchingAssets.length === 0) {
|
const noMatchError = !fileName
|
||||||
throw new Error(`No assets matched the default criteria: ${pattern}`);
|
? `No assets matched the default criteria: ${pattern}`
|
||||||
}
|
: `No assets matched the regex: ${pattern}`;
|
||||||
if (matchingAssets.length > 1) {
|
const multipleMatchErrorPrefix = !fileName
|
||||||
throw new Error(`Multiple assets matched the default criteria: ${matchingAssets.map((a: any) => a.name).join(', ')}`);
|
? 'Multiple assets matched the default criteria'
|
||||||
}
|
: 'Multiple assets matched the criteria';
|
||||||
return matchingAssets[0];
|
|
||||||
} else if (fileName.startsWith('~')) {
|
return matchSingleAssetByRegex(
|
||||||
// Rule 3: Regex matching rule
|
assets,
|
||||||
const pattern = matchFilenameString(fileName.substring(1), platform, extPattern);
|
pattern,
|
||||||
const regex = new RegExp(pattern, 'i');
|
noMatchError,
|
||||||
const matchingAssets = assets.filter((a: any) => regex.test(a.name));
|
multipleMatchErrorPrefix
|
||||||
if (matchingAssets.length === 0) {
|
);
|
||||||
throw new Error(`No assets matched the regex: ${pattern}`);
|
|
||||||
}
|
|
||||||
if (matchingAssets.length > 1) {
|
|
||||||
throw new Error(`Multiple assets matched the criteria: ${matchingAssets.map((a: any) => a.name).join(', ')}`);
|
|
||||||
}
|
|
||||||
return matchingAssets[0];
|
|
||||||
} else {
|
} else {
|
||||||
// Rule 2: Literal matching rule
|
// Rule 2: Literal matching rule
|
||||||
const asset = assets.find((a: any) => a.name === fileName);
|
const asset = assets.find((a: any) => a.name === fileName);
|
||||||
|
|||||||
Reference in New Issue
Block a user