feat: support ubuntu version on Ubuntu-based distros

This commit is contained in:
Amin Yahyaabadi 2023-06-28 16:04:33 -07:00
parent 76c4891c79
commit cfa0cc5326
5 changed files with 28 additions and 12 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -3,18 +3,23 @@ import { getUbuntuVersion } from "ubuntu-version"
import which from "which" import which from "which"
import { setupAptPack } from "../setup/setupAptPack" import { setupAptPack } from "../setup/setupAptPack"
import { isUbuntu } from "./isUbuntu" import { isUbuntu } from "./isUbuntu"
import os from "os"
export async function ubuntuVersion(): Promise<number[] | null> { export async function ubuntuVersion(): Promise<number[] | null> {
try { try {
if (isUbuntu()) { if (isUbuntu()) {
if (which.sync("lsb_release", { nothrow: true }) === null) { try {
await setupAptPack([{ name: "lsb-release" }]) if (which.sync("lsb_release", { nothrow: true }) === null) {
await setupAptPack([{ name: "lsb-release" }])
}
} catch {
return detectUsingOsVersion()
} }
const versionSplitted = await getUbuntuVersion() const versionSplitted = await getUbuntuVersion()
if (versionSplitted.length === 0) { if (versionSplitted.length === 0) {
warning("Failed to get the ubuntu major version.") return detectUsingOsVersion()
return null
} }
return versionSplitted return versionSplitted
@ -26,3 +31,14 @@ export async function ubuntuVersion(): Promise<number[] | null> {
return null return null
} }
} }
/** Detect Ubuntu version using os.version() for Ubuntu based distros */
function detectUsingOsVersion() {
// #46~22.04.1-Ubuntu SMP ...
const osVersion = os.version()
const versionSplitted = osVersion.split(".")
const majorVersion = parseInt(versionSplitted[0].replace("#", ""), 10)
const minorVersion = parseInt(versionSplitted[1].replace("~", ""), 10)
const patchVersion = parseInt(versionSplitted[2].split("-")[0], 10)
return [majorVersion, minorVersion, patchVersion]
}