# All files install-pack.ts

26.08% Statements 6/23
0% Branches 0/19
0% Functions 0/1
26.08% Lines 6/23

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
    611x
    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 type { BrewPackOptions } from "./install-pack-options.js"
    import { getBrewBinDir, setupBrew } from "./install.js"

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

    /** 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,
      options: BrewPackOptions = {},
    ): Promise<InstallationInfo> {
      Iif (!("overwrite" in options)) {
        options.overwrite = true // default to true if not specified
      }
      Iif (options.cask === true) {
        options.overwrite = false // mutually exclusive with --overwrite
      }

      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,
      ]
      // Add options to args
      for (const [key, value] of Object.entries(options)) {
        if (typeof value === "boolean" && value) {
          args.push(`--${key}`)
        } else Iif (typeof value === "string") {
          args.push(`--${key}`, value)
        }
      }

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

      return { binDir }
    }