All files pack-install-dir.ts

10.25% Statements 4/39
0% Branches 0/12
0% Functions 0/4
10.25% Lines 4/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 851x 1x 1x 1x                                                                                                                                                                  
import { existsSync } from "fs"
import { join } from "path"
import { execa } from "execa"
import { getBrewBinPath, getBrewDir } from "./install.js"
 
export function brewPackNameAndVersion(name: string, version: string | undefined) {
  return (version !== undefined && version !== "") ? `${name}@${version}` : name
}
 
/**
 * Get the installation directory of a package
 * @param name The name of the package
 * @param nameAndVersion The name and version of the package
 * @returns The installation directory of the package
 */
 
export async function brewPackInstallDir(name: string, version: string | undefined) {
  const nameAndVersion = brewPackNameAndVersion(name, version)
 
  // first try with --prefix
  const nameAndVersionPrefix = await getBrewPackPrefix(nameAndVersion)
  Iif (nameAndVersionPrefix !== undefined) {
    return nameAndVersionPrefix
  }
 
  // try with --prefix name
  const namePrefix = await getBrewPackPrefix(name)
  Iif (namePrefix !== undefined) {
    return namePrefix
  }
 
  // if that fails, try with searchInstallDir
  return searchInstallDir(name, nameAndVersion)
}
 
async function getBrewPackPrefix(packArg: string) {
  try {
    const brewPath = getBrewBinPath()
    return (await execa(brewPath, ["--prefix", packArg], { stdio: "pipe" })).stdout
  } catch {
    return undefined
  }
}
/**
 * Searches for the installation directory of a package
 * @param name The name of the package
 * @param nameAndVersion The name and version of the package
 * @returns The installation directory of the package
 */
function searchInstallDir(name: string, nameAndVersion: string) {
  const brewDir = getBrewDir()
 
  // Check in opt directory (most common location)
  const nameAndVersionOptDir = join(brewDir, "opt", nameAndVersion)
  Iif (existsSync(nameAndVersionOptDir)) {
    return nameAndVersionOptDir
  }
  const nameOptDir = join(brewDir, "opt", name)
  Iif (existsSync(nameOptDir)) {
    return nameOptDir
  }
 
  // Check in Cellar (where casks and some formulae are installed)
  const nameAndVersionCellarDir = join(brewDir, "Cellar", nameAndVersion)
  Iif (existsSync(nameAndVersionCellarDir)) {
    return nameAndVersionCellarDir
  }
  const nameCellarDir = join(brewDir, "Cellar", name)
  Iif (existsSync(nameCellarDir)) {
    return nameCellarDir
  }
 
  // Check in lib directory
  const nameAndVersionLibDir = join(brewDir, "lib", nameAndVersion)
  Iif (existsSync(nameAndVersionLibDir)) {
    return nameAndVersionLibDir
  }
  const nameLibDir = join(brewDir, "lib", name)
  Iif (existsSync(nameLibDir)) {
    return nameLibDir
  }
 
  return undefined
}