import { ChangeFileInfo } from '../types/ChangeInfo' ; import { getChangePath } from '../paths' ; import { getBranchName , stage , commit } from 'workspace-tools' ; import fs from 'fs-extra' ; import path from 'path' ; import { v4 as uuidv4 } from 'uuid' ; returns export function writeChangeFiles ( { changes , cwd , commitChangeFiles = true , groupChanges = false , } ) { const changePath = getChangePath ( cwd ) ; const branchName = getBranchName ( cwd ) ; if ( ! ( Object . keys ( changes ) . length && branchName && changePath ) ) { return [ ] ; } if ( ! fs . existsSync ( changePath ) ) { fs . mkdirpSync ( changePath ) ; } const getChangeFile = ( prefix ) => path . join ( changePath , `${ prefix }-${ uuidv4 ( ) }.json` ) ; let changeFiles ; if ( groupChanges ) { const changeFile = getChangeFile ( 'change' ) ; changeFiles = [ changeFile ] ; fs . writeFileSync ( changeFile , JSON . stringify ( { changes } , null , 2 ) ) ; } else { changeFiles = changes . map ( change => { const changeFile = getChangeFile ( change . packageName . replace ( /[^a-zA-Z0-9@]/g , '-' ) ) ; fs . writeJSONSync ( changeFile , change , { spaces : 2 } ) ; return changeFile ; } ) ; } stage ( changeFiles , cwd ) ; if ( commitChangeFiles ) { const commitOptions = [ '--only' , path . join ( changePath , '*.json' ) ] ; commit ( 'Change files' , cwd , commitOptions ) ; } console . log ( `git ${ commitChangeFiles ? 'committed' : 'staged' } these change files: ${ changeFiles . map ( f => ` - ${ f }` ) . join ( '\n' ) }` ) ; return changeFiles ; }