file copyright author Vivek M. Chawla <@VivekMChawla>
 *  summary description version license import { flags } from '@salesforce/command' ; import { SfdxCommand } from '@salesforce/command' ; import { Messages } from '@salesforce/core' ; import { SfdxError } from '@salesforce/core' ; import * as path from 'path' ; import { SfdxFalconDebug } from '../sfdx-falcon-debug' ; import { SfdxFalconError } from '../sfdx-falcon-error' ; import { FalconProgressNotifications } from '../sfdx-falcon-notifications' ; import { SfdxFalconResult } from '../sfdx-falcon-result' ; import { SfdxFalconResultType } from '../sfdx-falcon-result' ; import { SfdxFalconJsonResponse } from '../sfdx-falcon-types' ; import { validateLocalPath } from '../sfdx-falcon-validators' ; enum SfdxFalconCommandType description export enum SfdxFalconCommandType { APPX_PACKAGE = 'APPX_PACKAGE' , APPX_DEMO = 'APPX_DEMO' , APPX_EXTENSION = 'APPX_EXTENSION' , UNKNOWN = 'UNKNOWN' } interface description export interface SfdxFalconCommandResultDetail { commandName : string ; commandType : SfdxFalconCommandType ; commandFlags : any ; commandArgs : any ; commandExitCode : number ; enabledDebuggers : string [ ] ; } Messages . importMessagesDirectory ( __dirname ) ; const baseMessages = Messages . loadMessages ( 'sfdx-falcon' , 'sfdxFalconCommand' ) ; const errorMessages = Messages . loadMessages ( 'sfdx-falcon' , 'sfdxFalconError' ) ; abstract class extends SfdxCommand summary description version public export abstract class SfdxFalconCommand extends SfdxCommand { protected static falconBaseflagsConfig = { falcondebug : flags . array ( { description : baseMessages . getMessage ( 'falcondebug_FlagDescription' ) , required : false , hidden : false , default : [ ] } ) , falcondebugerror : flags . boolean ( { description : baseMessages . getMessage ( 'falcondebugerror_FlagDescription' ) , required : false , hidden : false } ) , falcondebugsuccess : flags . boolean ( { description : baseMessages . getMessage ( 'falcondebugsuccess_FlagDescription' ) , required : false , hidden : false } ) , falcondebugdepth : flags . number ( { description : baseMessages . getMessage ( 'falcondebugdepth_FlagDescription' ) , required : false , hidden : false , default : 2 } ) } ; protected falconCommandName : string ; protected falconCommandResult : SfdxFalconResult ; protected falconCommandResultDetail : SfdxFalconCommandResultDetail ; protected falconJsonResponse : SfdxFalconJsonResponse ; protected outputDirectory : string ; protected projectDirectory : string ; protected targetDirectory : string ; protected recipeFile : string ; protected configFile : string ; protected extendedOptions : { any ; } ; protected gitRemoteUri : string ; protected gitCloneDirectory : string ; protected falconDebugFlag : string [ ] = new Array < string > ( ) ; protected falconDebugErrorFlag : boolean = false ; protected falconDebugSuccessFlag : boolean = false ; protected falconDebugDepthFlag : number = 2 ; protected abstract buildFinalError ( cmdError ) ; function param { string } commandName param { SfdxFalconCommandType } commandType returns { void } description version protected protected sfdxFalconCommandInit ( commandName = 'UNSPECIFIED_FALCON_COMMAND' , commandType ) { this . falconJsonResponse = { falconStatus : 0 , falconResult : 'RESPONSE_NOT_SPECIFIED' } ; this . falconCommandName = commandName ; this . outputDirectory = path . resolve ( this . flags . outputdir || '.' ) ; this . projectDirectory = path . resolve ( this . flags . projectdir || '.' ) ; this . targetDirectory = path . resolve ( this . flags . targetdir || '.' ) ; this . recipeFile = this . flags . recipefile || '' ; this . configFile = this . flags . configfile || '' ; this . extendedOptions = JSON . parse ( ( this . flags . extendedOptions || '{}' ) ) ; this . gitRemoteUri = this . args . GIT_REMOTE_URI || '' ; this . gitCloneDirectory = this . args . GIT_CLONE_DIR || '' ; this . falconDebugFlag = this . flags . falcondebug || [ ] ; this . falconDebugErrorFlag = this . flags . falcondebugerror || false ; this . falconDebugSuccessFlag = this . flags . falcondebugsuccess || false ; this . falconDebugDepthFlag = this . flags . falcondebugdepth || 2 ; const enabledDebuggers = new Array < string > ( ) ; for ( const debugNamespace of this . falconDebugFlag ) { enabledDebuggers . push ( `${ debugNamespace . trim ( ) }` ) ; } if ( this . falconDebugErrorFlag ) enabledDebuggers . push ( 'FALCON_ERROR' ) ; if ( this . falconDebugSuccessFlag ) enabledDebuggers . push ( 'FALCON_SUCCESS' ) ; this . falconCommandResult = new SfdxFalconResult ( commandName , SfdxFalconResultType . COMMAND , { startNow : true , bubbleError : false , bubbleFailure : false } ) ; this . falconCommandResultDetail = { commandName : commandName , commandType : commandType , commandFlags : this . flags , commandArgs : this . args , commandExitCode : null , enabledDebuggers : enabledDebuggers } ; this . falconCommandResult . setDetail ( this . falconCommandResultDetail ) ; SfdxFalconDebug . enableDebuggers ( enabledDebuggers , this . falconDebugDepthFlag ) ; if ( validateLocalPath ( this . outputDirectory ) === false ) { throw new Error ( errorMessages . getMessage ( 'errInvalidDirectory' , [ 'Output ' ] ) ) ; } if ( validateLocalPath ( this . projectDirectory ) === false ) { throw new Error ( errorMessages . getMessage ( 'errInvalidDirectory' , [ 'Project ' ] ) ) ; } if ( validateLocalPath ( this . targetDirectory ) === false ) { throw new Error ( errorMessages . getMessage ( 'errInvalidDirectory' , [ 'Target ' ] ) ) ; } } function param { any } rejectedPromise param { boolean } showErrorDebug param { boolean } promptUser returns { Promise < void > } description version protected protected async onError ( rejectedPromise , showErrorDebug = true , promptUser = true ) { const errorResult = SfdxFalconResult . wrapRejectedPromise ( rejectedPromise , SfdxFalconResultType . UNKNOWN , 'RejectedPromise' ) ; this . falconCommandResultDetail . commandExitCode = 1 ; this . falconCommandResult . addChild ( errorResult ) ; this . falconCommandResult . error ( errorResult . errObj ) ; await this . terminateWithError ( showErrorDebug , promptUser ) ; } function param { any } resolvedPromise returns { Promise < void > } description version protected protected async onSuccess ( resolvedPromise ) { const successResult = SfdxFalconResult . wrap ( resolvedPromise , SfdxFalconResultType . UNKNOWN , `onSuccess` ) ; this . falconCommandResultDetail . commandExitCode = 0 ; this . falconCommandResult . addChild ( successResult ) ; this . falconCommandResult . success ( ) ; if ( this . falconDebugSuccessFlag ) { this . falconCommandResult . displayResult ( ) ; } } method param { boolean } showErrorDebug param { boolean } promptUser description private private async terminateWithError ( showErrorDebug = true , promptUser = true ) { FalconProgressNotifications . killAll ( ) ; if ( ( this . falconCommandResult instanceof SfdxFalconResult ) === false ) { throw new Error ( 'ERROR_X01: An unexpected fatal error has occured' ) ; } if ( ( this . falconCommandResult . errObj instanceof SfdxFalconError ) === false ) { throw new Error ( 'ERROR_X02: An unexpected fatal error has occured' ) ; } await this . falconCommandResult . displayErrorDebugInfo ( showErrorDebug , promptUser ) ; throw this . buildFinalError ( this . falconCommandResult . errObj ) ; } }