feat: add overwrite option for brew + enabled by default

Update setupBrewPack.ts
This commit is contained in:
Amin Yahyaabadi 2024-08-20 14:59:51 -07:00
parent 42d0df7db6
commit b7b6d75da7
No known key found for this signature in database
GPG Key ID: F52AF77F636088F0
9 changed files with 45 additions and 22 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -48,7 +48,7 @@ export async function setupBrew(_version: string, _setupDir: string, _arch: stri
})
// add the bin directory to the PATH
binDir = getBrewPath()
binDir = getBrewBinDir()
await addPath(binDir, rcOptions)
return { binDir }
@ -60,7 +60,7 @@ export async function setupBrew(_version: string, _setupDir: string, _arch: stri
*
* Based on the installation script from https://brew.sh
*/
export function getBrewPath() {
export function getBrewBinDir() {
if (process.platform === "darwin") {
if (process.arch === "arm64") {
return "/opt/homebrew/bin/"

View File

@ -21,7 +21,7 @@ export async function setupPowershell(version: string | undefined, _setupDir: st
return { binDir }
}
case "darwin": {
return setupBrewPack("powershell", version, ["--cask"])
return setupBrewPack("powershell", version, { cask: true })
}
case "linux": {
if (isArch()) {

View File

@ -3,17 +3,33 @@ import { info } from "@actions/core"
import { execaSync } from "execa"
import { join } from "patha"
import which from "which"
import { getBrewPath, setupBrew } from "../../brew/brew.js"
import { getBrewBinDir, setupBrew } from "../../brew/brew.js"
import type { InstallationInfo } from "./setupBin.js"
let hasBrew = false
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 */
export async function setupBrewPack(
name: string,
version?: string,
extraArgs: string[] = [],
givenOptions: BrewPackOptions = {},
): Promise<InstallationInfo> {
const options = {
overwrite: true,
cask: false,
args: [],
...givenOptions,
}
info(`Installing ${name} ${version ?? ""} via brew`)
if (!hasBrew || which.sync("brew", { nothrow: true }) === null) {
@ -21,16 +37,23 @@ export async function setupBrewPack(
hasBrew = true
}
const binDir = getBrewPath()
const binDir = getBrewBinDir()
const brewPath = join(binDir, "brew")
// Args
const args = [
"install",
(version !== undefined && version !== "") ? `${name}@${version}` : name,
]
if (options.overwrite) {
args.push("--overwrite")
}
if (options.cask) {
args.push("--cask")
}
// brew is not thread-safe
execaSync(
join(binDir, "brew"),
["install", version !== undefined && version !== "" ? `${name}@${version}` : name, ...extraArgs],
{
stdio: "inherit",
},
)
execaSync(brewPath, args, { stdio: "inherit" })
return { binDir }
}