feat: add support for adding repositories to apt

This commit is contained in:
Amin Yahyaabadi 2021-09-16 08:38:38 -05:00
parent 8ab3e55ee8
commit cf3dbb2b16
1 changed files with 9 additions and 5 deletions

View File

@ -1,19 +1,23 @@
/* eslint-disable require-atomic-updates */ /* eslint-disable require-atomic-updates */
import { exec } from "@actions/exec" import { exec } from "@actions/exec"
import { mightSudo } from "./sudo"
let didUpdate: boolean = false let didUpdate: boolean = false
/** A function that installs a package using apt */ /** A function that installs a package using apt */
export async function setupAptPack(name: string, version?: string, updateRepositories: boolean = true) { export async function setupAptPack(name: string, version?: string, repository: boolean | string = true) {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions, @typescript-eslint/no-unnecessary-condition const apt = mightSudo("apt-get")
const apt = process.env.CI || process.getuid?.() === 0 ? "sudo apt-get" : "apt-get"
if (!didUpdate || updateRepositories) { let exit = 0
if (!didUpdate || repository === true) {
await exec(apt, ["update"]) await exec(apt, ["update"])
didUpdate = true didUpdate = true
} else if (typeof repository === "string") {
exit = await exec(mightSudo("add-apt-repository"), ["--update", repository])
} }
const exit = await exec(apt, ["install", version !== undefined && version !== "" ? `${name}=${version}` : name]) exit = await exec(apt, ["install", version !== undefined && version !== "" ? `${name}=${version}` : name])
if (exit !== 0) { if (exit !== 0) {
throw new Error(`Failed to install ${name} ${version}`) throw new Error(`Failed to install ${name} ${version}`)