smartinteract/dist/smartinteract.classes.smartinteract.js
2016-11-20 01:11:22 +01:00

69 lines
4.2 KiB
JavaScript

"use strict";
const plugins = require("./smartinteract.plugins");
const q = require("q");
const lik_1 = require("lik");
/**
* 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 lik_1.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) {
let done = q.defer();
plugins.inquirer.prompt([{
name: optionsArg.name,
type: optionsArg.type,
message: optionsArg.message,
default: optionsArg.default,
choices: optionsArg.choices,
validate: optionsArg.validate
}]).then((answers) => {
console.log(answers);
done.resolve(answers);
});
return done.promise;
}
/**
* add questions to queue
*/
addQuestions(questionArrayArg) {
this.questionMap.addArray(questionArrayArg);
}
/**
* run the question queue
*/
runQueue() {
let done = q.defer();
let answerMap = new lik_1.Objectmap();
let handleQuestion = () => {
let oneQuestion = this.questionMap.getOneAndRemove();
this.askQuestion(oneQuestion).then(x => {
answerMap.addArray(x);
if (!this.questionMap.isEmpty()) {
handleQuestion(); // recursion: as questions until empty
}
else {
done.resolve(answerMap.getArray()); // when empty, then resolve promise
}
});
};
handleQuestion();
return done.promise;
}
}
exports.SmartInteract = SmartInteract;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRpbnRlcmFjdC5jbGFzc2VzLnNtYXJ0aW50ZXJhY3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGludGVyYWN0LmNsYXNzZXMuc21hcnRpbnRlcmFjdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsbURBQWtEO0FBQ2xELHVCQUFzQjtBQUN0Qiw2QkFBK0I7QUEwQi9COztHQUVHO0FBQ0g7SUFPS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