# All files install.ts

40% Statements 14/35
25% Branches 2/8
25% Functions 1/4
40% Lines 14/35

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
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    981x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

    1x

    1x

    1x
    1x
    1x
    1x

     import { tmpdir } from "os"
    import { dirname, join } from "path"
    import { type AddPathOptions, addPath } from "envosman"
    import { execaSync } from "execa"
    import { DownloaderHelper } from "node-downloader-helper"
    import { installAptPack } from "setup-apt"
    import which from "which"
    import type { InstallationInfo } from "./InstallationInfo.js"

    /* eslint-disable require-atomic-updates */
    let binDir: string | undefined

    export type SetupBrewOptions = {
      /** Options for adding the brew path to the rc file */
      rcOptions?: AddPathOptions
      /** (Unsupported option) The version of brew to install */
      version?: never
      /** (Unsupported option) The directory where brew should be installed */
      setupDir?: never
      /** (Unsupported option) The architecture of the system */
      arch?: never
    }

    export async function setupBrew(options: SetupBrewOptions = {}): Promise<InstallationInfo | undefined> {
      // brew is only available on darwin and linux
      Iif (!["darwin", "linux"].includes(process.platform)) {
        return undefined
      }

      // check if the function has already been called
      Iif (typeof binDir === "string") {
        return { binDir }
      }

      // check if brew is already installed
      const maybeBinDir = await which("brew", { nothrow: true })
      if (maybeBinDir !== null) {
        binDir = dirname(maybeBinDir)
        return { binDir }
      }

      // download the installation script
      await installAptPack([{ name: "ca-certificates" }])
      const dl = new DownloaderHelper("https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh", tmpdir(), {
        fileName: "install-brew.sh",
      })
      dl.on("error", (err) => {
        throw new Error(`Failed to download the brew installer script: ${err}`)
      })
      await dl.start()

      // brew installation is not thread-safe
      execaSync("/bin/bash", [dl.getDownloadPath()], {
        stdio: "inherit",
        env: {
          NONINTERACTIVE: "1",
        },
      })

      // add the bin directory to the PATH
      binDir = getBrewBinDir()
      await addPath(binDir, options.rcOptions)

      return { binDir }
    }

    /**
     * Get the path to the bin directory of brew
     * @returns {string} The path where brew binary is installed
     *
     * Based on the installation script from https://brew.sh
     */
    export function getBrewBinDir() {
      return join(getBrewDir(), "bin")
    }

    /**
     * Get the path where brew is installed
     * @returns {string} The path where brew is installed
     *
     * Based on the installation script from https://brew.sh
     */
    export function getBrewDir() {
      Iif (process.platform === "darwin") {
        if (process.arch === "arm64") {
          return "/opt/homebrew"
        } else {
          return "/usr/local"
        }
      }

      Iif (process.platform === "linux") {
        return "/home/linuxbrew/.linuxbrew"
      }

      throw new Error("Unsupported platform for brew")
    }