2023-04-22 17:19:33 +08:00
|
|
|
import { execaSync } from "execa"
|
2024-08-15 09:22:33 +08:00
|
|
|
import { addPath } from "os-env"
|
2023-04-23 16:09:38 +08:00
|
|
|
import { pathExists } from "path-exists"
|
2022-08-21 06:38:51 +08:00
|
|
|
import { dirname } from "patha"
|
2021-09-15 05:01:08 +08:00
|
|
|
import which from "which"
|
2024-08-16 06:22:07 +08:00
|
|
|
import { rcOptions } from "../cli-options.js"
|
|
|
|
import type { InstallationInfo } from "../utils/setup/setupBin.js"
|
2021-09-15 05:01:08 +08:00
|
|
|
|
2024-08-15 09:22:33 +08:00
|
|
|
/* eslint-disable require-atomic-updates */
|
2021-09-17 02:47:12 +08:00
|
|
|
let binDir: string | undefined
|
|
|
|
|
2022-05-13 03:55:00 +08:00
|
|
|
export async function setupChocolatey(
|
2021-09-17 02:47:12 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
_version: string,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2021-11-22 02:46:34 +08:00
|
|
|
_setupDir: string,
|
2021-09-17 02:47:12 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2023-07-25 04:20:04 +08:00
|
|
|
_arch: string,
|
2022-05-13 03:55:00 +08:00
|
|
|
): Promise<InstallationInfo | undefined> {
|
2021-09-15 13:37:37 +08:00
|
|
|
if (process.platform !== "win32") {
|
2021-09-17 02:47:12 +08:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof binDir === "string") {
|
|
|
|
return { binDir }
|
2021-09-15 13:37:37 +08:00
|
|
|
}
|
|
|
|
|
2021-09-17 02:47:12 +08:00
|
|
|
const maybeBinDir = which.sync("choco", { nothrow: true })
|
|
|
|
if (maybeBinDir !== null) {
|
2021-09-17 18:15:52 +08:00
|
|
|
binDir = dirname(maybeBinDir)
|
2021-09-17 02:47:12 +08:00
|
|
|
return { binDir }
|
2021-09-15 05:01:08 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 18:41:20 +08:00
|
|
|
let powershell = "powershell.exe"
|
|
|
|
const maybePowerShell = which.sync(`${process.env.SystemRoot}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`, {
|
|
|
|
nothrow: true,
|
|
|
|
})
|
|
|
|
if (maybePowerShell !== null) {
|
|
|
|
powershell = maybePowerShell
|
|
|
|
}
|
|
|
|
|
2021-09-15 05:01:08 +08:00
|
|
|
// https://docs.chocolatey.org/en-us/choco/setup#install-with-cmd.exe
|
2023-04-22 17:19:33 +08:00
|
|
|
execaSync(
|
2022-02-14 08:06:21 +08:00
|
|
|
powershell,
|
|
|
|
[
|
|
|
|
"-NoProfile",
|
|
|
|
"-InputFormat",
|
|
|
|
"None",
|
|
|
|
"-ExecutionPolicy",
|
|
|
|
"Bypass",
|
|
|
|
"-Command",
|
|
|
|
"[System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))",
|
|
|
|
],
|
2023-07-25 04:20:04 +08:00
|
|
|
{ stdio: "inherit" },
|
2022-02-14 08:06:21 +08:00
|
|
|
)
|
2021-12-06 18:41:20 +08:00
|
|
|
|
|
|
|
const chocoPath = `${process.env.ALLUSERSPROFILE}\\chocolatey\\bin`
|
2024-08-15 09:43:53 +08:00
|
|
|
await addPath(chocoPath, rcOptions)
|
2021-09-17 02:47:12 +08:00
|
|
|
|
2021-09-17 06:44:13 +08:00
|
|
|
const maybeChoco = which.sync("choco", { nothrow: true })
|
|
|
|
if (maybeChoco !== null) {
|
|
|
|
binDir = dirname(maybeChoco)
|
|
|
|
} else {
|
2022-01-31 06:40:42 +08:00
|
|
|
binDir = `${process.env.ChocolateyInstall ?? "C:/ProgramData/chocolatey"}/bin`
|
2021-09-17 06:44:13 +08:00
|
|
|
}
|
2021-09-17 04:08:54 +08:00
|
|
|
|
2022-11-23 12:51:18 +08:00
|
|
|
if (await pathExists(binDir)) {
|
2021-09-17 04:08:54 +08:00
|
|
|
return { binDir }
|
|
|
|
}
|
|
|
|
return undefined
|
2021-09-15 05:01:08 +08:00
|
|
|
}
|