|
1 | 1 | import * as fs from 'fs/promises'; |
2 | 2 | import * as path from 'path'; |
| 3 | +import * as os from 'os'; |
| 4 | +import { execFile } from 'child_process'; |
| 5 | +import { promisify } from 'util'; |
3 | 6 |
|
4 | | -// Finds jaclang version by scanning site-packages for a jaclang-*.dist-info folder (~1ms, no subprocess). |
5 | | -// Returns undefined if version cannot be determined. |
6 | | -export async function getJacVersion(jacPath: string): Promise<string | undefined> { |
7 | | - try { |
8 | | - const envRoot = path.dirname(path.dirname(jacPath)); |
9 | | - const libDir = path.join(envRoot, 'lib'); |
10 | | - |
11 | | - let libEntries: string[]; |
12 | | - try { libEntries = await fs.readdir(libDir); } |
13 | | - catch { return undefined; } |
14 | | - |
15 | | - for (const libEntry of libEntries.filter(entry => entry.startsWith('python'))) { |
16 | | - for (const pkgDir of ['site-packages', 'dist-packages']) { |
17 | | - try { |
18 | | - const sitePackages = path.join(libDir, libEntry, pkgDir); |
19 | | - const siteEntries = await fs.readdir(sitePackages); |
20 | | - const distInfoDir = siteEntries.find( |
21 | | - entry => entry.startsWith('jaclang-') && entry.endsWith('.dist-info') |
22 | | - ); |
23 | | - if (distInfoDir) { |
24 | | - return distInfoDir.slice('jaclang-'.length, -'.dist-info'.length); |
25 | | - } |
26 | | - } catch { continue; } |
27 | | - } |
| 7 | +const execFileAsync = promisify(execFile); |
| 8 | + |
| 9 | +// Fast path: read version from ~/.cache/jac/rt/*/site/jaclang-*.dist-info (~0.002s). |
| 10 | +async function getJacVersionFromCache(): Promise<string | undefined> { |
| 11 | + const cacheBase = process.env.XDG_CACHE_HOME ?? path.join(os.homedir(), '.cache'); |
| 12 | + const rtDir = path.join(cacheBase, 'jac', 'rt'); |
| 13 | + |
| 14 | + let hashDirs: string[]; |
| 15 | + try { hashDirs = await fs.readdir(rtDir); } |
| 16 | + catch { return undefined; } |
| 17 | + |
| 18 | + const versions: string[] = []; |
| 19 | + |
| 20 | + for (const hashDir of hashDirs) { |
| 21 | + const siteDir = path.join(rtDir, hashDir, 'site'); |
| 22 | + let entries: string[]; |
| 23 | + try { entries = await fs.readdir(siteDir); } |
| 24 | + catch { continue; } |
| 25 | + |
| 26 | + const distInfo = entries.find(e => e.startsWith('jaclang-') && e.endsWith('.dist-info')); |
| 27 | + if (distInfo) { |
| 28 | + versions.push(distInfo.slice('jaclang-'.length, -'.dist-info'.length)); |
28 | 29 | } |
29 | | - return undefined; |
| 30 | + } |
| 31 | + |
| 32 | + // Ambiguous if multiple distinct versions (e.g. dev + release) — fall back to subprocess. |
| 33 | + const unique = [...new Set(versions)]; |
| 34 | + return unique.length === 1 ? unique[0] : undefined; |
| 35 | +} |
| 36 | + |
| 37 | +// Slow fallback (~1.8s): only runs if cache is missing (first-ever launch). |
| 38 | +async function getJacVersionFromBinary(jacPath: string): Promise<string | undefined> { |
| 39 | + try { |
| 40 | + const { stdout } = await execFileAsync(jacPath, ['--version'], { timeout: 5000 }); |
| 41 | + return stdout.match(/Version:\s+([\d.]+)/)?.[1]; |
30 | 42 | } catch { return undefined; } |
31 | 43 | } |
32 | 44 |
|
| 45 | +// Cache first, subprocess fallback if cache is missing. |
| 46 | +export async function getJacVersion(jacPath: string): Promise<string | undefined> { |
| 47 | + return (await getJacVersionFromCache()) ?? (await getJacVersionFromBinary(jacPath)); |
| 48 | +} |
| 49 | + |
33 | 50 | // Compares two semver strings. Returns positive if a > b, negative if a < b, 0 if equal. |
34 | 51 | export function compareVersions(a: string, b: string): number { |
35 | 52 | const aParts = a.split('.').map(Number); |
|
0 commit comments