smartinteract/dist/smartinteract.classes.smartinteract.js

129 lines
7.2 KiB
JavaScript
Raw Normal View History

2016-11-20 00:11:22 +00:00
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
2016-11-20 00:11:22 +00:00
const plugins = require("./smartinteract.plugins");
const smartq = require("smartq");
2016-11-20 00:11:22 +00:00
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 = smartq.defer();
2016-11-20 21:24:09 +00:00
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) => {
done.resolve({
name: optionsArg.name,
value: answers[optionsArg.name]
});
});
}
else {
let answer = {
2016-11-20 00:11:22 +00:00
name: optionsArg.name,
2016-11-20 21:24:09 +00:00
value: optionsArg.default
};
done.resolve(answer);
}
2016-11-20 00:11:22 +00:00
return done.promise;
}
/**
* add questions to queue
*/
addQuestions(questionArrayArg) {
this.questionMap.addArray(questionArrayArg);
}
/**
* run the question queue
*/
runQueue() {
let done = smartq.defer();
2016-11-20 21:24:09 +00:00
let answerBucket = new AnswerBucket();
let handleQuestion = () => __awaiter(this, void 0, void 0, function* () {
if (!this.questionMap.isEmpty()) {
let oneQuestion = this.questionMap.getOneAndRemove();
let answer = yield this.askQuestion(oneQuestion);
answerBucket.addAnswer(answer);
handleQuestion(); // recursion: as questions until empty
}
else {
done.resolve(answerBucket); // when empty, then resolve promise
}
});
2016-11-20 00:11:22 +00:00
handleQuestion();
return done.promise;
}
2016-11-20 21:24:09 +00:00
/**
* checks if the current env is valid for userinput
*/
isValidEnv() {
if (!process.env.CI) {
return true;
}
else {
return false;
}
}
2016-11-20 00:11:22 +00:00
}
exports.SmartInteract = SmartInteract;
2016-11-20 21:24:09 +00:00
/**
* class AnswerBucket holds answers
*/
class AnswerBucket {
constructor() {
this.answerMap = new lik_1.Objectmap();
}
/**
* add an answer to the bucket
*/
addAnswer(answerArg) {
this.answerMap.add(answerArg);
}
/**
* gets an answer for a specific name
*/
getAnswerFor(nameArg) {
let answer = this.answerMap.find(answerArg => {
return answerArg.name === nameArg;
});
return answer.value;
}
/**
* gets all answers as array
*/
getAllAnswers() {
return this.answerMap.getArray();
}
}
exports.AnswerBucket = AnswerBucket;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRpbnRlcmFjdC5jbGFzc2VzLnNtYXJ0aW50ZXJhY3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGludGVyYWN0LmNsYXNzZXMuc21hcnRpbnRlcmFjdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQUEsbURBQWtEO0FBQ2xELGlDQUFnQztBQUNoQyw2QkFBK0I7QUEyQi9COztHQUVHO0FBQ0g7SUFPRTs7T0FFRztJQUNILFlBQWEsZ0JBQW9DO1FBUmpEOztXQUVHO1FBQ0ssZ0JBQVcsR0FBRyxJQUFJLGVBQVMsRUFBbUIsQ0FBQTtRQU1wRCxFQUFFLENBQUMsQ0FBQyxnQkFBZ0IsQ0FBQyxDQUFDLENBQUM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