import { eq , minVersion , satisfies , subset , valid , validRange , major , } from 'semver' ; class InvalidVersionError extends Error { constructor ( ) { super ( 'Invalid version' ) ; } } export function toVersion ( versionOrRange ) { if ( valid ( versionOrRange ) ) { return versionOrRange ; } if ( validRange ( versionOrRange ) ) { const versionObject = minVersion ( versionOrRange ) ; if ( ! versionObject ) throw new InvalidVersionError ( ) ; return versionObject . version ; } throw new InvalidVersionError ( ) ; } function compare ( versionOrRange , target , sign ) { if ( valid ( versionOrRange ) ) { return satisfies ( versionOrRange , sign + target ) ; } if ( validRange ( versionOrRange ) ) { const matches = satisfies ( target , versionOrRange ) ; return subset ( versionOrRange , sign + target ) || ( sign === '>' && matches ) ; } throw new InvalidVersionError ( ) ; } export function getMajor ( versionOrRange ) { const version = toVersion ( versionOrRange ) ; return major ( version ) ; } export function lower ( versionOrRange , target ) { return compare ( versionOrRange , target , '<' ) ; } export function mayBeGreater ( versionOrRange , target ) { return compare ( versionOrRange , target , '>' ) ; } export function greater ( versionOrRange , target ) { const version = toVersion ( versionOrRange ) ; return satisfies ( version , '>' + target ) ; } export function greaterOrEquals ( versionOrRange , target ) { const version = toVersion ( versionOrRange ) ; return satisfies ( version , '>=' + target ) ; } export function equals ( version , target ) { if ( ! valid ( version ) ) { return false ; } return eq ( version , target ) ; }