All files alternatives.ts

0% Statements 0/6
0% Branches 0/4
0% Functions 0/2
0% Lines 0/6

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                                                                                 
import { promises } from "fs"
import { execRoot } from "admina"
import { GITHUB_ACTIONS } from "ci-info"
import { sourceRC } from "envosman"
import type { RcOptions } from "envosman/dist/rc-file.js"
const { appendFile } = promises
 
/**
 * Update the alternatives for a package
 * @param name The name of the package
 * @param path The path to the binary
 * @param priority The priority of the alternative (Defaults to `40`)
 */
export async function updateAptAlternatives(name: string, path: string, priority: number = 40) {
  await execRoot("update-alternatives", ["--install", `/usr/bin/${name}`, name, path, priority.toString()])
}
 
/**
 * Add the update-alternatives command to the rc file
 * @param name The name of the package
 * @param path The path to the binary
 * @param rcOptions The options for the rc file to add the update-alternatives command to
 * @param priority The priority of the alternative (Defaults to `40`)
 */
export async function addUpdateAlternativesToRc(
  name: string,
  path: string,
  rcOptions: RcOptions,
  priority: number = 40,
) {
  if (GITHUB_ACTIONS) {
    await updateAptAlternatives(name, path, priority)
  } else {
    await sourceRC(rcOptions)
    await appendFile(
      rcOptions.rcPath,
      `\nif [ $UID -eq 0 ]; then update-alternatives --install /usr/bin/${name} ${name} ${path} ${priority}; fi\n`,
    )
  }
}