file copyright author Vivek M. Chawla <@VivekMChawla>
 *  summary description version license import * as path from 'path' ; import * as gitHelper from '../modules/sfdx-falcon-util/git' ; import * as iq from '../modules/sfdx-falcon-util/interview-questions' ; import { SfdxFalconDebug } from '../modules/sfdx-falcon-debug' ; import { SfdxFalconError } from '../modules/sfdx-falcon-error' ; import { SfdxFalconInterview } from '../modules/sfdx-falcon-interview' ; import { SfdxFalconKeyValueTableDataRow } from '../modules/sfdx-falcon-util/ux' ; import { SfdxFalconTableData } from '../modules/sfdx-falcon-util/ux' ; import { GeneratorOptions } from '../modules/sfdx-falcon-yeoman-command' ; import { SfdxFalconYeomanGenerator } from '../modules/sfdx-falcon-yeoman-generator' ; import { YeomanChoice } from '../modules/sfdx-falcon-types' ; import { SfdxOrgInfoMap } from '../modules/sfdx-falcon-types' ; const chalk = require ( 'chalk' ) ; const dbgNs = 'GENERATOR:clone-appx-package:' ; interface description interface InterviewAnswers { targetDirectory : string ; devHubAlias : string ; envHubAlias : string ; pkgOrgAlias : string ; devHubUsername : string ; envHubUsername : string ; pkgOrgUsername : string ; gitRemoteUri : string ; gitCloneDirectory : string ; } class extends SfdxFalconYeomanGenerator summary description public export default class CloneAppxPackageProject extends SfdxFalconYeomanGenerator < InterviewAnswers > { protected devHubAliasChoices : YeomanChoice [ ] ; protected envHubAliasChoices : YeomanChoice [ ] ; protected pkgOrgAliasChoices : YeomanChoice [ ] ; protected managedPkgOrgAliasChoices : YeomanChoice [ ] ; protected unmanagedPkgOrgAliasChoices : YeomanChoice [ ] ; protected gitRemoteUri : string ; protected gitCloneDirectory : string ; protected localProjectPath : string ; constructs param { string | string [ ] } args param { GeneratorOptions } opts description public constructor ( args , opts ) { super ( args , opts ) ; this . confirmationQuestion = 'Clone the AppExchange Package Kit (APK) project using the above settings?' ; this . gitRemoteUri = opts . gitRemoteUri as string ; this . gitCloneDirectory = opts . gitCloneDir as string ; this . devHubAliasChoices = new Array < YeomanChoice > ( ) ; this . envHubAliasChoices = new Array < YeomanChoice > ( ) ; this . pkgOrgAliasChoices = new Array < YeomanChoice > ( ) ; this . managedPkgOrgAliasChoices = new Array < YeomanChoice > ( ) ; this . unmanagedPkgOrgAliasChoices = new Array < YeomanChoice > ( ) ; this . defaultAnswers . targetDirectory = path . resolve ( opts . outputDir as string ) ; this . defaultAnswers . devHubAlias = 'NOT_SPECIFIED' ; this . defaultAnswers . envHubAlias = 'NOT_SPECIFIED' ; this . defaultAnswers . pkgOrgAlias = 'NOT_SPECIFIED' ; this . defaultAnswers . devHubUsername = 'NOT_SPECIFIED' ; this . defaultAnswers . envHubUsername = 'NOT_SPECIFIED' ; this . defaultAnswers . pkgOrgUsername = 'NOT_SPECIFIED' ; this . defaultAnswers . gitRemoteUri = this . gitRemoteUri ; this . defaultAnswers . gitCloneDirectory = this . gitCloneDirectory ; this . sharedData [ 'devHubAliasChoices' ] = this . devHubAliasChoices ; this . sharedData [ 'envHubAliasChoices' ] = this . envHubAliasChoices ; this . sharedData [ 'pkgOrgAliasChoices' ] = this . pkgOrgAliasChoices ; this . sharedData [ 'managedPkgOrgAliasChoices' ] = this . managedPkgOrgAliasChoices ; this . sharedData [ 'unmanagedPkgOrgAliasChoices' ] = this . unmanagedPkgOrgAliasChoices ; this . sharedData [ 'cliCommandName' ] = this . cliCommandName ; this . sharedData [ 'gitRemoteUri' ] = this . gitRemoteUri ; } method returns { SfdxFalconInterview < InterviewAnswers > } description protected protected _buildInterview ( ) { const interview = new SfdxFalconInterview < InterviewAnswers > ( { defaultAnswers : this . defaultAnswers , confirmation : iq . confirmProceedRestart , confirmationHeader : chalk . yellow ( 'Review Your Settings:' ) , display : this . _buildInterviewAnswersTableData , context : this , sharedData : this . sharedData } ) ; interview . createGroup ( { title : chalk . yellow ( '\nTarget Directory:' ) , questions : iq . provideTargetDirectory } ) ; interview . createGroup ( { title : chalk . yellow ( '\nDevHub Selection:' ) , questions : iq . chooseDevHub , confirmation : iq . confirmNoDevHub , abort : groupAnswers => { if ( groupAnswers . devHubUsername === 'NOT_SPECIFIED' ) { return 'A connection to your DevHub is required to continue.' ; } else { return false ; } } } ) ; interview . createGroup ( { title : chalk . yellow ( '\nEnvironment Hub Selection:' ) , questions : iq . chooseEnvHub , confirmation : iq . confirmNoEnvHub } ) ; return interview ; } method param { InterviewAnswers } userAnswers returns { Promise < SfdxFalconTableData > } description protected async protected async _buildInterviewAnswersTableData ( interviewAnswers ) { const tableData = new Array < SfdxFalconKeyValueTableDataRow > ( ) ; const sfdxOrgInfoMap = this . sharedData [ 'sfdxOrgInfoMap' ] as SfdxOrgInfoMap ; const repoClonedInto = this . defaultAnswers . gitCloneDirectory ? path . join ( interviewAnswers . targetDirectory , this . defaultAnswers . gitCloneDirectory ) : path . join ( interviewAnswers . targetDirectory , gitHelper . getRepoNameFromUri ( this . defaultAnswers . gitRemoteUri ) ) ; tableData . push ( { option : 'Git Remote URI:' , value : `${ this . defaultAnswers . gitRemoteUri }` } ) ; tableData . push ( { option : 'Clone Repo Into:' , value : `${ repoClonedInto }` } ) ; const devHubAlias = sfdxOrgInfoMap . get ( interviewAnswers . devHubUsername ) ? sfdxOrgInfoMap . get ( interviewAnswers . devHubUsername ) . alias : 'NOT_SPECIFIED' ; tableData . push ( { option : 'Dev Hub Alias:' , value : `${ devHubAlias }` } ) ; const envHubAlias = sfdxOrgInfoMap . get ( interviewAnswers . envHubUsername ) ? sfdxOrgInfoMap . get ( interviewAnswers . envHubUsername ) . alias : 'NOT_SPECIFIED' ; tableData . push ( { option : 'Env Hub Alias:' , value : `${ envHubAlias }` } ) ; return tableData ; } method returns { Promise < boolean > } description protected async protected async _checkForPkgOrgConnection ( ) { return false ; } method returns { Promise < void > } description protected async protected async initializing ( ) { return this . _default_initializing ( ) ; } method returns { Promise < void > } description protected async protected async prompting ( ) { console . log ( chalk `{yellow Starting APK project cloning interview...}` ) ; return this . _default_prompting ( ) ; } method returns { Promise < void > } description protected async protected async configuring ( ) { if ( this . generatorStatus . aborted ) { SfdxFalconDebug . msg ( `${ dbgNs }configuring:` , `generatorStatus.aborted found as TRUE inside configuring()` ) ; return ; } this . localProjectPath = await this . _cloneRepository ( ) ; console . log ( '' ) ; } method returns { Promise < void > } description protected async protected async writing ( ) { if ( this . generatorStatus . aborted ) { SfdxFalconDebug . msg ( `${ dbgNs }writing:` , `generatorStatus.aborted found as TRUE inside writing()` ) ; return ; } if ( ! this . localProjectPath ) { return ; } const falconProjectConfig = await this . _resolveFalconProjectConfig ( this . localProjectPath ) ; SfdxFalconDebug . obj ( `${ dbgNs }writing:falconProjectConfig:` , falconProjectConfig , `falconProjectConfig: ` ) ; if ( falconProjectConfig === null ) { return ; } const sfdxOrgInfoMap = this . sharedData [ 'sfdxOrgInfoMap' ] as SfdxOrgInfoMap ; this . finalAnswers . devHubAlias = sfdxOrgInfoMap . get ( this . finalAnswers . devHubUsername ) ? sfdxOrgInfoMap . get ( this . finalAnswers . devHubUsername ) . alias : 'NOT_SPECIFIED' ; this . finalAnswers . envHubAlias = sfdxOrgInfoMap . get ( this . finalAnswers . envHubUsername ) ? sfdxOrgInfoMap . get ( this . finalAnswers . envHubUsername ) . alias : 'NOT_SPECIFIED' ; switch ( falconProjectConfig . projectType ) { case '1GP:managed' : if ( typeof falconProjectConfig . appxPackage !== 'object' || typeof falconProjectConfig . appxPackage . metadataPackageId !== 'string' ) { throw new SfdxFalconError ( `Missing value for 'appxPackage.metadataPackageId' in sfdx-project.json. This value is required for 1GP:managed projects.` , `InvalidProjectConfig` , `${ dbgNs }writing` ) ; } for ( const sfdxOrgInfo of sfdxOrgInfoMap . values ( ) ) { if ( sfdxOrgInfo . managedPkgId && sfdxOrgInfo . managedPkgId === falconProjectConfig . appxPackage . metadataPackageId ) { this . finalAnswers . pkgOrgAlias = sfdxOrgInfo . alias ; break ; } } break ; case '1GP:unmanaged' : break ; case '2GP:managed' : break ; case '2GP:unlocked' : break ; default : throw new SfdxFalconError ( `Invalid Project Type: '${ falconProjectConfig . projectType }'. ` , `InvalidProjectType` , `${ dbgNs }writing` ) ; } this . sourceRoot ( this . localProjectPath ) ; this . destinationRoot ( this . localProjectPath ) ; SfdxFalconDebug . str ( `${ dbgNs }writing:sourceRoot` , this . sourceRoot ( ) , `SOURCE PATH: ` ) ; SfdxFalconDebug . str ( `${ dbgNs }writing:destinationRoot:` , this . destinationRoot ( ) , `DESTINATION PATH: ` ) ; this . log ( chalk `{yellow Customizing project files...}` ) ; this . fs . copyTpl ( this . templatePath ( '.templates/sfdx-falcon-config.json.ejs' ) , this . destinationPath ( '.sfdx-falcon/sfdx-falcon-config.json' ) , this ) ; this . fs . copyTpl ( this . templatePath ( 'tools/templates/local-config-template.sh.ejs' ) , this . destinationPath ( 'tools/lib/local-config.sh' ) , this ) ; return ; } method returns { void } description protected protected install ( ) { if ( this . _finalizeProjectCloning ( ) !== true ) { return ; } } method returns { void } description protected protected end ( ) { return this . _default_end ( ) ; } }