-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryCheck.ts
More file actions
53 lines (44 loc) · 1.43 KB
/
binaryCheck.ts
File metadata and controls
53 lines (44 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { logForDebugging } from './debug.js'
import { which } from './which.js'
// Session cache to avoid repeated checks
const binaryCache = new Map<string, boolean>()
/**
* Check if a binary/command is installed and available on the system.
* Uses 'which' on Unix systems (macOS, Linux, WSL) and 'where' on Windows.
*
* @param command - The command name to check (e.g., 'gopls', 'rust-analyzer')
* @returns Promise<boolean> - true if the command exists, false otherwise
*/
export async function isBinaryInstalled(command: string): Promise<boolean> {
// Edge case: empty or whitespace-only command
if (!command || !command.trim()) {
logForDebugging('[binaryCheck] Empty command provided, returning false')
return false
}
// Trim the command to handle whitespace
const trimmedCommand = command.trim()
// Check cache first
const cached = binaryCache.get(trimmedCommand)
if (cached !== undefined) {
logForDebugging(
`[binaryCheck] Cache hit for '${trimmedCommand}': ${cached}`,
)
return cached
}
let exists = false
if (await which(trimmedCommand).catch(() => null)) {
exists = true
}
// Cache the result
binaryCache.set(trimmedCommand, exists)
logForDebugging(
`[binaryCheck] Binary '${trimmedCommand}' ${exists ? 'found' : 'not found'}`,
)
return exists
}
/**
* Clear the binary check cache (useful for testing)
*/
export function clearBinaryCache(): void {
binaryCache.clear()
}