( bundleRoot: string, os: 'unix' | 'windows', exists: (p: string) => boolean )
| 69 | * windows: <installDir>\current (bundleRoot) → <installDir> |
| 70 | */ |
| 71 | export function deriveInstallDir( |
| 72 | bundleRoot: string, |
| 73 | os: 'unix' | 'windows', |
| 74 | exists: (p: string) => boolean |
| 75 | ): string | null { |
| 76 | // Use the TARGET platform's path semantics (not the host's), so this is |
| 77 | // deterministic when reasoning about a Windows layout from a POSIX host (CI) |
| 78 | // and vice-versa. In production `os` always matches the running platform. |
| 79 | const P = os === 'windows' ? path.win32 : path.posix; |
| 80 | if (os === 'windows') { |
| 81 | if (P.basename(bundleRoot).toLowerCase() === 'current') { |
| 82 | return P.dirname(bundleRoot); |
| 83 | } |
| 84 | return null; |
| 85 | } |
| 86 | // unix: bundleRoot is <installDir>/versions/<version> |
| 87 | const parent = P.dirname(bundleRoot); |
| 88 | if (P.basename(parent) === 'versions') { |
| 89 | const installDir = P.dirname(parent); |
| 90 | return exists(installDir) ? installDir : P.dirname(parent); |
| 91 | } |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | export function detectInstallMethod(input: DetectInput): InstallMethod { |
| 96 | const exists = input.exists ?? fs.existsSync; |
no outgoing calls
no test coverage detected