import yargs , { Arguments } from 'yargs' ; import Branch from '../wrapper-classes/branch' ; yargs . completion ( 'completion' , ( current , argv ) => { const branchArg = getBranchArg ( current , argv ) ; if ( branchArg === null ) { return ; } return Branch . allBranchesWithFilter ( { filter : ( b ) => b . name . startsWith ( branchArg ) , } ) . map ( ( b ) => b . name ) . sort ( ) ; } ) ; /**
 * If the user is entering a branch argument, returns the current entered
 * value. Else, returns null.
 *
 * e.g.
 *
 * gt branch checkout --branch ny--xyz => 'ny--xyz'
 * gt branch checkout --branch => ''
 *
 * gt repo sync => null
 * gt log => null
 */ function getBranchArg ( current , argv ) { const currentCommand = argv [ '_' ] . join ( ' ' ) ; if ( ( currentCommand . includes ( 'branch checkout' ) || currentCommand . includes ( 'b checkout' ) ) && 'branch' in argv ) { if ( current === '--branch' ) { return '' ; } else if ( current === argv [ 'branch' ] ) { return current ; } } if ( argv [ '_' ] . includes ( 'bco' ) && argv [ '_' ] . length <= 3 && typeof current === 'string' ) { return current ; } if ( currentCommand . includes ( 'b co' ) && argv [ '_' ] . includes ( 'b' ) && argv [ '_' ] . includes ( 'co' ) && argv [ '_' ] . length <= 4 && typeof current === 'string' ) { return current ; } if ( currentCommand . includes ( 'upstack onto' ) || currentCommand . includes ( 'us onto' ) ) { if ( argv [ '_' ] . length <= 4 && typeof current === 'string' ) { return current ; } } return null ; }