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. } }