import semver from 'semver' ; import { AppMetaInfo } from '../clients/infra/Apps' ; export const removeBuild = ( id ) : string => id . split ( '+' ) [ 0 ] ; export const removeVersionFromAppId = ( appId ) : string => appId . split ( '@' ) [ 0 ] ; export const extractVersionFromAppId = ( appId ) : string => appId . split ( '@' ) . slice ( - 1 ) [ 0 ] ; export const transformToLinkedLocator = ( appId ) => appId . replace ( /\+build.*$/ , '+linked' ) ; export const formatLocator = ( name , versionAndBuild ) : string => `${ name }@${ removeBuild ( versionAndBuild ) }` ; export const isLinkedApp = ( app ) => app . id . includes ( '+build' ) ; export const parseAppId = ( appId ) : ParsedLocator => { const name = removeVersionFromAppId ( appId ) ; const version = extractVersionFromAppId ( appId ) ; const splittedVersion = version . split ( '+' ) ; return { build : splittedVersion [ 1 ] , locator : formatLocator ( name , version ) , name , version : splittedVersion [ 0 ] , } ; } ; export const formatAppId = ( { locator , build } ) => ( build ? `${ locator }+${ build }` : locator ) ; export const satisfies = ( appId , version ) : boolean => { const { version : appVer } = parseAppId ( appId ) ; return semver . satisfies ( appVer , version ) ; } ; export const versionToMajor = ( version ) : string => version . split ( '.' ) [ 0 ] ; export const versionToMajorRange = ( version ) : string => `${ versionToMajor ( version ) }.x` ; export const formatMajorLocator = ( name , version ) : string => { const majorRange = versionToMajorRange ( version ) ; return `${ name }@${ majorRange }` ; } ; export const appIdToAppAtMajor = ( appId ) : string => { const { name , version } = parseAppId ( appId ) ; const majorRange = versionToMajorRange ( version ) ; return `${ name }@${ majorRange }` ; } ; const APP_ID_REGEX = /^[\w\-]+\.[\w\-]+@(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?$/ ; export const isValidAppIdOrLocator = ( appId ) : boolean => { return APP_ID_REGEX . test ( appId ) ; } ; export const sameMajor = ( v1 , v2 ) => versionToMajor ( v1 ) === versionToMajor ( v2 ) ; export const majorEqualAndGreaterThan = ( v1 , v2 ) => sameMajor ( v1 , v2 ) && semver . gt ( v1 , v2 ) ; export interface ParsedLocator { name : string ; version : string ; locator : string ; build ? : string ; }