Filemedium importancesource

binaryCheck.ts

utils/binaryCheck.ts

No strong subsystem tag
54
Lines
1466
Bytes
2
Exports
2
Imports
10
Keywords

What this is

This page documents one file from the repository and includes its full source so you can read it without leaving the docs site.

Beginner explanation

This file is one piece of the larger system. Its name, directory, imports, and exports show where it fits. Start by reading the exports and related files first.

How it is used

Start from the exports list and related files. Those are the easiest clues for where this file fits into the system.

Expert explanation

Architecturally, this file intersects with general runtime concerns. It contains 54 lines, 2 detected imports, and 2 detected exports.

Important relationships

Detected exports

  • isBinaryInstalled
  • clearBinaryCache

Keywords

commandexiststrimmedcommandcachelogfordebuggingwhichbinarycachecheckcachedbinary

Detected imports

  • ./debug.js
  • ./which.js

Source notes

This page embeds the full file contents. Small or leaf files are still indexed honestly instead of being over-explained.

Open parent directory

Full source

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()
}