farmcontrol-ui/src/desktop/windows-app-paths.js
Tom Butcher de93658df1
Some checks failed
farmcontrol/farmcontrol-ui/pipeline/head There was a failure building this commit
Enhance Windows instance management and path resolution
- Introduced a new function `isPrimaryInstanceReachable` to check the reachability of the primary instance, improving instance handling on Windows.
- Updated `forwardDeeplinkToRunningInstance` to include an option for signal fallback, enhancing flexibility in deeplink forwarding.
- Implemented `tryRecoverUnresponsivePrimaryLock` to handle unresponsive primary instances by removing stale lock files, improving application reliability.
- Refactored `resolveWindowsBinDir` to utilize unique path resolution, ensuring accurate directory handling for Windows applications.
- Enhanced the `ensureWindowsWorkingDirectory` function to streamline directory changes, improving application behavior.
2026-08-03 00:21:04 +01:00

73 lines
1.4 KiB
JavaScript

import { chdirSync, existsSync } from 'node:fs'
import { dirname, join, resolve } from 'node:path'
const NATIVE_WRAPPER_DLL = 'libNativeWrapper.dll'
function uniquePaths(paths) {
const seen = new Set()
const result = []
for (const candidate of paths) {
if (!candidate) {
continue
}
const normalized = resolve(candidate)
if (seen.has(normalized)) {
continue
}
seen.add(normalized)
result.push(normalized)
}
return result
}
function hasAppBinMarker(dir) {
return (
existsSync(join(dir, 'launcher.exe')) ||
existsSync(join(dir, NATIVE_WRAPPER_DLL)) ||
existsSync(join(dir, 'bun.exe')) ||
existsSync(join(dir, '..', 'Resources', 'version.json'))
)
}
export function resolveWindowsBinDir() {
const roots = uniquePaths([
dirname(process.argv0),
dirname(process.execPath),
process.cwd()
])
const candidates = []
for (const root of roots) {
candidates.push(root, join(root, 'bin'))
}
for (const candidate of uniquePaths(candidates)) {
if (hasAppBinMarker(candidate)) {
return candidate
}
}
return dirname(process.argv0 || process.execPath)
}
export function ensureWindowsWorkingDirectory() {
if (process.platform !== 'win32') {
return
}
const binDir = resolveWindowsBinDir()
if (resolve(process.cwd()) === resolve(binDir)) {
return
}
try {
chdirSync(binDir)
} catch {
// Ignore if we cannot change directory.
}
}