import semver from "semver" ; import { SystemRequirement , SystemRequirementError , SystemRequirementResolution } from "./models" ; param resolution param actualVersion param requirement returns see SystemRequirementError export const validateVersionRequirement = ( resolution , actualVersion , requirement ) : SystemRequirementResolution | SystemRequirementError => { if ( ! requirement . version ) { return resolution ; } try { if ( versionIsSatisfied ( actualVersion , requirement . version ) ) { return resolution ; } return { ... resolution , error : true , message : `'${ resolution . command }' version is '${ actualVersion }' but doesn't satisfy requirement '${ requirement . version }'. Please update.` , actualVersion : actualVersion , neededVersion : requirement . version , } ; } catch { return { ... resolution , error : true , message : `Couldn't parse the version ${ actualVersion }. This is not a valid semver version.` , actualVersion : actualVersion , neededVersion : requirement . version , } ; } } ; export const versionIsSatisfied = ( version , requirement ) : boolean => { const cleanedVersion = semver . coerce ( version ) ; if ( ! cleanedVersion ) { throw new Error ( `Invalid version ${ version }.` ) ; } return semver . satisfies ( cleanedVersion , requirement , true ) ; } ;