Files
setup-github-release/src/core/finder.ts
Slawomir Koszewski 620da93338
All checks were successful
Test Action / test (push) Successful in 3s
Rearranged the code and added CLI that installs tools locally.
2026-01-11 10:54:25 +01:00

39 lines
1.2 KiB
TypeScript

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 {
const items = fs.readdirSync(dir);
if (debug) {
logger(`Searching for binary in ${dir}...`);
items.forEach(item => logger(` - ${item}`));
}
for (const item of items) {
const fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
const found = findBinary(fullPath, pattern, debug, logger);
if (found) return found;
} else {
let isMatch = false;
if (pattern instanceof RegExp) {
isMatch = pattern.test(item);
} else {
isMatch = item === pattern;
// On Windows, also check for .exe extension if the pattern doesn't have it
if (!isMatch && process.platform === 'win32' && !pattern.toLowerCase().endsWith('.exe')) {
isMatch = item.toLowerCase() === `${pattern.toLowerCase()}.exe`;
}
}
if (isMatch) return fullPath;
}
}
return undefined;
}
export function setExecutable(filePath: string): void {
if (process.platform !== 'win32') {
fs.chmodSync(filePath, '755');
}
}