import path from 'path';
import fs from 'fs-extra';
import { fileURLToPath } from 'url';
import { downloadProbeBinary } from './downloader.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const binDir = path.resolve(__dirname, '..', 'bin');
let probeBinaryPath = '';
export async function getBinaryPath(options = {}) {
const { forceDownload = false, version } = options;
if (probeBinaryPath && !forceDownload && fs.existsSync(probeBinaryPath)) {
return probeBinaryPath;
}
if (process.env.PROBE_PATH && fs.existsSync(process.env.PROBE_PATH) && !forceDownload) {
probeBinaryPath = process.env.PROBE_PATH;
return probeBinaryPath;
}
const isWindows = process.platform === 'win32';
const binaryName = isWindows ? 'probe.exe' : 'probe';
const binaryPath = path.join(binDir, binaryName);
if (fs.existsSync(binaryPath) && !forceDownload) {
probeBinaryPath = binaryPath;
return probeBinaryPath;
}
console.log(`${forceDownload ? 'Force downloading' : 'Binary not found. Downloading'} probe binary...`);
probeBinaryPath = await downloadProbeBinary(version);
return probeBinaryPath;
}
export function setBinaryPath(binaryPath) {
if (!fs.existsSync(binaryPath)) {
throw new Error(`No binary found at path: ${binaryPath}`);
}
probeBinaryPath = binaryPath;
}
export async function ensureBinDirectory() {
await fs.ensureDir(binDir);
}
export function buildCliArgs(options, flagMap) {
const cliArgs = [];
for (const [key, flag] of Object.entries(flagMap)) {
if (key in options) {
const value = options[key];
if (typeof value === 'boolean') {
if (value) {
cliArgs.push(flag);
}
} else if (Array.isArray(value)) {
for (const item of value) {
cliArgs.push(flag, item);
}
} else if (value !== undefined && value !== null) {
cliArgs.push(flag, value.toString());
}
}
}
return cliArgs;
}
export function escapeString(str) {
if (process.platform === 'win32') {
return `"${str.replace(/"/g, '\\"')}"`;
} else {
return `'${str.replace(/'/g, "'\\''")}'`;
}
}