import { Context , VersionBumpType } from "./context" ; import { getReleasedPrereleaseDependencies } from "./bumpDependencies" ; import { bumpRepo } from "./bumpVersion" ; import { ReferenceVersionBag , getRepoStateChange } from "./versionBag" ; import { fatal , runPolicyCheckWithFix , } from "./utils" ; import { MonoRepoKind } from "../common/monoRepo" ; import { Package } from "../common/npmPackage" ; import * as semver from "semver" ; /**
 * Create release branch based on the repo state, bump minor version immediately
 * and push it to `main` and the new release branch to remote
 */ export async function createReleaseBranch ( context ) { await runPolicyCheckWithFix ( context . gitRepo ) ; const remote = await context . gitRepo . getRemote ( context . originRemotePartialUrl ) ; if ( ! remote ) { fatal ( `Unable to find remote for '${ context . originRemotePartialUrl }'` ) ; } if ( context . originalBranchName !== "main" ) { console . warn ( "WARNING: Bumping minor version outside of main branch is not normal!  Make sure you know what you are doing." ) ; } else if ( ! await context . gitRepo . isBranchUpToDate ( "main" , remote ) ) { fatal ( `Local 'main' branch not up to date with remote. Please pull from '${ remote }'.` ) ; } const releasedPrereleaseDependencies = getReleasedPrereleaseDependencies ( context ) ; if ( releasedPrereleaseDependencies . size !== 0 ) { fatal ( `Prelease dependencies for released package found. ` + `Run 'bump-version --update' and submit the PR to update the dependencies first.` + `\n  ${ Array . from ( releasedPrereleaseDependencies . keys ( ) ) . join ( "\n  " ) }` ) ; } const releaseName = MonoRepoKind [ MonoRepoKind . Client ] ; const depVersions = await context . collectBumpInfo ( releaseName ) ; const releaseVersion = depVersions . repoVersions . get ( releaseName ) ; if ( ! releaseVersion ) { fatal ( `Missing ${ releaseName } packages` ) ; } const releaseBranchVersion = `${ semver . major ( releaseVersion ) }.${ semver . minor ( releaseVersion ) }` ; const releaseBranch = `release / ${ releaseBranchVersion }` ; const commit = await context . gitRepo . getShaForBranch ( releaseBranch ) ; if ( commit ) { fatal ( `${ releaseBranch } already exists` ) ; } const bumpBranch = `minor_bump_${ releaseBranchVersion }_${ Date . now ( ) }` ; console . log ( `Creating branch ${ bumpBranch }` ) ; await context . createBranch ( bumpBranch ) ; if ( ! await context . repo . install ( ) ) { fatal ( "Install failed" ) ; } console . log ( `Bumping minor version for development` ) ; console . log ( await bumpCurrentBranch ( context , "minor" , releaseName , depVersions ) ) ; console . log ( "======================================================================================================" ) ; console . log ( `Please create PR for branch ${ bumpBranch } targeting ${ context . originalBranchName } ` ) ; console . log ( `After PR is merged, create branch ${ releaseBranch } one commit before the merged PR and push to the repo.` ) ; console . log ( `Then--release can be use to start the release.` ) ; } param versionBump param serverNeedBump param packageNeedBump param oldVersions async function bumpCurrentBranch ( context , versionBump , releaseName , depVersions ) { let clientNeedBump = false ; let serverNeedBump = false ; const packageNeedBump = new Set < Package > ( ) ; for ( const [ name ] of depVersions ) { if ( depVersions . needBump ( name ) ) { if ( name === MonoRepoKind [ MonoRepoKind . Client ] ) { clientNeedBump = true ; } else if ( name === MonoRepoKind [ MonoRepoKind . Server ] ) { serverNeedBump = true ; } else { const pkg = context . fullPackageMap . get ( name ) ; if ( pkg ) { packageNeedBump . add ( pkg ) ; } } } } const newVersions = await bumpRepo ( context , versionBump , clientNeedBump , serverNeedBump , packageNeedBump ) ; const repoState = getRepoStateChange ( depVersions . repoVersions , newVersions ) ; const releaseNewVersion = newVersions . get ( releaseName ) ; const currentBranchName = await context . gitRepo . getCurrentBranchName ( ) ; console . log ( `  Committing ${ releaseName } version bump to ${ releaseNewVersion } into ${ currentBranchName } ` ) ; await context . gitRepo . commit ( `[bump] package version to ${ releaseNewVersion } for development after ${ releaseName . toLowerCase ( ) } release\n${ repoState } ` , "create bumped version commit" ) ; return `Repo Versions in branch ${ currentBranchName }: ${ repoState } ` ; }