import * as path from "path" ; const inquirer = require ( "inquirer" ) ; const { getPackages } = require ( "@monorepo-utils/package-utils" ) ; const canNpmPublish = require ( "can-npm-publish" ) . canNpmPublish ; import { execUnlessDry , log , logPromise } from "./utils/utils" ; import { asyncFilter } from "./utils/async-utils" ; /**
 * publish packages to npm
 */ export const publish = async ( { projectDir , dry , skipPrompt , ciMode , distTag } ) : Promise < void > => { const packages = getPackages ( projectDir ) ; const publishablePackages = await asyncFilter ( packages , async ( pkg ) => { try { await canNpmPublish ( pkg . location ) ; return true ; } catch ( error ) { return false ; } } ) ; if ( publishablePackages . length === 0 ) { return log ( "No need to publish." ) ; } const answers = skipPrompt ? { publish : true } : await inquirer . prompt ( [ { type : "confirm" , name : "publish" , message : `Do you publish these packages?
${ publishablePackages . map ( ( pkg ) => { let item = `${ pkg . packageJSON . name }@${ pkg . packageJSON . version }` ; if ( distTag ) { item += `, distTag: @${ distTag }` ; } return `- ${ item }` ; } ) . join ( "\n" ) }
` } ] ) ; if ( ! answers [ "publish" ] ) { return log ( "No publish" ) ; } await publishablePackages . reduce ( ( promise , pkg ) => { return promise . then ( ( ) => { const location = pkg . location ; const newVersion = pkg . packageJSON . version ; const args = [ ] ; const npmPath = process . env . npm_execpath ; const isYarn = path . basename ( npmPath || "npm" ) . startsWith ( "yarn" ) ; const bin = isYarn ? "yarn" : "npm" ; if ( isYarn ) { args . push ( "--new-version" , newVersion , "--non-interactive" ) ; } if ( distTag ) { args . push ( "--tag" , distTag ) ; } const task = execUnlessDry ( `${ bin } publish ` + args . join ( " " ) , { cwd : location , dry } ) ; let message = `${ pkg . packageJSON . name }@${ pkg . packageJSON . version }` ; if ( distTag ) { message += `, distTag: @${ distTag }` ; } return logPromise ( task , message , { ciMode : ciMode } ) ; } ) ; } , Promise . resolve ( ) ) ; } ;