2025-01-24 13:04:43 -08:00
|
|
|
import path from 'node:path';
|
2025-02-10 15:05:13 -08:00
|
|
|
import fs from 'node:fs';
|
2025-01-24 13:04:43 -08:00
|
|
|
import Electron from 'electron';
|
2025-02-10 15:05:13 -08:00
|
|
|
import log from './logger';
|
2025-01-24 13:04:43 -08:00
|
|
|
|
|
|
|
|
export const getBinaryPath = (app: Electron.App, binaryName: string): string => {
|
|
|
|
|
const isDev = process.env.NODE_ENV === 'development';
|
|
|
|
|
const isPackaged = app.isPackaged;
|
2025-02-10 15:05:13 -08:00
|
|
|
const isWindows = process.platform === 'win32';
|
|
|
|
|
|
|
|
|
|
// On Windows, use .cmd for npx and .exe for uvx
|
|
|
|
|
const executableName = isWindows
|
|
|
|
|
? binaryName === 'npx'
|
|
|
|
|
? 'npx.cmd'
|
|
|
|
|
: `${binaryName}.exe`
|
|
|
|
|
: binaryName;
|
|
|
|
|
|
|
|
|
|
// List of possible paths to check
|
|
|
|
|
const possiblePaths = [];
|
2025-01-24 13:04:43 -08:00
|
|
|
|
|
|
|
|
if (isDev && !isPackaged) {
|
2025-02-10 15:05:13 -08:00
|
|
|
// In development, check multiple possible locations
|
|
|
|
|
possiblePaths.push(
|
|
|
|
|
path.join(process.cwd(), 'src', 'bin', executableName),
|
|
|
|
|
path.join(process.cwd(), 'bin', executableName),
|
|
|
|
|
path.join(process.cwd(), '..', '..', 'target', 'release', executableName)
|
2025-01-24 13:04:43 -08:00
|
|
|
);
|
|
|
|
|
} else {
|
2025-02-10 15:05:13 -08:00
|
|
|
// In production, check resources paths
|
|
|
|
|
possiblePaths.push(
|
|
|
|
|
path.join(process.resourcesPath, 'bin', executableName),
|
|
|
|
|
path.join(app.getAppPath(), 'resources', 'bin', executableName)
|
2025-01-24 13:04:43 -08:00
|
|
|
);
|
|
|
|
|
}
|
2025-02-10 15:05:13 -08:00
|
|
|
|
|
|
|
|
// Log all paths we're checking
|
|
|
|
|
log.info('Checking binary paths:', possiblePaths);
|
|
|
|
|
|
|
|
|
|
// Try each path and return the first one that exists
|
|
|
|
|
for (const binPath of possiblePaths) {
|
|
|
|
|
try {
|
|
|
|
|
if (fs.existsSync(binPath)) {
|
|
|
|
|
log.info(`Found binary at: ${binPath}`);
|
|
|
|
|
return binPath;
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
log.error(`Error checking path ${binPath}:`, error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we get here, we couldn't find the binary
|
|
|
|
|
const error = `Could not find ${binaryName} binary in any of the expected locations: ${possiblePaths.join(', ')}`;
|
|
|
|
|
log.error(error);
|
|
|
|
|
throw new Error(error);
|
2025-01-24 13:04:43 -08:00
|
|
|
};
|