# All files is-installed.ts

10% Statements 2/20
0% Branches 0/2
0% Functions 0/4
11.11% Lines 2/18

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
    42
    432x
    2x

     import { execa } from "execa"
    import { getAptEnv } from "./apt-env.js"

    /**
     * Check if a package is installed
     * @param pack The package to check
     * @returns `true` if the package is installed, `false` otherwise
     */
    export async function isAptPackInstalled(pack: string) {
      try {
        // check if a package is installed
        const { stdout } = await execa("dpkg", ["-s", pack], { env: getAptEnv("apt-get"), stdio: "pipe" })
        Iif (typeof stdout !== "string") {
          return false
        }
        const lines = stdout.split("\n")
        // check if the output contains a line that starts with "Status: install ok installed"
        return lines.some((line) => line.startsWith("Status: install ok installed"))
      } catch {
        return false
      }
    }

    /**
     * Check if a package matching a regexp is installed
     * @param regexp The regexp to check
     * @returns `true` if a package is installed, `false` otherwise
     */
    export async function isAptPackRegexInstalled(regexp: string) {
      try {
        // check if a package matching the regexp is installed
        const { stdout } = await execa("dpkg", ["-l", regexp], { env: getAptEnv("apt-get"), stdio: "pipe" })
        Iif (typeof stdout !== "string") {
          return false
        }
        const lines = stdout.split("\n")
        // check if the output contains any lines that start with "ii"
        return lines.some((line) => line.startsWith("ii"))
      } catch {
        return false
      }
    }