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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { tmpdir } from "os"
import { dirname, join } from "path"
import { warning } from "ci-log"
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
}
/**
* Install brew
*
* @param options - Options for adding the brew path to the rc file
* @returns The path to the bin directory of brew if brew is installed, otherwise undefined if brew is not supported on the current platform
*/
export async function setupBrew(options: SetupBrewOptions = {}): Promise<InstallationInfo | undefined> {
Iif (
// brew is only available on darwin and linux
!["darwin", "linux"].includes(process.platform)
// brew is only supported on Linux x64
|| (process.platform === "linux" && process.arch !== "x64")
) {
warning(`Brew is not supported on ${process.platform} ${process.arch}`)
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")
}
export function getBrewBinPath() {
return join(getBrewBinDir(), "brew")
}
/**
* 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")
}
|