Refactor: Simplify binary pattern handling in findBinary and findInstalledBinary functions
Test Action / test (push) Successful in 12s

This commit is contained in:
2026-04-07 09:43:53 +02:00
parent e8a9b0906d
commit 9138f03d35
4 changed files with 85 additions and 88 deletions
+2 -2
View File
@@ -189,13 +189,13 @@ async function run() {
console.log(`Extracting ${asset.name}...`);
await extractAsset(downloadPath, extractDir);
let binaryPattern: string | RegExp;
let binaryPattern: string;
if (binarySource.startsWith('~')) {
const binaryRegex = binarySource
.substring(1)
.replace(/{{SYSTEM}}/g, platformInfo.systemPattern)
.replace(/{{ARCH}}/g, platformInfo.archPattern);
binaryPattern = new RegExp(binaryRegex, 'i');
binaryPattern = `~${binaryRegex}`;
} else {
binaryPattern = binarySource
.replace(/{{SYSTEM}}/g, platformInfo.system)
+4 -3
View File
@@ -1,7 +1,8 @@
import * as fs from 'fs';
import * as path from 'path';
export function findBinary(dir: string, pattern: string | RegExp, debug: boolean, logger: (msg: string) => void): string | undefined {
export function findBinary(dir: string, pattern: string, debug: boolean, logger: (msg: string) => void): string | undefined {
const regex = pattern.startsWith('~') ? new RegExp(pattern.substring(1), 'i') : undefined;
const items = fs.readdirSync(dir);
if (debug) {
logger(`Searching for binary in ${dir}...`);
@@ -16,8 +17,8 @@ export function findBinary(dir: string, pattern: string | RegExp, debug: boolean
if (found) return found;
} else {
let isMatch = false;
if (pattern instanceof RegExp) {
isMatch = pattern.test(item);
if (regex) {
isMatch = regex.test(item);
} else {
isMatch = item === pattern;
// On Windows, also check for .exe extension if the pattern doesn't have it
+3 -9
View File
@@ -10,12 +10,9 @@ import { fetchLatestRelease } from './core/downloader';
import { installSystemPackage } from './core/installer';
function findInstalledBinary(binaryName: string): string | undefined {
let pattern: RegExp;
if (!binaryName.startsWith('~')) {
pattern = new RegExp(`^${binaryName}$`, 'i');
} else {
pattern = new RegExp(binaryName.substring(1), 'i');
binaryName = `~^${binaryName}$`;
}
const dirs = [
@@ -33,7 +30,7 @@ function findInstalledBinary(binaryName: string): string | undefined {
if (!fs.existsSync(d)) {
continue;
}
const p = findBinary(d, pattern, false, () => undefined);
const p = findBinary(d, binaryName, false, () => undefined);
if (p) {
return p;
}
@@ -138,16 +135,13 @@ async function run() {
}
// Find the binary within the extracted/prepared directory
let binaryPattern: string | RegExp;
if (binaryName.startsWith('~')) {
binaryPattern = new RegExp(binaryName.substring(1), 'i');
core.info(`Searching for binary matching regex: ${binaryName.substring(1)}`);
} else {
binaryPattern = binaryName;
core.info(`Searching for binary named: ${binaryName}`);
}
const binaryPath = findBinary(toolDir, binaryPattern, debug, (msg) => core.info(msg));
const binaryPath = findBinary(toolDir, binaryName, debug, (msg) => core.info(msg));
if (!binaryPath) {
throw new Error(`Could not find binary "${binaryName}" in the extracted asset.`);
}