file copyright author Vivek M. Chawla <@VivekMChawla>
 *  summary description version license import * as yoValidate from '../sfdx-falcon-validators/yeoman-validator' ; import * as gitHelper from './git' ; import { SfdxFalconDebug } from '../sfdx-falcon-debug' ; import { SfdxFalconError } from '../sfdx-falcon-error' ; import { filterLocalPath } from '../sfdx-falcon-util/yeoman' ; import { YeomanSeparator } from '../sfdx-falcon-util/yeoman' ; import { ConfirmationAnswers } from '../sfdx-falcon-types' ; import { Question } from '../sfdx-falcon-types' ; import { Questions } from '../sfdx-falcon-types' ; import { ShellExecResult } from '../sfdx-falcon-types' ; import { YeomanChoice } from '../sfdx-falcon-types' ; const dbgNs = 'UTILITY:inquirer-questions:' ; const PKG_PROJECT_TYPE_CHOICES = [ new YeomanSeparator ( ) , { name : 'Managed Package (1GP)' , value : '1GP:managed' , short : 'Managed Package (1GP)' } ] ; function param { YeomanChoice [ ] } devHubChoices returns { Question } description public export function chooseDevHub ( devHubChoices ? ) { SfdxFalconDebug . obj ( `${ dbgNs }chooseDevHub:` , arguments , `arguments: ` ) ; if ( typeof devHubChoices === 'undefined' ) { validateInterviewScope . call ( this ) ; devHubChoices = this . sharedData [ 'devHubAliasChoices' ] ; } if ( Array . isArray ( devHubChoices ) !== true ) { throw new SfdxFalconError ( `Expected devHubChoices to be an Array. Got type '${ typeof devHubChoices }' instead. ` , `TypeError` , `${ dbgNs }chooseDevHub` ) ; } return { type : 'list' , name : 'devHubUsername' , message : 'Which DevHub do you want to use for this project?' , choices : devHubChoices , when : devHubChoices . length > 0 } ; } function param { YeomanChoice [ ] } envHubChoices returns { Question } description public export function chooseEnvHub ( envHubChoices ? ) { SfdxFalconDebug . obj ( `${ dbgNs }chooseEnvHub:` , arguments , `arguments: ` ) ; if ( typeof envHubChoices === 'undefined' ) { validateInterviewScope . call ( this ) ; envHubChoices = this . sharedData [ 'envHubAliasChoices' ] ; } if ( Array . isArray ( envHubChoices ) !== true ) { throw new SfdxFalconError ( `Expected envHubChoices to be an Array. Got type '${ typeof envHubChoices }' instead. ` , `TypeError` , `${ dbgNs }chooseEnvHub` ) ; } return { type : 'list' , name : 'envHubUsername' , message : 'Which Environment Hub do you want to use for this project?' , choices : envHubChoices , when : envHubChoices . length > 0 } ; } function param { YeomanChoice [ ] } pkgOrgChoices returns { Question } description public export function choosePkgOrg ( pkgOrgChoices ? ) { SfdxFalconDebug . obj ( `${ dbgNs }choosePkgOrg:` , arguments , `arguments: ` ) ; if ( typeof pkgOrgChoices === 'undefined' ) { validateInterviewScope . call ( this ) ; SfdxFalconDebug . obj ( `${ dbgNs }choosePkgOrg:context.userAnswers:` , this . context . userAnswers , `this.context.userAnswers: ` ) ; SfdxFalconDebug . obj ( `${ dbgNs }choosePkgOrg:sharedData.pkgOrgAliasChoices:` , this . sharedData [ 'pkgOrgAliasChoices' ] , `this.sharedData['pkgOrgAliasChoices']: ` ) ; if ( typeof this . context . userAnswers === 'object' ) { switch ( this . context . userAnswers . projectType ) { case '1GP:managed' : pkgOrgChoices = this . sharedData [ 'managedPkgOrgAliasChoices' ] ; break ; case '1GP:unmanaged' : pkgOrgChoices = this . sharedData [ 'unmanagedPkgOrgAliasChoices' ] ; break ; default : throw new SfdxFalconError ( `Invalid Project Type: '${ this . finalAnswers . projectType }'. ` , `InvalidProjectType` , `${ dbgNs }choosePkgOrg` ) ; } } else { pkgOrgChoices = this . sharedData [ 'pkgOrgAliasChoices' ] ; } } if ( Array . isArray ( pkgOrgChoices ) !== true ) { throw new SfdxFalconError ( `Expected pkgOrgChoices to be an Array. Got type '${ typeof pkgOrgChoices }' instead. ` , `TypeError` , `${ dbgNs }choosePkgOrg` ) ; } return { type : 'list' , name : 'pkgOrgUsername' , message : 'Which Packaging Org do you want to use for this project?' , choices : pkgOrgChoices , when : pkgOrgChoices . length > 0 } ; } function param { YeomanChoice [ ] } pkgProjectTypeChoices returns { Questions } description public export function choosePkgProjectType ( pkgProjectTypeChoices = PKG_PROJECT_TYPE_CHOICES ) { SfdxFalconDebug . obj ( `${ dbgNs }chooseEnvHub:` , arguments , `arguments: ` ) ; if ( Array . isArray ( pkgProjectTypeChoices ) !== true ) { throw new SfdxFalconError ( `Expected pkgProjectTypeChoices argument to be an Array. Got type '${ typeof pkgProjectTypeChoices }' instead. ` , `InvalidCallScope` , `${ dbgNs }choosePkgProjectType` ) ; } return [ { type : 'list' , name : 'projectType' , message : 'What type of packaging project will this be?' , choices : pkgProjectTypeChoices , when : pkgProjectTypeChoices . length > 0 } , { type : 'confirm' , name : 'pkgOrgExists' , message : 'Have you created a packaging org?' , default : false , when : answerHash => String ( answerHash . projectType ) . startsWith ( '1GP:' ) } ] ; } function returns { Questions } description public export function confirmNoDevHub ( ) { validateInterviewScope . call ( this ) ; return [ { type : 'confirm' , name : 'restart' , message : 'Selecting a DevHub is required. Would you like to see the choices again?' , default : true , when : this . userAnswers . devHubUsername === 'NOT_SPECIFIED' } ] ; } function returns { Questions } description public export function confirmNoEnvHub ( ) { validateInterviewScope . call ( this ) ; return [ { type : 'confirm' , name : 'restart' , message : 'Selecting an Environment Hub is recommended but NOT required. Would you like to see the choices again?' , default : true , when : this . userAnswers . envHubUsername === 'NOT_SPECIFIED' && ( Array . isArray ( this . sharedData [ 'envHubAliasChoices' ] ) && this . sharedData [ 'envHubAliasChoices' ] . length > 2 ) } ] ; } function returns { Questions } description public export function confirmNoGitHubRepo ( ) { validateInterviewScope . call ( this ) ; return [ { type : 'confirm' , name : 'restart' , message : 'The Git Remote you specified does not exist or is unreachable. Continue anyway?' , default : false , when : ( confirmationAnswers ) => { return new Promise ( ( resolve , reject ) => { if ( this . userAnswers [ 'hasGitRemote' ] === false || typeof this . userAnswers [ 'gitRemoteUri' ] === 'undefined' ) { confirmationAnswers . proceed = true ; resolve ( false ) ; } else { return gitHelper . checkGitRemoteStatus ( this . userAnswers [ 'gitRemoteUri' ] ) . then ( ( successResult ) => { this . userAnswers [ 'isGitRemoteReachable' ] = true ; } ) . catch ( ( errorResult ) => { if ( errorResult . code === 2 ) { this . userAnswers [ 'isGitRemoteReachable' ] = true ; } else { this . userAnswers [ 'isGitRemoteReachable' ] = false ; } } ) . finally ( ( ) => { confirmationAnswers . proceed = this . userAnswers [ 'isGitRemoteReachable' ] ; resolve ( ! this . userAnswers [ 'isGitRemoteReachable' ] ) ; } ) ; } } ) ; } } , { type : 'confirm' , name : 'restart' , message : 'Specifying a GitHub Remote is strongly recommended. Skip anyway?' , default : false , when : userInput => { if ( typeof userInput . restart === 'undefined' && this . userAnswers . hasGitRemote !== true ) { userInput . proceed = false ; return true ; } else { return false ; } } } ] ; } function returns { Questions } description public export function confirmNoPkgOrg ( ) { validateInterviewScope . call ( this ) ; return [ { type : 'confirm' , name : 'restart' , message : 'A Packaging Org is required for 1GP projects. Would you like to modify your selection?' , default : true , when : ( ) => String ( this . userAnswers . projectType ) . startsWith ( '1GP:' ) && this . userAnswers . pkgOrgExists !== true } ] ; } function returns { Questions } description public export function confirmNoPkgOrgConnection ( ) { validateInterviewScope . call ( this ) ; SfdxFalconDebug . obj ( `${ dbgNs }confirmNoPkgOrgConnection:` , this . context . userAnswers , `this.context.userAnswers: ` ) ; return [ { type : 'confirm' , name : 'restart' , message : 'A connection to your Packaging Org is required to continue. Would you like to modify your selection?' , default : true , when : ( ) => this . userAnswers . pkgOrgUsername === 'NOT_SPECIFIED' } ] ; } function returns { Questions } description public export function confirmProceedRestart ( ) { validateInterviewScope . call ( this ) ; let confirmationQuestion = 'Would you like to proceed based on the above settings?' ; if ( typeof this . context . confirmationQuestion === 'string' ) { confirmationQuestion = this . context . confirmationQuestion ; SfdxFalconDebug . msg ( `${ dbgNs }confirmProceedRestartAbort:` , `Parent Confirmation Question Found. ` ) ; } if ( this . context . context && typeof this . context . context . confirmationQuestion === 'string' ) { confirmationQuestion = this . context . context . confirmationQuestion ; SfdxFalconDebug . msg ( `${ dbgNs }confirmProceedRestartAbort:` , `Grandparent Confirmation Question Found. ` ) ; } SfdxFalconDebug . str ( `${ dbgNs }confirmProceedRestartAbort:` , confirmationQuestion , `confirmationQuestion: ` ) ; return [ { type : 'confirm' , name : 'proceed' , message : confirmationQuestion , default : false , when : true } , { type : 'confirm' , name : 'restart' , message : 'Would you like to start again and enter new values?' , default : true , when : answerHash => ! answerHash . proceed } ] ; } function returns { Questions } description public export function provideDeveloperInfo ( ) { validateInterviewScope . call ( this ) ; return [ { type : 'input' , name : 'developerName' , message : 'What is your Company\'s Name (or your name if individual developer)?' , default : ( typeof this . userAnswers . developerName !== 'undefined' ) ? this . userAnswers . developerName : this . defaultAnswers . developerName , validate : yoValidate . standardName , filter : ( userInput ) => userInput . trim ( ) , when : true } , { type : 'input' , name : 'developerAlias' , message : 'Provide an alias for the above (1-15 chars: a-Z, 0-9, -, and _ only)' , default : ( typeof this . userAnswers . developerAlias !== 'undefined' ) ? this . userAnswers . developerAlias : this . defaultAnswers . developerAlias , validate : yoValidate . standardAlias , when : true } ] ; } function returns { Questions } description public export function provideGitRemote ( ) { validateInterviewScope . call ( this ) ; return [ { type : 'confirm' , name : 'isInitializingGit' , message : 'Would you like to initialize Git for this project? (RECOMMENDED)' , default : ( typeof this . userAnswers . isInitializingGit !== 'undefined' ) ? this . userAnswers . isInitializingGit : this . defaultAnswers . isInitializingGit , when : true } , { type : 'confirm' , name : 'hasGitRemote' , message : 'Have you created a Remote Git Repository for your project?' , default : ( typeof this . userAnswers . hasGitRemote !== 'undefined' ) ? this . userAnswers . hasGitRemote : this . defaultAnswers . hasGitRemote , when : answerHash => answerHash . isInitializingGit } , { type : 'input' , name : 'gitRemoteUri' , message : 'What is the URI of your Git Remote?' , default : ( typeof this . userAnswers . gitRemoteUri !== 'undefined' ) ? this . userAnswers . gitRemoteUri : this . defaultAnswers . gitRemoteUri , validate : yoValidate . gitRemoteUri , when : answerHash => answerHash . hasGitRemote } ] ; } function returns { Questions } description public export function provideProjectInfo ( ) { validateInterviewScope . call ( this ) ; return [ { type : 'input' , name : 'projectName' , message : 'What is the name of your project?' , default : ( typeof this . userAnswers . projectName !== 'undefined' ) ? this . userAnswers . projectName : this . defaultAnswers . projectName , validate : yoValidate . standardName , filter : ( userInput ) => userInput . trim ( ) , when : true } , { type : 'input' , name : 'projectAlias' , message : 'Provide an alias for the above (1-15 chars: a-Z, 0-9, -, and _ only)' , default : ( typeof this . userAnswers . projectAlias !== 'undefined' ) ? this . userAnswers . projectAlias : this . defaultAnswers . projectAlias , validate : yoValidate . standardAlias , when : true } ] ; } function returns { Questions } description public export function provideTargetDirectory ( ) { validateInterviewScope . call ( this ) ; return [ { type : 'input' , name : 'targetDirectory' , message : 'What is the target directory for this project?' , default : ( typeof this . userAnswers . targetDirectory !== 'undefined' ) ? this . userAnswers . targetDirectory : this . defaultAnswers . targetDirectory , validate : yoValidate . targetPath , filter : filterLocalPath , when : true } ] ; } function returns { void } description private function validateInterviewScope ( ) { if ( typeof this . userAnswers !== 'object' ) { throw new SfdxFalconError ( `Expected this.userAnswers to be an object available in the calling scope. Got type '${ typeof this . userAnswers }' instead. ` + `Please execute this function using the syntax: functionName.call(this)` , `InvalidCallScope` , `${ dbgNs }validateInterviewScope` ) ; } if ( typeof this . defaultAnswers !== 'object' ) { throw new SfdxFalconError ( `Expected this.defaultAnswers to be an object available in the calling scope. Got type '${ typeof this . defaultAnswers }' instead. ` + `Please execute this function using the syntax: functionName.call(this)` , `InvalidCallScope` , `${ dbgNs }validateInterviewScope` ) ; } if ( typeof this . confirmationAnswers !== 'object' ) { throw new SfdxFalconError ( `Expected this.confirmationAnswers to be an object available in the calling scope. Got type '${ typeof this . confirmationAnswers }' instead. ` + `Please execute this function using the syntax: functionName.call(this)` , `InvalidCallScope` , `${ dbgNs }validateInterviewScope` ) ; } if ( typeof this . context !== 'object' ) { throw new SfdxFalconError ( `Expected this.context to be an object available in the calling scope. Got type '${ typeof this . context }' instead. ` + `Please execute this function using the syntax: functionName.call(this)` , `InvalidCallScope` , `${ dbgNs }validateInterviewScope` ) ; } 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. ` + `Please execute this function using the syntax: functionName.call(this)` , `InvalidCallScope` , `${ dbgNs }validateInterviewScope` ) ; } }