| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  | import * as plugins from './smartinteract.plugins'; | 
					
						
							| 
									
										
										
										
											2018-07-25 17:37:34 +02:00
										 |  |  | import * as smartpromise from '@pushrocks/smartpromise'; | 
					
						
							| 
									
										
										
										
											2020-02-02 13:45:59 +00:00
										 |  |  | import { AnswerBucket } from './smartinteract.classes.answerbucket'; | 
					
						
							| 
									
										
										
										
											2016-11-20 01:11:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * the availeable question types | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export type questionType = | 
					
						
							|  |  |  |   | 'input' | 
					
						
							|  |  |  |   | 'confirm' | 
					
						
							|  |  |  |   | 'list' | 
					
						
							|  |  |  |   | 'rawlist' | 
					
						
							|  |  |  |   | 'expand' | 
					
						
							|  |  |  |   | 'checkbox' | 
					
						
							|  |  |  |   | 'password' | 
					
						
							|  |  |  |   | 'editor'; | 
					
						
							| 
									
										
										
										
											2016-11-20 01:11:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:51 +02:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2020-02-07 16:36:35 +00:00
										 |  |  |  * a choice | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:51 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-11-20 01:11:22 +01:00
										 |  |  | export interface IChoiceObject { | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |   name: string; | 
					
						
							|  |  |  |   value: any; | 
					
						
							| 
									
										
										
										
											2016-11-20 01:11:22 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export interface IQuestionObject { | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |   name: string; | 
					
						
							|  |  |  |   type: questionType; | 
					
						
							|  |  |  |   message: string; | 
					
						
							|  |  |  |   default: any; | 
					
						
							|  |  |  |   choices?: string[] | IChoiceObject[]; | 
					
						
							|  |  |  |   validate?: IValidateFunction; | 
					
						
							| 
									
										
										
										
											2016-11-20 01:11:22 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export interface IAnswerObject { | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |   name: string; | 
					
						
							|  |  |  |   value: any; | 
					
						
							| 
									
										
										
										
											2016-11-20 01:11:22 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export interface IValidateFunction { | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |   (anyObject: any): boolean; | 
					
						
							| 
									
										
										
										
											2016-11-20 01:11:22 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * class SmartInteract - allows to specify an user interaction during runtime | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export class SmartInteract { | 
					
						
							| 
									
										
										
										
											2017-07-28 14:27:09 +02:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * holds  the qestion queue, that is emptied once you call | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-02-02 13:45:59 +00:00
										 |  |  |   private questionMap = new plugins.lik.Objectmap<IQuestionObject>(); | 
					
						
							| 
									
										
										
										
											2017-07-28 14:27:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * constructor of class SmartInteract | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |   constructor(questionArrayArg?: IQuestionObject[]) { | 
					
						
							| 
									
										
										
										
											2017-07-28 14:27:09 +02:00
										 |  |  |     if (questionArrayArg) { | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |       this.addQuestions(questionArrayArg); | 
					
						
							| 
									
										
										
										
											2016-11-20 01:11:22 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-07-28 14:27:09 +02:00
										 |  |  |   } | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * allows you to ask a single question and returns the answer in a promise | 
					
						
							|  |  |  |    * skips the queue | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |   askQuestion(optionsArg: IQuestionObject): Promise<IAnswerObject> { | 
					
						
							| 
									
										
										
										
											2020-02-07 16:36:35 +00:00
										 |  |  |     const done = smartpromise.defer<IAnswerObject>(); | 
					
						
							| 
									
										
										
										
											2017-07-28 14:27:09 +02:00
										 |  |  |     if (this.isValidEnv()) { | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |       plugins.inquirer | 
					
						
							|  |  |  |         .prompt([ | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             name: optionsArg.name, | 
					
						
							|  |  |  |             type: optionsArg.type, | 
					
						
							|  |  |  |             message: optionsArg.message, | 
					
						
							|  |  |  |             default: optionsArg.default, | 
					
						
							|  |  |  |             choices: optionsArg.choices, | 
					
						
							|  |  |  |             validate: optionsArg.validate | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         ]) | 
					
						
							| 
									
										
										
										
											2020-02-07 16:36:35 +00:00
										 |  |  |         .then(answers => { | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |           // adjust to the fact that now dots define paths for inquirer
 | 
					
						
							| 
									
										
										
										
											2020-02-07 16:36:35 +00:00
										 |  |  |           const answerValue = plugins.smartparam.smartGet(answers, optionsArg.name); | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |           done.resolve({ | 
					
						
							|  |  |  |             name: optionsArg.name, | 
					
						
							|  |  |  |             value: answerValue | 
					
						
							|  |  |  |           }); | 
					
						
							| 
									
										
										
										
											2017-07-28 14:27:09 +02:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |         .catch(err => { | 
					
						
							|  |  |  |           console.log(err); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-07-28 14:27:09 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2020-02-07 16:36:35 +00:00
										 |  |  |       const answer: IAnswerObject = { | 
					
						
							| 
									
										
										
										
											2017-07-28 14:27:09 +02:00
										 |  |  |         name: optionsArg.name, | 
					
						
							|  |  |  |         value: optionsArg.default | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |       }; | 
					
						
							|  |  |  |       done.resolve(answer); | 
					
						
							| 
									
										
										
										
											2016-11-20 01:11:22 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |     return done.promise; | 
					
						
							| 
									
										
										
										
											2017-07-28 14:27:09 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * add questions to queue | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |   addQuestions(questionArrayArg: IQuestionObject[]) { | 
					
						
							|  |  |  |     this.questionMap.addArray(questionArrayArg); | 
					
						
							| 
									
										
										
										
											2017-07-28 14:27:09 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * run the  question queue | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |   runQueue() { | 
					
						
							| 
									
										
										
										
											2020-02-07 16:36:35 +00:00
										 |  |  |     const done = smartpromise.defer<AnswerBucket>(); | 
					
						
							|  |  |  |     const answerBucket = new AnswerBucket(); | 
					
						
							|  |  |  |     const handleQuestion = async () => { | 
					
						
							| 
									
										
										
										
											2017-07-28 14:27:09 +02:00
										 |  |  |       if (!this.questionMap.isEmpty()) { | 
					
						
							| 
									
										
										
										
											2020-02-07 16:36:35 +00:00
										 |  |  |         const oneQuestion = this.questionMap.getOneAndRemove(); | 
					
						
							|  |  |  |         const answer: IAnswerObject = await this.askQuestion(oneQuestion); | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |         answerBucket.addAnswer(answer); | 
					
						
							|  |  |  |         handleQuestion(); // recursion: as questions until empty
 | 
					
						
							| 
									
										
										
										
											2017-07-28 14:27:09 +02:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |         done.resolve(answerBucket); // when empty, then resolve promise
 | 
					
						
							| 
									
										
										
										
											2017-07-28 14:27:09 +02:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |     handleQuestion(); | 
					
						
							|  |  |  |     return done.promise; | 
					
						
							| 
									
										
										
										
											2017-07-28 14:27:09 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * checks if the current env is valid for userinput | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |   private isValidEnv(): boolean { | 
					
						
							| 
									
										
										
										
											2017-07-28 14:27:09 +02:00
										 |  |  |     if (!process.env.CI) { | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |       return true; | 
					
						
							| 
									
										
										
										
											2017-07-28 14:27:09 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-07-25 16:58:18 +02:00
										 |  |  |       return false; | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:09 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-07-28 14:27:09 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-11-20 22:24:09 +01:00
										 |  |  | } |