import { cosmiconfigSync } from 'cosmiconfig' ; import { getDefaultRemoteBranch } from 'workspace-tools' ; import { RepoOptions , CliOptions } from '../types/BeachballOptions' ; let cachedRepoOptions = new Map < CliOptions , RepoOptions > ( ) ; export function getRepoOptions ( cliOptions ) { const { configPath , path : repoRoot , branch } = cliOptions ; if ( cachedRepoOptions . has ( cliOptions ) ) { return cachedRepoOptions . get ( cliOptions ) ! ; } let repoOptions ; if ( configPath ) { repoOptions = tryLoadConfig ( configPath ) ; if ( ! repoOptions ) { console . error ( `Config file "${ configPath }" could not be loaded` ) ; process . exit ( 1 ) ; } } else { repoOptions = trySearchConfig ( ) || ( { } as RepoOptions ) ; } if ( ! branch ) { if ( repoOptions . branch && ! repoOptions . branch . includes ( '/' ) ) { repoOptions . branch = getDefaultRemoteBranch ( repoOptions . branch , repoRoot ) ; } else if ( ! repoOptions . branch ) { repoOptions . branch = getDefaultRemoteBranch ( undefined , cliOptions . path ) ; } } cachedRepoOptions . set ( cliOptions , repoOptions ) ; return repoOptions ; } function tryLoadConfig ( configPath ) { const configExplorer = cosmiconfigSync ( 'beachball' ) ; const loadResults = configExplorer . load ( configPath ) ; return ( loadResults && loadResults . config ) || null ; } function trySearchConfig ( ) { const configExplorer = cosmiconfigSync ( 'beachball' ) ; const searchResults = configExplorer . search ( ) ; return ( searchResults && searchResults . config ) || null ; }