file copyright author Vivek M. Chawla <@VivekMChawla>
 *  summary description version license import { SfdxFalconDebug } from '../sfdx-falcon-debug' ; import { SfdxFalconKeyValueTable } from '../sfdx-falcon-util/ux' ; import { AnswersDisplay } from '../sfdx-falcon-types' ; import { ConfirmationAnswers } from '../sfdx-falcon-types' ; import { PromptOptions } from '../sfdx-falcon-types' ; import { Questions } from '../sfdx-falcon-types' ; import { QuestionsBuilder } from '../sfdx-falcon-types' ; const inquirer = require ( 'inquirer' ) ; const dbgNs = 'MODULE:sfdx-falcon-prompt:' ; class summary description public export class SfdxFalconPrompt < T extends object > { public readonly defaultAnswers : T ; public userAnswers : T ; public confirmationAnswers : ConfirmationAnswers ; public context : object ; private readonly _questions : Questions | QuestionsBuilder ; private readonly _confirmation : Questions | QuestionsBuilder ; private readonly _display : AnswersDisplay < T > ; private readonly _invertConfirmation : boolean ; public get confirmation ( ) : Questions { if ( typeof this . _confirmation === 'function' ) { return this . _confirmation . call ( this ) ; } else { return this . _confirmation ; } } public get sharedData ( ) : object { return this . context [ 'sharedData' ] ; } public get finalAnswers ( ) : T { return { ... this . defaultAnswers as object , ... this . userAnswers as object } as T ; } public get questions ( ) : Questions { if ( typeof this . _questions === 'function' ) { return this . _questions . call ( this ) ; } else { return this . _questions ; } } constructs param { PromptOptions < T > } opts description public constructor ( opts ) { this . _questions = opts . questions ; this . _confirmation = opts . confirmation ; this . _display = opts . display ; this . _invertConfirmation = opts . invertConfirmation || false ; this . defaultAnswers = opts . defaultAnswers ; this . context = opts . context || { } as object ; this . confirmationAnswers = { } as ConfirmationAnswers ; this . userAnswers = { } as T ; } method returns { Promise < T > } description public async public async prompt ( ) { do { const questions = this . questions ; SfdxFalconDebug . obj ( `${ dbgNs }prompt:questions:` , questions , `questions: ` ) ; this . userAnswers = await inquirer . prompt ( questions ) as T ; await this . displayAnswers ( ) ; } while ( await this . confirmRestart ( ) ) ; return this . userAnswers ; } method returns { Promise < boolean > } description private async private async confirmRestart ( ) { if ( typeof this . _confirmation === 'undefined' ) { this . confirmationAnswers . proceed = true ; this . confirmationAnswers . restart = false ; this . confirmationAnswers . abort = false ; return false ; } this . confirmationAnswers = await inquirer . prompt ( this . confirmation ) as ConfirmationAnswers ; SfdxFalconDebug . obj ( `${ dbgNs }confirmRestart:` , this . confirmationAnswers , `this.confirmationAnswers: ` ) ; if ( this . confirmationAnswers . proceed === true ) { return false ; } const invertConfirmation = this . _invertConfirmation ? 1 : 0 ; const restart = this . confirmationAnswers . restart ? 1 : 0 ; if ( ( invertConfirmation ^ restart ) === 0 ) { return false ; } else { console . log ( '' ) ; return true ; } } method returns { Promise < void > } description private async private async displayAnswers ( ) { if ( typeof this . _display === 'function' ) { const displayResults = await this . _display ( this . userAnswers ) ; if ( Array . isArray ( displayResults ) ) { const falconTable = new SfdxFalconKeyValueTable ( ) ; console . log ( '' ) ; falconTable . render ( displayResults ) ; console . log ( '' ) ; } } } }