import { gatherBumpInfo } from '../bump/gatherBumpInfo' ; import { BeachballOptions } from '../types/BeachballOptions' ; import { gitFailFast , getBranchName , getCurrentHash } from 'workspace-tools' ; import prompts from 'prompts' ; import { initializePackageChangeInfo } from '../changefile/getPackageChangeTypes' ; import { readChangeFiles } from '../changefile/readChangeFiles' ; import { bumpAndPush } from '../publish/bumpAndPush' ; import { publishToRegistry } from '../publish/publishToRegistry' ; import { getNewPackages } from '../publish/getNewPackages' ; import { getPackageInfos } from '../monorepo/getPackageInfos' ; export async function publish ( options ) { const { path : cwd , branch , registry , tag } = options ; const oldPackageInfos = getPackageInfos ( cwd ) ; const changes = readChangeFiles ( options , oldPackageInfos ) ; const packageChangeTypes = initializePackageChangeInfo ( changes ) ; if ( Object . keys ( packageChangeTypes ) . length === 0 ) { console . log ( 'Nothing to bump, skipping publish!' ) ; return ; } const currentBranch = getBranchName ( cwd ) ; const currentHash = getCurrentHash ( cwd ) ; console . log ( `Publishing with the following configuration:

  registry: ${ registry }

  current branch: ${ currentBranch }
  current hash: ${ currentHash }
  target branch: ${ branch }
  tag: ${ tag }

  bumps versions: ${ options . bump ? 'yes' : 'no' }
  publishes to npm registry: ${ options . publish ? 'yes' : 'no' }
  pushes to remote git repo: ${ options . bump && options . push && options . branch ? 'yes' : 'no' }

` ) ; if ( ! options . yes ) { const response = await prompts ( { type : 'confirm' , name : 'yes' , message : 'Is everything correct (use the --yes or -y arg to skip this prompt)?' , } ) ; if ( ! response . yes ) { return ; } } const publishBranch = 'publish_' + String ( new Date ( ) . getTime ( ) ) ; console . log ( `creating temporary publish branch ${ publishBranch }` ) ; gitFailFast ( [ 'checkout' , '-b' , publishBranch ] , { cwd } ) ; if ( options . bump ) { console . log ( 'Bumping version for npm publish' ) ; } const bumpInfo = gatherBumpInfo ( options , oldPackageInfos ) ; if ( options . new ) { bumpInfo . newPackages = new Set < string > ( await getNewPackages ( bumpInfo , options . registry ) ) ; } if ( options . publish ) { await publishToRegistry ( bumpInfo , options ) ; } else { console . log ( 'Skipping publish' ) ; } if ( options . bump && branch && options . push ) { await bumpAndPush ( bumpInfo , publishBranch , options ) ; } else { console . log ( 'Skipping git push and tagging' ) ; } const revParseSuccessful = currentBranch || currentHash ; const inBranch = currentBranch && currentBranch !== 'HEAD' ; const hasHash = currentHash !== null ; if ( inBranch ) { console . log ( `git checkout ${ currentBranch }` ) ; gitFailFast ( [ 'checkout' , currentBranch ! ] , { cwd } ) ; } else if ( hasHash ) { console . log ( `Looks like the repo was detached from a branch` ) ; console . log ( `git checkout ${ currentHash }` ) ; gitFailFast ( [ 'checkout' , currentHash ! ] , { cwd } ) ; } if ( revParseSuccessful ) { console . log ( `deleting temporary publish branch ${ publishBranch }` ) ; gitFailFast ( [ 'branch' , '-D' , publishBranch ] , { cwd } ) ; } }