file copyright author Vivek M. Chawla <@VivekMChawla>
 *  summary description version license import { SfdxFalconDebug } from '../../../../sfdx-falcon-debug' ; import { SfdxFalconError } from '../../../../sfdx-falcon-error' ; import { SfdxFalconResult } from '../../../../sfdx-falcon-result' ; import { SfdxFalconResultOptions } from '../../../../sfdx-falcon-result' ; import { SfdxFalconResultStatus } from '../../../../sfdx-falcon-result' ; import { SfdxFalconResultType } from '../../../../sfdx-falcon-result' ; import { waitASecond } from '../../../../sfdx-falcon-util/async' ; import { SfdxCommandDefinition } from '../../../executors/sfdx' ; import { AppxEngineActionContext } from '../../../engines/appx' ; import { ActionOptions } from '../../../types/' ; import { ExecutorMessages } from '../../../types/' ; import { SfdxFalconActionType } from '../../../types/' ; const dbgNs = 'ACTION:appx-engine-action:' ; interface description export interface CoreActionResultDetail { actionType : SfdxFalconActionType ; actionName : string ; description : string ; executorMessages : ExecutorMessages ; actionContext : AppxEngineActionContext ; actionOptions : ActionOptions ; } interface description export interface SfdxCliActionResultDetail extends CoreActionResultDetail { sfdxCommandDef : SfdxCommandDefinition ; } class description public abstract export abstract class AppxEngineAction { protected actionName : string ; protected actionType : SfdxFalconActionType ; protected description : string ; protected successDelay : number ; protected errorDelay : number ; protected progressDelay : number ; constructs description public public constructor ( ) { this . actionName = 'unspecified-action' ; this . actionType = SfdxFalconActionType . UNSPECIFIED ; this . description = 'Unspecified Action' ; this . successDelay = 2 ; this . errorDelay = 2 ; this . progressDelay = 1000 ; this . initializeAction ( ) ; } method param { AppxEngineActionContext } actionContext param { ActionOptions } actionOptions returns { Promise < SfdxFalconActionResponse > } description public async public async execute ( actionContext , actionOptions = { } ) { this . validateActionContext ( actionContext ) ; this . validateActionOptions ( actionOptions ) ; const falconActionResult = await this . executeAction ( actionContext , actionOptions ) . then ( successResult => this . onSuccess ( successResult ) ) . catch ( failureResult => this . onError ( failureResult ) ) ; await waitASecond ( this . successDelay ) ; return falconActionResult ; } protected async abstract executeAction ( actionContext , actionOptions ) ; protected abstract initializeAction ( ) ; protected abstract validateActionOptions ( actionOptions ) ; method param { AppxEngineActionContext } actionContext param { ActionOptions } actionOptions param { SfdxFalconResultOptions } sfdxFalconResultOptions returns { SfdxFalconResult } description protected protected createActionResult ( actionContext , actionOptions = { } , sfdxFalconResultOptions = { } ) { const falconActionResult = new SfdxFalconResult ( `${ this . actionName }:executeAction` , SfdxFalconResultType . ACTION , sfdxFalconResultOptions ) ; falconActionResult . setDetail ( { actionType : this . actionType , actionName : this . actionName , description : this . description , actionContext : actionContext , actionOptions : actionOptions } ) ; return falconActionResult ; } method param { unknown } rejectedPromise returns { void } description version private private onError ( rejectedPromise ) { SfdxFalconDebug . obj ( `${ dbgNs }onError:rejectedPromise:` , { rejectedPromise : rejectedPromise } , `rejectedPromise: ` ) ; const thisActionResult = SfdxFalconResult . wrap ( rejectedPromise , SfdxFalconResultType . UNKNOWN , 'ActionResult (REJECTED)' ) ; if ( thisActionResult . status === SfdxFalconResultStatus . WAITING ) { thisActionResult . error ( thisActionResult . errObj ) ; } throw thisActionResult ; } method param { SfdxFalconExecutorResponse } execSuccessResponse returns { SfdxFalconResult } description private private onSuccess ( falconActionResult ) { if ( ( falconActionResult instanceof SfdxFalconResult ) !== true ) { const typeError = new SfdxFalconError ( `AppxEngineAction.onSuccess() expects an argument that's an instance  ` + `of 'SfdxFalconResult', not '${ falconActionResult . constructor . name }'` , `TypeError` , `${ dbgNs }onSuccess` ) ; typeError . setData ( { rawResult : falconActionResult } ) ; throw typeError ; } SfdxFalconDebug . obj ( `${ dbgNs }onSuccess:falconActionResult:` , falconActionResult , `falconActionResult: ` ) ; if ( falconActionResult . status === SfdxFalconResultStatus . WAITING ) { falconActionResult . success ( ) ; } return falconActionResult ; } method param { AppxEngineActionContext } actionContext description private private validateActionContext ( actionContext ) { if ( typeof actionContext === 'undefined' ) { throw new SfdxFalconError ( `Missing actionContxt object when attempting to ` + `execute an AppxEngineAction` , `InvalidActionContext` , `${ dbgNs }validateActionContext` ) ; } if ( actionContext . targetOrg . isScratchOrg === true && ( ! actionContext . devHubAlias ) ) { const actionContextError = new SfdxFalconError ( `Target Org is identified as a scratch org, but ` + `a DevHub Alias was not provided` , `InvalidActionContext` , `${ dbgNs }validateActionContext` ) ; actionContextError . setData ( actionContext ) ; throw actionContextError ; } } }