# All files install-pack.ts

28.57% Statements 6/21
0% Branches 0/14
0% Functions 0/1
28.57% Lines 6/21

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
    671x
    1x
    1x
    1x

    1x

    1x

     import { join } from "path"
    import { info } from "ci-log"
    import { execaSync } from "execa"
    import which from "which"
    import type { InstallationInfo } from "./InstallationInfo.js"
    import { getBrewBinDir, setupBrew } from "./install.js"

    /* eslint-disable require-atomic-updates */
    let hasBrew = false

    export type BrewPackOptions = {
      /** Whether to overwrite the package if it already exists */
      overwrite?: boolean
      /** Whether to install the package as a cask */
      cask?: boolean
      /** Extra args */
      args?: string[]
    }

    /** A function that installs a package using brew
     *
     * @param name The name of the package
     * @param version The version of the package (optional)
     * @param options The options for installing the package
     *
     * @returns The installation information
     */
    export async function installBrewPack(
      name: string,
      version?: string,
      givenOptions: BrewPackOptions = {},
    ): Promise<InstallationInfo> {
      const options = {
        overwrite: true,
        cask: false,
        args: [],
        ...givenOptions,
      }

      info(`Installing ${name} ${version ?? ""} via brew`)

      Iif (!hasBrew || which.sync("brew", { nothrow: true }) === null) {
        await setupBrew()
        hasBrew = true
      }

      const binDir = getBrewBinDir()
      const brewPath = join(binDir, "brew")

      // Args
      const args = [
        "install",
        (version !== undefined && version !== "") ? `${name}@${version}` : name,
      ]
      Iif (options.overwrite) {
        args.push("--overwrite")
      }
      Iif (options.cask) {
        args.push("--cask")
      }

      // brew is not thread-safe
      execaSync(brewPath, args, { stdio: "inherit" })

      return { binDir }
    }