Files
tkmind_go/ui/desktop/src/utils/binaryPath.ts
T

63 lines
1.7 KiB
TypeScript
Raw Normal View History

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 => {
2025-02-10 15:05:13 -08:00
const isWindows = process.platform === 'win32';
const possiblePaths = [];
2025-04-08 10:07:37 -07:00
if (isWindows) {
addPaths(isWindows, possiblePaths, `${binaryName}.exe`, app);
addPaths(isWindows, possiblePaths, `${binaryName}.cmd`, app);
} else {
addPaths(isWindows, possiblePaths, binaryName, app);
}
for (const binPath of possiblePaths) {
try {
if (fs.existsSync(binPath)) {
return binPath;
}
} catch (error) {
log.error(`Error checking path ${binPath}:`, error);
}
}
throw new Error(
`Could not find ${binaryName} binary in any of the expected locations: ${possiblePaths.join(
', '
)}`
);
};
2025-01-24 13:04:43 -08:00
2025-04-08 10:07:37 -07:00
const addPaths = (
isWindows: boolean,
possiblePaths: any[],
executableName: string,
app: Electron.App
): void => {
const isDev = process.env.NODE_ENV === 'development';
const isPackaged = app.isPackaged;
2025-01-24 13:04:43 -08:00
if (isDev && !isPackaged) {
2025-02-10 15:05:13 -08:00
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
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
2025-03-24 09:50:28 -07:00
if (isWindows) {
possiblePaths.push(
path.join(process.resourcesPath, executableName),
path.join(app.getAppPath(), 'resources', executableName),
path.join(app.getPath('exe'), '..', 'bin', executableName)
);
}
}
2025-01-24 13:04:43 -08:00
};