2016-11-20 00:11:22 +00:00
|
|
|
"use strict";
|
2020-02-07 16:45:48 +00:00
|
|
|
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;
|
2017-07-28 12:27:09 +00:00
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2020-02-07 16:45:48 +00:00
|
|
|
const plugins = __importStar(require("./smartinteract.plugins"));
|
|
|
|
const smartpromise = __importStar(require("@pushrocks/smartpromise"));
|
|
|
|
const smartinteract_classes_answerbucket_1 = require("./smartinteract.classes.answerbucket");
|
2016-11-20 00:11:22 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-02-07 16:45:48 +00:00
|
|
|
this.questionMap = new plugins.lik.Objectmap();
|
2016-11-20 00:11:22 +00:00
|
|
|
if (questionArrayArg) {
|
|
|
|
this.addQuestions(questionArrayArg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* allows you to ask a single question and returns the answer in a promise
|
|
|
|
* skips the queue
|
|
|
|
*/
|
|
|
|
askQuestion(optionsArg) {
|
2020-02-07 16:45:48 +00:00
|
|
|
const done = smartpromise.defer();
|
2016-11-20 21:24:09 +00:00
|
|
|
if (this.isValidEnv()) {
|
2020-02-07 16:45:48 +00:00
|
|
|
plugins.inquirer
|
|
|
|
.prompt([
|
|
|
|
{
|
2016-11-20 21:24:09 +00:00
|
|
|
name: optionsArg.name,
|
|
|
|
type: optionsArg.type,
|
|
|
|
message: optionsArg.message,
|
|
|
|
default: optionsArg.default,
|
|
|
|
choices: optionsArg.choices,
|
|
|
|
validate: optionsArg.validate
|
2017-08-09 14:30:30 +00:00
|
|
|
}
|
2020-02-07 16:45:48 +00:00
|
|
|
])
|
|
|
|
.then(answers => {
|
|
|
|
// adjust to the fact that now dots define paths for inquirer
|
|
|
|
const answerValue = plugins.smartparam.smartGet(answers, optionsArg.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
|
|
|
});
|
2020-02-07 16:45:48 +00:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2017-08-09 14:30:30 +00:00
|
|
|
console.log(err);
|
2016-11-20 21:24:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2020-02-07 16:45:48 +00:00
|
|
|
const 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() {
|
2020-02-07 16:45:48 +00:00
|
|
|
const done = smartpromise.defer();
|
|
|
|
const answerBucket = new smartinteract_classes_answerbucket_1.AnswerBucket();
|
|
|
|
const handleQuestion = async () => {
|
2017-07-28 12:27:09 +00:00
|
|
|
if (!this.questionMap.isEmpty()) {
|
2020-02-07 16:45:48 +00:00
|
|
|
const oneQuestion = this.questionMap.getOneAndRemove();
|
|
|
|
const answer = await this.askQuestion(oneQuestion);
|
2017-07-28 12:27:09 +00:00
|
|
|
answerBucket.addAnswer(answer);
|
|
|
|
handleQuestion(); // recursion: as questions until empty
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
done.resolve(answerBucket); // when empty, then resolve promise
|
|
|
|
}
|
2020-02-07 16:45:48 +00:00
|
|
|
};
|
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;
|
2020-02-07 16:45:48 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRpbnRlcmFjdC5jbGFzc2VzLnNtYXJ0aW50ZXJhY3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGludGVyYWN0LmNsYXNzZXMuc21hcnRpbnRlcmFjdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7QUFBQSxpRUFBbUQ7QUFDbkQsc0VBQXdEO0FBQ3hELDZGQUFvRTtBQXlDcEU7O0dBRUc7QUFDSCxNQUFhLGFBQWE7SUFNeEI7O09BRUc7SUFDSCxZQUFZLGdCQUFvQztRQVJoRDs7V0FFRztRQUNLLGdCQUFXLEdBQUcsSUFBSSxPQUFPLENBQUMsR0FBRyxDQUFDLFNBQVMsRUFBbUIsQ0FBQztRQU1qRSxJQUFJLGdCQUFnQixFQUFFO1lBQ3BCLElBQUksQ0FBQyxZQUFZLENBQUMsZ0JBQWdCLENBQUMsQ0FBQztTQUNyQztJQUNILENBQUM7SUFDRDs7O09BR0c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