"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const plugins = __importStar(require("./smartinteract.plugins")); const smartpromise = __importStar(require("@pushrocks/smartpromise")); const smartinteract_classes_answerbucket_1 = require("./smartinteract.classes.answerbucket"); /** * class SmartInteract - allows to specify an user interaction during runtime */ class SmartInteract { /** * constructor of class SmartInteract */ constructor(questionArrayArg) { /** * holds the qestion queue, that is emptied once you call */ this.questionMap = new plugins.lik.Objectmap(); if (questionArrayArg) { this.addQuestions(questionArrayArg); } } /** * allows you to ask a single question and returns the answer in a promise * skips the queue */ askQuestion(optionsArg) { const done = smartpromise.defer(); if (this.isValidEnv()) { plugins.inquirer .prompt([ { name: optionsArg.name, type: optionsArg.type, message: optionsArg.message, default: optionsArg.default, choices: optionsArg.choices, validate: optionsArg.validate } ]) .then(answers => { // adjust to the fact that now dots define paths for inquirer const answerValue = plugins.smartparam.smartGet(answers, optionsArg.name); done.resolve({ name: optionsArg.name, value: answerValue }); }) .catch(err => { console.log(err); }); } else { const answer = { name: optionsArg.name, value: optionsArg.default }; done.resolve(answer); } return done.promise; } /** * add questions to queue */ addQuestions(questionArrayArg) { this.questionMap.addArray(questionArrayArg); } /** * run the question queue */ runQueue() { const done = smartpromise.defer(); const answerBucket = new smartinteract_classes_answerbucket_1.AnswerBucket(); const handleQuestion = async () => { if (!this.questionMap.isEmpty()) { const oneQuestion = this.questionMap.getOneAndRemove(); const answer = await this.askQuestion(oneQuestion); answerBucket.addAnswer(answer); handleQuestion(); // recursion: as questions until empty } else { done.resolve(answerBucket); // when empty, then resolve promise } }; handleQuestion(); return done.promise; } /** * checks if the current env is valid for userinput */ isValidEnv() { if (!process.env.CI) { return true; } else { return false; } } } exports.SmartInteract = SmartInteract; //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9u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