import { Context } from "./context" ; import { VersionBag } from "./versionBag" ; import { fatal , prereleaseSatisfies } from "./utils" ; import { MonoRepo , MonoRepoKind } from "../common/monoRepo" ; import { Package } from "../common/npmPackage" ; import { FluidRepo } from "../common/fluidRepo" ; param repo param bumpDepPackages param updateLock param release export async function bumpDependencies ( context , commitMessage , bumpDepPackages , updateLock , commit = false , release = false ) { const suffix = release ? "" : "-0" ; const bumpPackages = context . repo . packages . packages . map ( pkg => { const matchName = pkg . monoRepo ? MonoRepoKind [ pkg . monoRepo . kind ] : pkg . name ; const matched = bumpDepPackages . has ( matchName ) ; const version = bumpDepPackages . get ( matchName ) ?? `${ pkg . version }${ suffix }` ; return { matched , pkg , version } ; } ) . filter ( rec => rec . matched ) ; if ( bumpPackages . length === 0 ) { fatal ( "Unable to find dependencies to bump" ) ; } const bumpPackageMap = new Map ( bumpPackages . map ( rec => [ rec . pkg . name , { pkg : rec . pkg , rangeSpec : `^${ rec . version }` } ] ) ) ; return bumpDependenciesCore ( context , commitMessage , bumpPackageMap , updateLock , commit , release ) ; } async function bumpDependenciesCore ( context , commitMessage , bumpPackageMap , updateLock , commit , release ) { let changed = false ; const updateLockPackage = [ ] ; const changedVersion = new VersionBag ( ) ; for ( const pkg of context . repo . packages . packages ) { if ( await bumpPackageDependencies ( pkg , bumpPackageMap , release , changedVersion ) ) { updateLockPackage . push ( pkg ) ; changed = true ; } } if ( await bumpPackageDependencies ( context . templatePackage , bumpPackageMap , release , changedVersion ) ) { changed = true ; } if ( changed ) { if ( updateLockPackage . length !== 0 ) { if ( updateLock ) { if ( ! await FluidRepo . ensureInstalled ( updateLockPackage , false ) ) { fatal ( "Install Failed" ) ; } } else { console . log ( "      SKIPPED: updating lock file" ) ; } } let changedVersionString = [ ] ; for ( const [ name , version ] of changedVersion ) { changedVersionString . push ( `${ name . padStart ( 40 ) } -> ${ version }` ) ; } const changedVersionMessage = changedVersionString . join ( "\n" ) ; const bumpBranch = `dep_${ Date . now ( ) }` ; if ( commit ) { console . log ( `Creating branch ${ bumpBranch }` ) ; await context . createBranch ( bumpBranch ) ; await context . gitRepo . commit ( `${ commitMessage }\n\n${ changedVersionMessage }` , "bump dependencies" ) ; } console . log ( `      ${ commitMessage }` ) ; console . log ( changedVersionMessage ) ; if ( commit ) { console . log ( "======================================================================================================" ) ; console . log ( `Please create PR for branch ${ bumpBranch } targeting ${ context . originalBranchName }` ) ; } return changedVersionMessage ; } else { console . log ( "      No dependencies need to be updated" ) ; } } export function getReleasedPrereleaseDependencies ( context ) { const bumpPackageMap = new Map < string , { pkg : Package ; rangeSpec : string ; } > ( ) ; for ( const pkg of context . repo . packages . packages ) { for ( const dep of pkg . combinedDependencies ) { if ( dep . version . includes ( "-" ) ) { const depPackage = context . fullPackageMap . get ( dep . name ) ; if ( depPackage && ! prereleaseSatisfies ( depPackage . version , dep . version ) ) { bumpPackageMap . set ( dep . name , { pkg : depPackage , rangeSpec : dep . version . substring ( 0 , dep . version . lastIndexOf ( "-" ) ) } ) ; } } } } return bumpPackageMap ; } export async function cleanPrereleaseDependencies ( context , updateLock , commit ) { const releasedPrereleaseDependencies = getReleasedPrereleaseDependencies ( context ) ; if ( releasedPrereleaseDependencies . size === 0 ) { console . log ( "No released prerelease dependencies found." ) ; return ; } console . log ( `Updating released prerelease dependencies` + `\n${ Array . from ( releasedPrereleaseDependencies . keys ( ) ) . join ( "\n  " ) }` ) ; await bumpDependenciesCore ( context , "Remove prelease dependencies on release packages" , releasedPrereleaseDependencies , updateLock , commit , false ) ; } param pkg param bumpPackageMap param release param changedVersion async function bumpPackageDependencies ( pkg , bumpPackageMap , release , changedVersion ? ) { let changed = false ; for ( const { name , dev } of pkg . combinedDependencies ) { const dep = bumpPackageMap . get ( name ) ; if ( dep && ! MonoRepo . isSame ( dep . pkg . monoRepo , pkg . monoRepo ) ) { const depVersion = dep . rangeSpec ; const dependencies = dev ? pkg . packageJson . devDependencies : pkg . packageJson . dependencies ; if ( release ? dependencies [ name ] . startsWith ( `${ depVersion }-` ) : dependencies [ name ] !== depVersion ) { if ( changedVersion ) { changedVersion . add ( dep . pkg , depVersion ) ; } changed = true ; dependencies [ name ] = depVersion ; } } } if ( changed ) { await pkg . savePackageJson ( ) ; } return changed ; }