diff --git a/src/utils/setup/setupChoco.ts b/src/utils/setup/setupChoco.ts new file mode 100644 index 00000000..07fdcd0b --- /dev/null +++ b/src/utils/setup/setupChoco.ts @@ -0,0 +1,21 @@ +import { exec } from "@actions/exec" +import which from "which" +import { setupChocolatey } from "../../chocolatey/chocolatey" + +/** A function that installs a package using choco */ +export async function setupChoco(name: string, version?: string) { + if (which.sync("choco", { nothrow: true }) === null) { + await setupChocolatey() + } + + let exit + if (version === undefined) { + exit = await exec("choco", ["install", name]) + } else { + exit = await exec("choco", ["install", name, `--version=${version}`]) + } + + if (exit !== 0) { + throw new Error(`Failed to install ${name} ${version}`) + } +}