file copyright author Vivek M. Chawla <@VivekMChawla>
 *  summary description version license import { isEmpty } from 'lodash' ; import * as path from 'path' ; import { Observable } from 'rxjs' ; import * as sfdxHelper from '../sfdx-falcon-util/sfdx' ; import * as yoHelper from '../sfdx-falcon-util/yeoman' ; import * as gitHelper from './git' ; import * as zipHelper from './zip' ; 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 { waitASecond } from '../sfdx-falcon-util/async' ; import { ErrorOrResult } from '../sfdx-falcon-types' ; import { ListrContextFinalizeGit } from '../sfdx-falcon-types' ; import { ListrContextPkgRetExCon } from '../sfdx-falcon-types' ; import { ListrExecutionOptions } from '../sfdx-falcon-types' ; import { ListrObject } from '../sfdx-falcon-types' ; import { ListrSkipCommand } from '../sfdx-falcon-types' ; import { ListrTask } from '../sfdx-falcon-types' ; import { RawSfdxOrgInfo } from '../sfdx-falcon-types' ; import { SfdxOrgInfoMap } from '../sfdx-falcon-types' ; import { ShellExecResult } from '../sfdx-falcon-types' ; import { Subscriber } from '../sfdx-falcon-types' ; const falconUpdateRenderer = require ( 'falcon-listr-update-renderer' ) ; const listr = require ( 'listr' ) ; const dbgNs = 'UTILITY:listr-tasks:' ; function param { string } targetDir param { string } gitRemoteUri returns { ListrTask } description public export function addGitRemote ( targetDir , gitRemoteUri ) { validateSharedData . call ( this ) ; return { title : `Adding the Git Remote...` , enabled : ( ) => ( typeof targetDir === 'string' && targetDir !== '' && typeof gitRemoteUri === 'string' && gitRemoteUri !== '' ) , skip : ( listrContext ) => { if ( listrContext . gitInstalled !== true ) { return true ; } if ( listrContext . gitRemoteIsValid !== true ) { return 'Git Remote is Invalid' ; } } , task : ( listrContext , thisTask ) => { return new Observable ( observer => { const otr = initObservableTaskResult ( `${ dbgNs }addGitRemote` , listrContext , thisTask , observer , this . sharedData , this . generatorResult , `Adding the Git Remote ${ gitRemoteUri } to the local repository` ) ; const asyncTask = async ( ) => { const shellString = gitHelper . gitRemoteAddOrigin ( targetDir , gitRemoteUri ) ; SfdxFalconDebug . obj ( `${ dbgNs }addGitRemote:shellString:` , shellString , `shellString: ` ) ; return shellString ; } ; asyncTask ( ) . then ( async ( result ) => { await waitASecond ( 3 ) ; thisTask . title += 'Done!' ; listrContext . gitRemoteAdded = true ; finalizeObservableTaskResult ( otr ) ; } ) . catch ( async ( error ) => { await waitASecond ( 3 ) ; thisTask . title += 'Failed' ; listrContext . gitRemoteAdded = false ; finalizeObservableTaskResult ( otr , error ) ; } ) ; } ) ; } } as ListrTask ; } function returns { ListrTask } description public export function buildDevHubAliasList ( ) { validateSharedData . call ( this ) ; return { title : 'Building DevHub Alias List...' , enabled : ( ) => Array . isArray ( this . sharedData . devHubAliasChoices ) , task : ( listrContext , thisTask ) => { SfdxFalconDebug . obj ( `${ dbgNs }buildDevHubAliasList:listrContext.devHubOrgInfos:` , listrContext . devHubOrgInfos , `listrContext.devHubOrgInfos: ` ) ; this . sharedData . devHubAliasChoices = yoHelper . buildOrgAliasChoices ( listrContext . devHubOrgInfos ) ; this . sharedData . devHubAliasChoices . push ( new yoHelper . YeomanSeparator ( ) ) ; this . sharedData . devHubAliasChoices . push ( { name : 'My DevHub Is Not Listed Above' , value : 'NOT_SPECIFIED' , short : 'Not Specified' } ) ; thisTask . title += 'Done!' ; return ; } } as ListrTask ; } function returns { ListrTask } description public export function buildEnvHubAliasList ( ) { validateSharedData . call ( this ) ; return { title : 'Building EnvHub Alias List...' , enabled : ( ) => Array . isArray ( this . sharedData . envHubAliasChoices ) , task : ( listrContext , thisTask ) => { SfdxFalconDebug . obj ( `${ dbgNs }buildEnvHubAliasList:listrContext.envHubOrgInfos:` , listrContext . envHubOrgInfos , `listrContext.envHubOrgInfos: ` ) ; this . sharedData . envHubAliasChoices = yoHelper . buildOrgAliasChoices ( listrContext . envHubOrgInfos ) ; this . sharedData . envHubAliasChoices . push ( new yoHelper . YeomanSeparator ( ) ) ; this . sharedData . envHubAliasChoices . push ( { name : 'My Environment Hub Is Not Listed' , value : 'NOT_SPECIFIED' , short : 'Not Specified' } ) ; thisTask . title += 'Done!' ; return ; } } as ListrTask ; } function returns { ListrTask } description public export function buildPkgOrgAliasList ( ) { validateSharedData . call ( this ) ; return { title : 'Building PkgOrg Alias List...' , enabled : ( ) => Array . isArray ( this . sharedData . pkgOrgAliasChoices ) , task : ( listrContext , thisTask ) => { SfdxFalconDebug . obj ( `${ dbgNs }buildPkgOrgAliasList:listrContext.pkgOrgInfos:` , listrContext . pkgOrgInfos , `listrContext.pkgOrgInfos: ` ) ; this . sharedData . pkgOrgAliasChoices = yoHelper . buildOrgAliasChoices ( listrContext . pkgOrgInfos ) ; this . sharedData . pkgOrgAliasChoices . push ( new yoHelper . YeomanSeparator ( ) ) ; this . sharedData . pkgOrgAliasChoices . push ( { name : 'My Packaging Org Is Not Listed' , value : 'NOT_SPECIFIED' , short : 'Not Specified' } ) ; const managedPkgOrgInfos = new Array < sfdxHelper . SfdxOrgInfo > ( ) ; for ( const orgInfo of listrContext . pkgOrgInfos as sfdxHelper . SfdxOrgInfo [ ] ) { if ( orgInfo . nsPrefix ) { managedPkgOrgInfos . push ( orgInfo ) ; } } this . sharedData . managedPkgOrgAliasChoices = yoHelper . buildOrgAliasChoices ( managedPkgOrgInfos ) ; this . sharedData . managedPkgOrgAliasChoices . push ( new yoHelper . YeomanSeparator ( ) ) ; this . sharedData . managedPkgOrgAliasChoices . push ( { name : 'My Packaging Org Is Not Listed' , value : 'NOT_SPECIFIED' , short : 'Not Specified' } ) ; const unmanagedPkgOrgInfos = new Array < sfdxHelper . SfdxOrgInfo > ( ) ; for ( const orgInfo of listrContext . pkgOrgInfos as sfdxHelper . SfdxOrgInfo [ ] ) { if ( ! orgInfo . nsPrefix ) { unmanagedPkgOrgInfos . push ( orgInfo ) ; } } this . sharedData . unmanagedPkgOrgAliasChoices = yoHelper . buildOrgAliasChoices ( unmanagedPkgOrgInfos ) ; this . sharedData . unmanagedPkgOrgAliasChoices . push ( new yoHelper . YeomanSeparator ( ) ) ; this . sharedData . unmanagedPkgOrgAliasChoices . push ( { name : 'My Packaging Org Is Not Listed' , value : 'NOT_SPECIFIED' , short : 'Not Specified' } ) ; thisTask . title += 'Done!' ; return ; } } as ListrTask ; } function param { string } gitRemoteUri param { string } targetDirectory param { string } gitCloneDirectory returns { ListrObject } description public export function cloneGitRemote ( gitRemoteUri , targetDirectory , gitCloneDirectory = '' ) { validateSharedData . call ( this ) ; validateGitCloneArguments . apply ( null , arguments ) ; return new listr ( [ { title : `Cloning ${ gitRemoteUri }...` , enabled : ( ) => ( gitRemoteUri && targetDirectory ) , task : ( listrContext , thisTask ) => { return new Observable ( observer => { const otr = initObservableTaskResult ( `${ dbgNs }cloneGitRemote` , listrContext , thisTask , observer , this . sharedData , this . generatorResult , `Cloning repository to ${ path . join ( targetDirectory , gitCloneDirectory ) }` ) ; const asyncTask = async ( ) => { await waitASecond ( 5 ) ; SfdxFalconDebug . str ( `${ dbgNs }cloneGitRemote:gitRemoteUri:` , gitRemoteUri , `gitRemoteUri: ` ) ; SfdxFalconDebug . str ( `${ dbgNs }cloneGitRemote:targetDirectory:` , targetDirectory , `targetDirectory: ` ) ; SfdxFalconDebug . str ( `${ dbgNs }cloneGitRemote:gitCloneDirectory:` , gitCloneDirectory , `gitCloneDirectory: ` ) ; return gitHelper . gitClone ( gitRemoteUri , targetDirectory , gitCloneDirectory ) ; } ; asyncTask ( ) . then ( async ( shellExecResult ) => { await waitASecond ( 3 ) ; thisTask . title += 'Done!' ; listrContext . gitRemoteCloned = true ; finalizeObservableTaskResult ( otr ) ; } ) . catch ( async ( shellExecError ) => { await waitASecond ( 3 ) ; thisTask . title += 'Failed' ; listrContext . gitRemoteCloned = false ; finalizeObservableTaskResult ( otr , new SfdxFalconError ( `Could not clone repository: ${ shellExecError . message }` , `GitCloneFailure` , `${ dbgNs }cloneGitRemote` , SfdxFalconError . wrap ( shellExecError ) ) ) ; } ) ; } ) ; } } ] , { concurrent : false , collapse : false , renderer : falconUpdateRenderer } ) ; } function param { string } targetDir param { string } commitMessage returns { ListrTask } description public export function commitProjectFiles ( targetDir , commitMessage ) { validateSharedData . call ( this ) ; return { title : `Committing Files...` , enabled : ( ) => ( typeof targetDir === 'string' && targetDir !== '' ) , skip : ( listrContext ) => { if ( listrContext . gitInstalled !== true ) { return true ; } } , task : ( listrContext , thisTask ) => { return new Observable ( observer => { const otr = initObservableTaskResult ( `${ dbgNs }commitProjectFiles` , listrContext , thisTask , observer , this . sharedData , this . generatorResult , `Committing files with this message: '${ commitMessage }'` ) ; const asyncTask = async ( ) => { const shellString = gitHelper . gitCommit ( targetDir , commitMessage ) ; SfdxFalconDebug . obj ( `${ dbgNs }commitProjectFiles:shellString:` , shellString , `shellString: ` ) ; return shellString ; } ; asyncTask ( ) . then ( async ( result ) => { await waitASecond ( 3 ) ; thisTask . title += 'Done!' ; listrContext . projectFilesCommitted = true ; finalizeObservableTaskResult ( otr ) ; } ) . catch ( async ( error ) => { await waitASecond ( 3 ) ; listrContext . projectFilesCommitted = false ; ( thisTask . skip as ListrSkipCommand ) ( 'Nothing to Commit' ) ; finalizeObservableTaskResult ( otr ) ; } ) ; } ) ; } } as ListrTask ; } function param { string } mdapiSourceRootDir param { string } sfdxSourceOutputDir returns { ListrTask } description private function convertMetadataSource ( mdapiSourceRootDir , sfdxSourceOutputDir ) { validateSharedData . call ( this ) ; return { title : 'Converting MDAPI Source...' , task : ( listrContext , thisTask ) => { return new Observable ( observer => { const otr = initObservableTaskResult ( `${ dbgNs }convertMetadataSource` , listrContext , thisTask , observer , this . sharedData , this . generatorResult , `Converting MDAPI source from ${ mdapiSourceRootDir }` ) ; sfdxHelper . mdapiConvert ( mdapiSourceRootDir , sfdxSourceOutputDir ) . then ( ( successResult ) => { SfdxFalconDebug . obj ( `${ dbgNs }convertMetadataSource:successResult:` , successResult , `successResult: ` ) ; thisTask . title += 'Done!' ; finalizeObservableTaskResult ( otr ) ; } ) . catch ( ( failureResult ) => { SfdxFalconDebug . obj ( `${ dbgNs }convertMetadataSource:failureResult:` , failureResult , `failureResult: ` ) ; thisTask . title += 'Failed' ; finalizeObservableTaskResult ( otr , failureResult ) ; } ) ; } ) ; } } as ListrTask ; } function param { string } zipFile param { string } zipExtractTarget returns { ListrTask } description private function extractMdapiSource ( zipFile , zipExtractTarget ) { validateSharedData . call ( this ) ; return { title : 'Extracting MDAPI Source...' , task : ( listrContext , thisTask ) => { return new Observable ( observer => { const otr = initObservableTaskResult ( `${ dbgNs }extractMdapiSource` , listrContext , thisTask , observer , this . sharedData , this . generatorResult , `Extracting source into ${ zipExtractTarget }` ) ; zipHelper . extract ( zipFile , zipExtractTarget ) . then ( ( ) => { listrContext . sourceExtracted = true ; thisTask . title += 'Done!' ; finalizeObservableTaskResult ( otr ) ; } ) . catch ( extractionError => { listrContext . sourceExtracted = false ; thisTask . title += 'Failed' ; finalizeObservableTaskResult ( otr , new SfdxFalconError ( `MDAPI source from ${ zipFile } could not be extracted to ${ zipExtractTarget }` , `SourceExtractionError` , `${ dbgNs }extractZipFile` , extractionError ) ) ; } ) ; } ) ; } } as ListrTask ; } function param { string } aliasOrUsername param { string } packageName param { string } projectDir param { string } packageDir returns { ListrObject } description public export function fetchAndConvertManagedPackage ( aliasOrUsername , packageName , projectDir , packageDir ) { validatePkgConversionArguments . apply ( null , arguments ) ; const retrieveTargetDir = path . join ( projectDir , 'temp' ) ; const zipFile = path . join ( projectDir , 'temp' , 'unpackaged.zip' ) ; const zipExtractTarget = path . join ( projectDir , 'mdapi-source' , 'original' ) ; const mdapiSourceRootDir = path . join ( projectDir , 'mdapi-source' , 'original' ) ; const sfdxSourceOutputDir = path . join ( projectDir , 'sfdx-source' , packageDir ) ; return new listr ( [ packagedMetadataFetch . call ( this , aliasOrUsername , [ packageName ] , retrieveTargetDir ) , extractMdapiSource . call ( this , zipFile , zipExtractTarget ) , convertMetadataSource . call ( this , mdapiSourceRootDir , sfdxSourceOutputDir ) ] , { concurrent : false , collapse : false , renderer : falconUpdateRenderer } ) ; } function param { string } targetDirectory param { string } gitRemoteUri returns { ListrObject } description public export function finalizeGit ( targetDir , gitRemoteUri = '' , gitCommitMsg = 'Initial Commit' ) { SfdxFalconDebug . obj ( `${ dbgNs }finalizeGit:arguments:` , arguments , `arguments: ` ) ; validateSharedData . call ( this ) ; if ( typeof targetDir !== 'string' || targetDir === '' ) { throw new SfdxFalconError ( `Expected targetDir to be a non-empty string but got type '${ typeof targetDir }' instead.` , `TypeError` , `${ dbgNs }finalizeGit` ) ; } return new listr ( [ gitRuntimeCheck . call ( this ) , initializeGit . call ( this , targetDir ) , stageProjectFiles . call ( this , targetDir ) , commitProjectFiles . call ( this , targetDir , gitCommitMsg ) , reValidateGitRemote . call ( this , gitRemoteUri ) , addGitRemote . call ( this , targetDir , gitRemoteUri ) ] , { concurrent : false , collapse : false , exitOnError : false , renderer : falconUpdateRenderer } ) as ListrObject ; } function param { SfdxFalconResult } otr param { ErrorOrResult } errorOrResult returns { void } description private function finalizeObservableTaskResult ( otr , errorOrResult ? ) { const observerThrow = ( error ) : void => { try { otr . detail [ 'observer' ] . error ( SfdxFalconError . wrap ( error ) ) ; } catch ( noObserverError ) { SfdxFalconDebug . debugObject ( 'WARNING! OBSERVER MISSING:' , error ) ; throw SfdxFalconError . wrap ( error ) ; } } ; if ( typeof otr === 'undefined' ) { return observerThrow ( new SfdxFalconError ( `Expected otr to be an SfdxFalconResult but got 'undefined' instead.` , `TypeError` , `${ dbgNs }finalizeObservableTaskResult` ) ) ; } if ( ( otr instanceof SfdxFalconResult ) !== true ) { return observerThrow ( new SfdxFalconError ( `Expected otr to be an SfdxFalconResult but got '${ ( otr . constructor ) ? otr . constructor . name : 'unknown' }' instead.` , `TypeError` , `${ dbgNs }finalizeObservableTaskResult` ) ) ; } if ( typeof otr . detail !== 'object' ) { return observerThrow ( new SfdxFalconError ( `Expected otr.detail to be an object but got '${ typeof otr . detail }' instead.` , `TypeError` , `${ dbgNs }finalizeObservableTaskResult` ) ) ; } if ( typeof errorOrResult !== 'undefined' && ( ( errorOrResult instanceof Error ) !== true ) && ( ( errorOrResult instanceof SfdxFalconResult ) !== true ) ) { return observerThrow ( new SfdxFalconError ( `Expected errorOrResult to be an instance of Error or SfdxFalconResult if provided but got '${ ( errorOrResult . constructor ) ? errorOrResult . constructor . name : 'unknown' }' instead.` , `TypeError` , `${ dbgNs }finalizeObservableTaskResult` ) ) ; } const otrObserver = otr . detail [ 'observer' ] || { } as Subscriber ; const parentResult = otr . detail [ 'parentResult' ] || { } as SfdxFalconResult ; const progressNotification = otr . detail [ 'progressNotification' ] as NodeJS . Timeout ; FalconProgressNotifications . finish ( progressNotification ) ; if ( typeof errorOrResult === 'undefined' ) { otr . success ( ) ; } else { if ( errorOrResult instanceof Error ) { otr . error ( errorOrResult ) ; } if ( errorOrResult instanceof SfdxFalconResult ) { try { otr . addChild ( errorOrResult ) ; } catch { } } } if ( parentResult instanceof SfdxFalconResult ) { try { parentResult . addChild ( otr ) ; } catch ( bubbledError ) { return observerThrow ( SfdxFalconError . wrap ( bubbledError . errObj ) ) ; } } if ( typeof errorOrResult === 'undefined' ) { if ( typeof otrObserver . complete === 'function' ) { return otrObserver . complete ( ) ; } } else { if ( errorOrResult instanceof Error ) { return observerThrow ( SfdxFalconError . wrap ( errorOrResult ) ) ; } if ( errorOrResult instanceof SfdxFalconResult ) { if ( isEmpty ( errorOrResult . errObj ) !== true ) { return observerThrow ( SfdxFalconError . wrap ( errorOrResult . errObj ) ) ; } else { return observerThrow ( new SfdxFalconError ( `finalizeObservableTaskResult() received an Error Result that did not contain an Error Object.` , `MissingErrObj` , `${ dbgNs }finalizeObservableTaskResult` , SfdxFalconError . wrap ( errorOrResult ) ) ) ; } } return observerThrow ( new SfdxFalconError ( `finalizeObservableTaskResult() received an invalid value for its errorOrResult parameter.` , `InvalidErrorOrResult` , `${ dbgNs }finalizeObservableTaskResult` , SfdxFalconError . wrap ( errorOrResult ) ) ) ; } } function returns { ListrObject } description public export function gitInitTasks ( ) { validateSharedData . call ( this ) ; const cliCommandName = this . sharedData . cliCommandName ; const gitRemoteUri = this . sharedData . gitRemoteUri ; if ( typeof cliCommandName !== 'string' || cliCommandName === '' ) { throw new SfdxFalconError ( `Expected cliCommandName to be a non-empty string but got type '${ typeof cliCommandName }' instead.` , `TypeError` , `${ dbgNs }gitInitTasks` ) ; } return new listr ( [ { title : `Inspecting Environment` , task : listrContext => { return new listr ( [ gitRuntimeCheck . call ( this ) , validateGitRemote ( gitRemoteUri ) ] , { concurrent : false , renderer : falconUpdateRenderer } ) ; } } ] , { concurrent : false , collapse : false , renderer : falconUpdateRenderer } ) ; } function param { string } dbgNs returns { ListrTask } description public export function gitRuntimeCheck ( ) { return { title : 'Looking for Git...' , task : ( listrContext , thisTask ) => { if ( gitHelper . isGitInstalled ( ) === true ) { thisTask . title += 'Found!' ; listrContext . gitInstalled = true ; } else { listrContext . gitInstalled = false ; thisTask . title += 'Not Found!' ; throw new SfdxFalconError ( 'Git must be installed in your local environment.' , 'GitNotFound' , `${ dbgNs }gitRuntimeCheck` ) ; } } } as ListrTask ; } function returns { ListrTask } description public export function identifyDevHubs ( ) { validateSharedData . call ( this ) ; return { title : 'Identifying DevHub Orgs...' , enabled : ( ) => Array . isArray ( this . sharedData . devHubAliasChoices ) , task : ( listrContext , thisTask ) => { SfdxFalconDebug . obj ( `${ dbgNs }identifyDevHubs:` , listrContext . rawSfdxOrgList , `listrContext.rawSfdxOrgList: ` ) ; const devHubOrgInfos = sfdxHelper . identifyDevHubOrgs ( this . sharedData . sfdxOrgInfoMap as SfdxOrgInfoMap ) ; SfdxFalconDebug . obj ( `${ dbgNs }identifyDevHubs:` , devHubOrgInfos , `devHubOrgInfos: ` ) ; if ( devHubOrgInfos . length < 1 ) { thisTask . title += 'No Dev Hubs Found' ; throw new SfdxFalconError ( `No Dev Hubs found. You must have at least one active Dev Hub to continue. ` + `Please run force:auth:web:login to connect to your Dev Hub.` , `NoDevHubs` , `${ dbgNs }identifyDevHubs` ) ; } listrContext . devHubOrgInfos = devHubOrgInfos ; thisTask . title += 'Done!' ; } } as ListrTask ; } function returns { ListrTask } description public export function identifyEnvHubs ( ) { validateSharedData . call ( this ) ; return { title : 'Identifying EnvHub Orgs...' , enabled : ( ) => Array . isArray ( this . sharedData . envHubAliasChoices ) , task : ( listrContext , thisTask ) => { SfdxFalconDebug . obj ( `${ dbgNs }identifyEnvHubs:listrContext.rawSfdxOrgList:` , listrContext . rawSfdxOrgList , `listrContext.rawSfdxOrgList: ` ) ; return sfdxHelper . identifyEnvHubOrgs ( this . sharedData . sfdxOrgInfoMap as SfdxOrgInfoMap ) . then ( envHubOrgInfos => { SfdxFalconDebug . obj ( `${ dbgNs }identifyEnvHubs:envHubOrgInfos:` , envHubOrgInfos , `envHubOrgInfos: ` ) ; listrContext . envHubOrgInfos = envHubOrgInfos ; if ( envHubOrgInfos . length < 1 ) { thisTask . title += 'No Environment Hubs Found' ; } else { thisTask . title += 'Done!' ; } } ) . catch ( error => { SfdxFalconDebug . obj ( `${ dbgNs }identifyEnvHubs:error:` , error , `error: ` ) ; thisTask . title += 'Unexpected error while identifying Environment Hub Orgs' ; throw error ; } ) ; } } as ListrTask ; } function returns { ListrTask } description public export function identifyPkgOrgs ( ) { validateSharedData . call ( this ) ; return { title : 'Identifying Packaging Orgs...' , enabled : ( ) => Array . isArray ( this . sharedData . pkgOrgAliasChoices ) , task : ( listrContext , thisTask ) => { SfdxFalconDebug . obj ( `${ dbgNs }identifyPkgOrgs:listrContext.rawSfdxOrgList:` , listrContext . rawSfdxOrgList , `listrContext.rawSfdxOrgList: (BEFORE ASYNC CALL)` ) ; return sfdxHelper . identifyPkgOrgs ( this . sharedData . sfdxOrgInfoMap as SfdxOrgInfoMap ) . then ( pkgOrgInfos => { SfdxFalconDebug . obj ( `${ dbgNs }identifyPkgOrgs:pkgOrgInfos:` , pkgOrgInfos , `pkgOrgInfos: ` ) ; listrContext . pkgOrgInfos = pkgOrgInfos ; if ( pkgOrgInfos . length < 1 ) { thisTask . title += 'No Packaging Orgs Found' ; } else { thisTask . title += 'Done!' ; } } ) . catch ( error => { SfdxFalconDebug . obj ( `${ dbgNs }identifyPkgOrgs:error:` , error , `error: ` ) ; thisTask . title += 'Unexpected error while identifying Packaging Orgs' ; throw error ; } ) ; } } as ListrTask ; } function param { string } targetDir returns { ListrTask } description public export function initializeGit ( targetDir ) { validateSharedData . call ( this ) ; return { title : `Initializing Git in Target Directory...` , enabled : ( ) => ( typeof targetDir === 'string' && targetDir !== '' ) , skip : ( listrContext ) => { if ( listrContext . gitInstalled !== true ) { return true ; } } , task : ( listrContext , thisTask ) => { return new Observable ( observer => { const otr = initObservableTaskResult ( `${ dbgNs }:initializeGit` , listrContext , thisTask , observer , this . sharedData , this . generatorResult , `Running git init in ${ targetDir }` ) ; const asyncTask = async ( ) => { const shellString = gitHelper . gitInit ( targetDir ) ; SfdxFalconDebug . obj ( `${ dbgNs }initializeGit:shellString:` , shellString , `shellString: ` ) ; return shellString ; } ; asyncTask ( ) . then ( async ( result ) => { await waitASecond ( 3 ) ; thisTask . title += 'Done!' ; listrContext . gitInitialized = true ; finalizeObservableTaskResult ( otr ) ; } ) . catch ( async ( error ) => { await waitASecond ( 3 ) ; thisTask . title += 'Failed' ; listrContext . gitInitialized = false ; finalizeObservableTaskResult ( otr , error ) ; } ) ; } ) ; } } as ListrTask ; } function param { string } extDbgNs param { object } listrContext param { object } listrTask param { object } observer param { object } sharedData param { string } message param { SfdxFalconResult } parentResult returns { ListrExecutionOptions } description private function initObservableTaskResult ( extDbgNs , listrContext , listrTask , observer , sharedData , parentResult , message ) { const listrExecOpts = { listrContext : listrContext , listrTask : listrTask , observer : observer , sharedData : sharedData } ; SfdxFalconDebug . obj ( `${ extDbgNs }` , listrExecOpts , `listrExecOpts: ` ) ; const observableTaskResult = new SfdxFalconResult ( extDbgNs , SfdxFalconResultType . LISTR , { startNow : true , bubbleError : false , bubbleFailure : false } ) ; if ( typeof observer === 'object' && typeof observer . next === 'function' && typeof message === 'string' ) { observer . next ( `[0s] ${ message }` ) ; } const progressNotification = FalconProgressNotifications . start ( message , 1000 , observableTaskResult , observer ) ; const observableTaskResultDetail = { progressNotification : progressNotification , parentResult : parentResult , listrExecOpts : listrExecOpts , listrContext : listrContext , listrTask : listrTask , observer : observer , sharedData : sharedData , message : message } ; observableTaskResult . setDetail ( observableTaskResultDetail ) ; return observableTaskResult ; } function param { string } aliasOrUsername param { string [ ] } packageNames param { string } retrieveTargetDir returns { ListrTask } description public export function packagedMetadataFetch ( aliasOrUsername , packageNames , retrieveTargetDir ) { validatePkgConversionArguments . apply ( null , arguments ) ; validateSharedData . call ( this ) ; return { title : 'Fetching Metadata Packages...' , task : ( listrContext , thisTask ) => { return new Observable ( observer => { const otr = initObservableTaskResult ( `${ dbgNs }packagedMetadataFetch` , listrContext , thisTask , observer , this . sharedData , this . generatorResult , `Retrieving '${ packageNames }' from ${ aliasOrUsername } (this might take a few minutes)` ) ; sfdxHelper . fetchMetadataPackages ( aliasOrUsername , packageNames , retrieveTargetDir ) . then ( ( successResult ) => { SfdxFalconDebug . obj ( `${ dbgNs }packagedMetadataFetch:successResult:` , successResult , `successResult: ` ) ; this . sharedData . pkgMetadataFetchResult = successResult ; thisTask . title += 'Done!' ; finalizeObservableTaskResult ( otr ) ; } ) . catch ( ( failureResult ) => { SfdxFalconDebug . obj ( `${ dbgNs }packagedMetadataFetch:failureResult:` , failureResult , `failureResult: ` ) ; thisTask . title += 'Failed' ; finalizeObservableTaskResult ( otr , failureResult ) ; } ) ; } ) ; } } ; } function param { string } gitRemoteUri returns { ListrTask } description public export function reValidateGitRemote ( gitRemoteUri ) { validateSharedData . call ( this ) ; return { title : `Validating Access to the Git Remote...` , enabled : ( ) => ( typeof gitRemoteUri === 'string' && gitRemoteUri !== '' ) , skip : ( listrContext ) => { if ( listrContext . gitInstalled !== true ) { return true ; } } , task : ( listrContext , thisTask ) => { return new Observable ( observer => { const otr = initObservableTaskResult ( `${ dbgNs }reValidateGitRemote` , listrContext , thisTask , observer , this . sharedData , this . generatorResult , `Attempting to reach ${ gitRemoteUri }` ) ; gitHelper . checkGitRemoteStatus ( gitRemoteUri , 3 ) . then ( ( successResult ) => { SfdxFalconDebug . obj ( `${ dbgNs }reValidateGitRemote:successResult:` , successResult , `successResult: ` ) ; listrContext . gitRemoteIsValid = true ; thisTask . title += 'Done!' ; finalizeObservableTaskResult ( otr ) ; } ) . catch ( ( errorResult ) => { SfdxFalconDebug . obj ( `${ dbgNs }reValidateGitRemote:errorResult:` , errorResult , `errorResult: ` ) ; if ( errorResult . code === 2 ) { listrContext . gitRemoteIsValid = true ; thisTask . title += 'Done!' ; finalizeObservableTaskResult ( otr ) ; return ; } listrContext . gitRemoteIsValid = false ; thisTask . title += errorResult . message ; finalizeObservableTaskResult ( otr , new SfdxFalconError ( `Git Remote is invalid. ${ errorResult . message }. ` , `GitRemoteError` , `${ dbgNs }reValidateGitRemote` ) ) ; } ) ; } ) ; } } as ListrTask ; } function returns { ListrTask } description public export function scanConnectedOrgs ( ) { validateSharedData . call ( this ) ; return { title : 'Scanning Connected Orgs...' , task : ( listrContext , thisTask ) => { return sfdxHelper . scanConnectedOrgs ( ) . then ( successResult => { SfdxFalconDebug . obj ( `${ dbgNs }scanConnectedOrgs:successResult:` , successResult , `successResult: ` ) ; let rawSfdxOrgList ; if ( successResult . detail && typeof successResult . detail === 'object' ) { if ( ( successResult . detail as sfdxHelper . SfdxUtilityResultDetail ) . stdOutParsed ) { rawSfdxOrgList = ( successResult . detail as sfdxHelper . SfdxUtilityResultDetail ) . stdOutParsed [ 'result' ] [ 'nonScratchOrgs' ] ; } } if ( Array . isArray ( rawSfdxOrgList ) === false || rawSfdxOrgList . length < 1 ) { throw new SfdxFalconError ( `No orgs have been authenticated to the Salesforce CLI. ` + `Please run force:auth:web:login to connect to an org.` , `NoConnectedOrgs` , `${ dbgNs }scanConnectedOrgs` ) ; } this . sharedData . rawSfdxOrgList = rawSfdxOrgList ; this . sharedData . sfdxOrgInfoMap = sfdxHelper . buildSfdxOrgInfoMap ( rawSfdxOrgList as RawSfdxOrgInfo [ ] ) ; thisTask . title += 'Done!' ; } ) . catch ( failureResult => { SfdxFalconDebug . obj ( `${ dbgNs }scanConnectedOrgs:failureResult:` , failureResult , `failureResult: ` ) ; thisTask . title += 'No Connections Found' ; throw failureResult ; } ) ; } } as ListrTask ; } function returns { ListrObject } description public export function sfdxInitTasks ( ) { return new listr ( [ { title : 'Inspecting Local SFDX Configuration' , task : listrContext => { return new listr ( [ scanConnectedOrgs . call ( this ) , identifyDevHubs . call ( this ) , identifyEnvHubs . call ( this ) , identifyPkgOrgs . call ( this ) , buildDevHubAliasList . call ( this ) , buildEnvHubAliasList . call ( this ) , buildPkgOrgAliasList . call ( this ) ] , { concurrent : false , collapse : false , renderer : falconUpdateRenderer } ) ; } } ] , { concurrent : false , collapse : false , renderer : falconUpdateRenderer } ) ; } function param { string } targetDir returns { ListrTask } description public export function stageProjectFiles ( targetDir ) { validateSharedData . call ( this ) ; return { title : `Staging Files...` , enabled : ( ) => ( typeof targetDir === 'string' && targetDir !== '' ) , skip : ( listrContext ) => { if ( listrContext . gitInstalled !== true ) { return true ; } } , task : ( listrContext , thisTask ) => { return new Observable ( observer => { const otr = initObservableTaskResult ( `${ dbgNs }stageProjectFiles` , listrContext , thisTask , observer , this . sharedData , this . generatorResult , `Staging all new and modified files (git -A) in ${ targetDir }` ) ; const asyncTask = async ( ) => { const shellString = gitHelper . gitAdd ( targetDir ) ; SfdxFalconDebug . obj ( `${ dbgNs }stageProjectFiles:shellString:` , shellString , `shellString: ` ) ; return shellString ; } ; asyncTask ( ) . then ( async ( result ) => { await waitASecond ( 3 ) ; thisTask . title += 'Done!' ; listrContext . projectFilesStaged = true ; finalizeObservableTaskResult ( otr ) ; } ) . catch ( async ( error ) => { await waitASecond ( 3 ) ; thisTask . title += 'Failed' ; listrContext . projectFilesStaged = false ; finalizeObservableTaskResult ( otr , error ) ; } ) ; } ) ; } } as ListrTask ; } function returns { void } description private function validateGitCloneArguments ( ) { if ( typeof arguments [ 0 ] !== 'string' || arguments [ 0 ] === '' ) { throw new SfdxFalconError ( `Expected gitRemoteUri to be a non-empty string but got type '${ typeof arguments [ 0 ] }' instead.` , `TypeError` , `${ dbgNs }validateGitCloneArguments` ) ; } if ( typeof arguments [ 1 ] !== 'string' || arguments [ 1 ] === '' ) { throw new SfdxFalconError ( `Expected targetDirectory to be a non-empty string but got type '${ typeof arguments [ 1 ] }' instead.` , `TypeError` , `${ dbgNs }validateGitCloneArguments` ) ; } if ( typeof arguments [ 2 ] !== 'string' ) { throw new SfdxFalconError ( `Expected gitCloneDirectory to be a string but got type '${ typeof arguments [ 2 ] }' instead.` , `TypeError` , `${ dbgNs }validateGitCloneArguments` ) ; } } function param { string } gitRemoteUri returns { ListrTask } description public export function validateGitRemote ( gitRemoteUri = '' ) { if ( typeof gitRemoteUri !== 'string' ) { throw new SfdxFalconError ( `Expected gitRemoteUri to be string but got type '${ typeof gitRemoteUri }' instead.` , `TypeError` , `${ dbgNs }validateGitRemote` ) ; } return { title : 'Validating Git Remote...' , enabled : listrContext => ( gitRemoteUri && listrContext . gitInstalled === true ) , task : ( listrContext , thisTask ) => { return gitHelper . checkGitRemoteStatus ( gitRemoteUri , 3 ) . then ( result => { thisTask . title += 'Valid!' ; listrContext . wizardInitialized = true ; } ) . catch ( result => { thisTask . title += 'ERROR' ; if ( result instanceof Error ) { throw new SfdxFalconError ( 'There was a problem with your Git Remote.' , 'InvalidGitRemote' , `${ dbgNs }validateGitRemote` , result ) ; } else { throw new SfdxFalconError ( `There was a problem with your Git Remote: ${ result . message }.` , 'InvalidGitRemote' , `${ dbgNs }validateGitRemote` ) ; } } ) ; } } as ListrTask ; } function returns { void } description private function validateSharedData ( ) { if ( typeof this . sharedData !== 'object' ) { throw new SfdxFalconError ( `Expected this.sharedData to be an object available in the calling scope. Got type '${ typeof this . sharedData }' instead. ` + `You must execute listr-tasks functions using the syntax: functionName.call(this). ` + `You must also ensure that the calling scope has defined an object named 'sharedData'.` , `InvalidSharedData` , `${ dbgNs }validateSharedData` ) ; } } function returns { void } description private function validatePkgConversionArguments ( ) { if ( typeof arguments [ 0 ] !== 'string' || arguments [ 0 ] === '' ) { throw new SfdxFalconError ( `Expected aliasOrUsername to be a non-empty string but got type '${ typeof arguments [ 0 ] }' instead.` , `TypeError` , `${ dbgNs }validatePkgConversionArguments` ) ; } if ( Array . isArray ( arguments [ 1 ] ) !== true || arguments [ 1 ] . length < 1 ) { throw new SfdxFalconError ( `Expected packageNames to be a non-empty array but got type '${ typeof arguments [ 1 ] }' instead.` , `TypeError` , `${ dbgNs }validatePkgConversionArguments` ) ; } if ( typeof arguments [ 2 ] !== 'string' || arguments [ 2 ] === '' ) { throw new SfdxFalconError ( `Expected retrieveTargetDir to be a non-empty string but got type '${ typeof arguments [ 2 ] }' instead.` , `TypeError` , `${ dbgNs }validatePkgConversionArguments` ) ; } }