import chalk from 'chalk' ; import yargs from 'yargs' ; import { postTelemetryInBackground , registerSigintHandler } from '.' ; import { version } from '../../../package.json' ; import { init } from '../../actions/init' ; import { execStateConfig , repoConfig } from '../config' ; import { ConfigError , ExitCancelledError , ExitFailedError , KilledError , MultiParentError , PreconditionsFailedError , RebaseConflictError , SiblingBranchError , ValidationFailedError , } from '../errors' ; import { logError , logInfo , logNewline , logWarn , parseArgs , VALIDATION_HELPER_MESSAGE , } from '../utils' ; import { printGraphiteMergeConflictStatus } from '../utils/merge_conflict_help' ; import { getUserEmail } from './context' ; import tracer from './tracer' ; export async function profile ( args , canonicalName , handler ) { const parsedArgs = parseArgs ( args ) ; const start = Date . now ( ) ; registerSigintHandler ( { commandName : parsedArgs . command , canonicalCommandName : canonicalName , startTime : start , } ) ; if ( parsedArgs . command !== 'repo init' && ! repoConfig . getTrunk ( ) ) { logInfo ( `Graphite has not been initialized, attempting to setup now...` ) ; logNewline ( ) ; await init ( ) ; } try { await tracer . span ( { name : 'command' , resource : parsedArgs . command , meta : { user : getUserEmail ( ) || 'NotFound' , version : version , args : parsedArgs . args , alias : parsedArgs . alias , } , } , async ( ) => { try { await handler ( ) ; } catch ( err ) { switch ( err . constructor ) { case ExitFailedError : logError ( err . message ) ; throw err ; case PreconditionsFailedError : logInfo ( err . message ) ; throw err ; case RebaseConflictError : logNewline ( ) ; logError ( `Rebase conflict. ${ err . message }` ) ; logNewline ( ) ; printGraphiteMergeConflictStatus ( ) ; logNewline ( ) ; logInfo ( [ `To fix and continue your previous Graphite command:` , `(1) resolve the listed merge conflicts` , `(2) mark them as resolved with "git add"` , `(3) run "gt continue" to continue executing your previous Graphite command` , ] . map ( ( line ) => chalk . yellow ( line ) ) . join ( '\n' ) ) ; return ; case ValidationFailedError : logError ( `Validation: ${ err . message }` ) ; logInfo ( VALIDATION_HELPER_MESSAGE ) ; throw err ; case ConfigError : logError ( `Bad Config: ${ err . message }` ) ; throw err ; case ExitCancelledError : logWarn ( `Cancelled: ${ err . message }` ) ; return ; case SiblingBranchError : logError ( err . message ) ; throw err ; case MultiParentError : logError ( err . message ) ; throw err ; case KilledError : return ; default : logError ( err . message ) ; throw err ; } } } ) ; } catch ( err ) { const end = Date . now ( ) ; if ( execStateConfig . outputDebugLogs ( ) ) { logInfo ( err ) ; logInfo ( err . stack ) ; } postTelemetryInBackground ( { canonicalCommandName : canonicalName , commandName : parsedArgs . command , durationMiliSeconds : end - start , err : { errName : err . name , errMessage : err . message , errStack : err . stack || '' , } , } ) ; process . exit ( 1 ) ; } const end = Date . now ( ) ; postTelemetryInBackground ( { canonicalCommandName : canonicalName , commandName : parsedArgs . command , durationMiliSeconds : end - start , } ) ; }