Skip to content

Commit

Permalink
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions core/src/util/validateInstall.ts
Original file line number Diff line number Diff line change
@@ -11,27 +11,25 @@ import { RuntimeError } from "../exceptions"
import { deline } from "./string"
import { exec } from "./util"

/**
* throws if version check fails or the version is too old
*/
export async function validateInstall(params: {
type BinaryVersionCheckParams = {
name: string
versionCommand: { cmd: string; args: string[] }
versionRegex: RegExp
minVersion: string
}) {
let version: string | undefined = undefined
const versionDetectFailure = new RuntimeError(
}

const versionDetectFailure = (params: BinaryVersionCheckParams) =>
new RuntimeError(
deline`
Could not detect ${params.name} version.
Please make sure ${params.name} version ${params.minVersion} or later is installed and on your PATH.
`,
{}
)

async function execVersionCheck(params: BinaryVersionCheckParams): Promise<string> {
try {
const versionOutput = (await exec(params.versionCommand.cmd, params.versionCommand.args)).stdout
version = versionOutput.split("\n")[0].match(params.versionRegex)?.[1]
return (await exec(params.versionCommand.cmd, params.versionCommand.args)).stdout
} catch (error) {
throw new RuntimeError(
deline`
@@ -41,18 +39,32 @@ export async function validateInstall(params: {
{ error }
)
}
}

if (!version) {
throw versionDetectFailure
function parseVersionOutput(versionOutput: string, params: BinaryVersionCheckParams): string {
const versionOutputFirstLine = versionOutput.split("\n")[0]
const match = versionOutputFirstLine.match(params.versionRegex)
if (!match || match.length < 2) {
throw versionDetectFailure(params)
}
return match[1]
}

let versionGte = true

function validateVersionNumber(version: string, params: BinaryVersionCheckParams): boolean {
try {
versionGte = semver.gte(version, params.minVersion)
return semver.gte(version, params.minVersion)
} catch (_) {
throw versionDetectFailure
throw versionDetectFailure(params)
}
}

/**
* throws if version check fails or the version is too old
*/
export async function validateInstall(params: BinaryVersionCheckParams): Promise<void> {
const versionOutput = await execVersionCheck(params)
const version = parseVersionOutput(versionOutput, params)
const versionGte = validateVersionNumber(version, params)

if (!versionGte) {
throw new RuntimeError(

0 comments on commit 383f2cb

Please sign in to comment.