import is from '@sindresorhus/is' ; import semver from 'semver' ; import { CONFIG_VALIDATION } from '../../constants/error-messages' ; import { regEx } from '../../util/regex' ; import { GenericVersion , GenericVersioningApi } from '../loose/generic' ; import type { VersioningApiConstructor } from '../types' ; export const id = 'regex' ; export const displayName = 'Regular Expression' ; export const urls = [ ] ; export const supportsRanges = false ; export interface RegExpVersion extends GenericVersion { /**
     * compatibility, if present, are treated as a compatibility layer: we will
     * never try to update to a version with a different compatibility.
     */ compatibility : string ; } function asSemver ( version ) { let vstring = `${ version . release [ 0 ] }.${ version . release [ 1 ] }.${ version . release [ 2 ] }` ; if ( is . nonEmptyString ( version . prerelease ) ) { vstring += `-${ version . prerelease }` ; } return vstring ; } export class RegExpVersioningApi extends GenericVersioningApi < RegExpVersion > { private _config : RegExp = null ; constructor ( _new_config ) { super ( ) ; const new_config = _new_config || '^(?<major>\\d+)?$' ; if ( ! new_config . includes ( '<major>' ) && ! new_config . includes ( '<minor>' ) && ! new_config . includes ( '<patch>' ) ) { const error = new Error ( CONFIG_VALIDATION ) ; error . validationSource = new_config ; error . validationError = 'regex versioning needs at least one major, minor or patch group defined' ; throw error ; } this . _config = regEx ( new_config ) ; } protected _parse ( version ) { const match = this . _config . exec ( version ) ; if ( match === null ) { return null ; } const groups = match . groups ; const release = [ typeof groups . major === 'undefined' ? 0 : Number ( groups . major ) , typeof groups . minor === 'undefined' ? 0 : Number ( groups . minor ) , typeof groups . patch === 'undefined' ? 0 : Number ( groups . patch ) , ] ; if ( groups . build ) { release . push ( Number ( groups . build ) ) ; } return { release , prerelease : groups . prerelease , compatibility : groups . compatibility , } ; } override isCompatible ( version , current ) { return ( this . _parse ( version ) . compatibility === this . _parse ( current ) . compatibility ) ; } override isLessThanRange ( version , range ) { return semver . ltr ( asSemver ( this . _parse ( version ) ) , asSemver ( this . _parse ( range ) ) ) ; } override getSatisfyingVersion ( versions , range ) { return semver . maxSatisfying ( versions . map ( ( v ) => asSemver ( this . _parse ( v ) ) ) , asSemver ( this . _parse ( range ) ) ) ; } override minSatisfyingVersion ( versions , range ) { return semver . minSatisfying ( versions . map ( ( v ) => asSemver ( this . _parse ( v ) ) ) , asSemver ( this . _parse ( range ) ) ) ; } override matches ( version , range ) { return semver . satisfies ( asSemver ( this . _parse ( version ) ) , asSemver ( this . _parse ( range ) ) ) ; } } export const api = RegExpVersioningApi ; export default api ;