smartinteract/dist/smartinteract.classes.smartinteract.js

137 lines
7.9 KiB
JavaScript
Raw Permalink 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) => {
2017-08-09 14:30:30 +00:00
// adjust to the fact that now dots define paths for inquirer
let answerValue = answers;
let nameArray = optionsArg.name.split('.');
for (let name of nameArray) {
answerValue = answerValue[name];
}
2016-11-20 21:24:09 +00:00
done.resolve({
name: optionsArg.name,
2017-08-09 14:30:30 +00:00
value: answerValue
2016-11-20 21:24:09 +00:00
});
2017-08-09 14:30:30 +00:00
}).catch(err => {
console.log(err);
2016-11-20 21:24:09 +00:00
});
}
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;
2017-08-09 14:30:30 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRpbnRlcmFjdC5jbGFzc2VzLnNtYXJ0aW50ZXJhY3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGludGVyYWN0LmNsYXNzZXMuc21hcnRpbnRlcmFjdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQUEsbURBQWtEO0F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