implement AnswerBucket
This commit is contained in:
parent
638282d91c
commit
4a81f148b4
19
README.md
19
README.md
@ -24,18 +24,33 @@ We recommend the use of TypeScript for best in class intellesense
|
||||
import { SmartInteract } from 'smartinteract'
|
||||
|
||||
let myInteract = new SmartInteract([{ // note: its an array. You can specify multiple questions
|
||||
name: 'question1',
|
||||
type: 'input',
|
||||
message: 'Who are you?',
|
||||
default: 'Somebody',
|
||||
choices: { ... }, // optional, only needed if type is 'choice'
|
||||
validate: (inputString) => { return true } // implement your own validation
|
||||
}])
|
||||
SmartInteract.addQuestions([ ... ]) // add more questions
|
||||
SmartInteract.runQueue()
|
||||
.then(answersArray => {
|
||||
.then(answerBucket => { // the bucket has all the answers of the completed queue
|
||||
let answerQuestion1 = answerBucket.getAnswerFor('question1')
|
||||
// do something with the answers
|
||||
})
|
||||
|
||||
// alternatively use .askQuestion() for more direct control
|
||||
SmartInteract.askQuestion{ // note: its an array. You can specify multiple questions
|
||||
name: 'question2',
|
||||
type: 'confirm',
|
||||
message: 'Do you speak English?',
|
||||
default: true,
|
||||
validate: (inputString) => { return true } // implement your own validation
|
||||
}().then(answerObject => {
|
||||
// answerObject looks like { name: 'question2', value: true }
|
||||
})
|
||||
```
|
||||
|
||||
### Environment
|
||||
In some environments like CI there is no user input to be expected.
|
||||
smartinteract will always directyl create an answer from the default value
|
||||
|
||||
[![npm](https://push.rocks/assets/repo-header.svg)](https://push.rocks)
|
||||
|
33
dist/smartinteract.classes.smartinteract.d.ts
vendored
33
dist/smartinteract.classes.smartinteract.d.ts
vendored
@ -1,5 +1,6 @@
|
||||
/// <reference types="q" />
|
||||
import * as q from 'q';
|
||||
import { Objectmap } from 'lik';
|
||||
export declare type questionType = 'input' | 'confirm' | 'list' | 'rawlist' | 'expand' | 'checkbox' | 'password' | 'editor';
|
||||
export interface IChoiceObject {
|
||||
name: string;
|
||||
@ -10,10 +11,12 @@ export interface IQuestionObject {
|
||||
type: questionType;
|
||||
message: string;
|
||||
default: any;
|
||||
choices: string[] | IChoiceObject[];
|
||||
validate: IValidateFunction;
|
||||
choices?: string[] | IChoiceObject[];
|
||||
validate?: IValidateFunction;
|
||||
}
|
||||
export interface IAnswerObject {
|
||||
name: string;
|
||||
value: any;
|
||||
}
|
||||
export interface IValidateFunction {
|
||||
(anyObject: any): boolean;
|
||||
@ -34,7 +37,7 @@ export declare class SmartInteract {
|
||||
* allows you to ask a single question and returns the answer in a promise
|
||||
* skips the queue
|
||||
*/
|
||||
askQuestion(optionsArg: IQuestionObject): q.Promise<IAnswerObject[]>;
|
||||
askQuestion(optionsArg: IQuestionObject): q.Promise<IAnswerObject>;
|
||||
/**
|
||||
* add questions to queue
|
||||
*/
|
||||
@ -42,5 +45,27 @@ export declare class SmartInteract {
|
||||
/**
|
||||
* run the question queue
|
||||
*/
|
||||
runQueue(): q.Promise<IAnswerObject[]>;
|
||||
runQueue(): q.Promise<AnswerBucket>;
|
||||
/**
|
||||
* checks if the current env is valid for userinput
|
||||
*/
|
||||
private isValidEnv();
|
||||
}
|
||||
/**
|
||||
* class AnswerBucket holds answers
|
||||
*/
|
||||
export declare class AnswerBucket {
|
||||
answerMap: Objectmap<IAnswerObject>;
|
||||
/**
|
||||
* add an answer to the bucket
|
||||
*/
|
||||
addAnswer(answerArg: IAnswerObject): void;
|
||||
/**
|
||||
* gets an answer for a specific name
|
||||
*/
|
||||
getAnswerFor(nameArg: string): any;
|
||||
/**
|
||||
* gets all answers as array
|
||||
*/
|
||||
getAllAnswers(): IAnswerObject[];
|
||||
}
|
||||
|
82
dist/smartinteract.classes.smartinteract.js
vendored
82
dist/smartinteract.classes.smartinteract.js
vendored
@ -24,17 +24,28 @@ class SmartInteract {
|
||||
*/
|
||||
askQuestion(optionsArg) {
|
||||
let done = q.defer();
|
||||
plugins.inquirer.prompt([{
|
||||
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 = {
|
||||
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);
|
||||
});
|
||||
value: optionsArg.default
|
||||
};
|
||||
done.resolve(answer);
|
||||
}
|
||||
return done.promise;
|
||||
}
|
||||
/**
|
||||
@ -48,22 +59,63 @@ class SmartInteract {
|
||||
*/
|
||||
runQueue() {
|
||||
let done = q.defer();
|
||||
let answerMap = new lik_1.Objectmap();
|
||||
let answerBucket = new AnswerBucket();
|
||||
let handleQuestion = () => {
|
||||
let oneQuestion = this.questionMap.getOneAndRemove();
|
||||
this.askQuestion(oneQuestion).then(x => {
|
||||
answerMap.addArray(x);
|
||||
this.askQuestion(oneQuestion).then((answerArg) => {
|
||||
answerBucket.addAnswer(answerArg);
|
||||
if (!this.questionMap.isEmpty()) {
|
||||
handleQuestion(); // recursion: as questions until empty
|
||||
}
|
||||
else {
|
||||
done.resolve(answerMap.getArray()); // when empty, then resolve promise
|
||||
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRpbnRlcmFjdC5jbGFzc2VzLnNtYXJ0aW50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c0NBOERDIn0=
|
||||
/**
|
||||
* 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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRpbnRlcmFjdC5jbGFzc2VzLnNtYXJ0aW50ZXJhY3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydGludGVyYWN0LmNsYXNzZXMuc21hcnRpbnRlcmFjdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsbURBQWtEO0FBQ2xELHVCQUFzQjtBQUN0Qiw2QkFBK0I7QUEyQi9COztHQUVHO0FBQ0g7SUFPSTs7T0FFRztJQUNILFlBQVksZ0JBQW9DO1FBUmhEOztXQUVHO1FBQ0ssZ0JBQVcsR0FBRyxJQUFJLGVBQVMsRUFBbUIsQ0FBQTtRQU1sRCxFQUFFLENBQUMsQ0FBQyxnQkFBZ0IsQ0FBQyxDQUFDLENBQUM7WUFDbkIsSUFBSSxDQUFDLFlBQVksQ0FBQyxnQkFBZ0IsQ0FBQyxDQUFBO1FBQ3ZDLENBQUM7SUFDTCxDQUFDO0lBQ0Q7OztPQUdHO0lBQ0gsV0FBVyxDQUFDLFVBQTJCO1FBQ25DLElBQUksSUFBSSxHQU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UFBTSxDQUFDLEtBQUssQ0FBQTtRQUNoQixDQUFDO0lBQ0wsQ0FBQztDQUNKO0FBcEZELHNDQW9GQztBQUVEOztHQUVHO0FBQ0g7SUFBQTtRQUNJLGNBQVMsR0FBRyxJQUFJLGVBQVMsRUFBaUIsQ0FBQTtJQXlCOUMsQ0FBQztJQXZCRzs7T0FFRztJQUNILFNBQVMsQ0FBQyxTQUF3QjtRQUM5QixJQUFJLENBQUMsU0FBUyxDQUFDLEdBQUcsQ0FBQyxTQUFTLENBQUMsQ0FBQTtJQUNqQyxDQUFDO0lBRUQ7O09BRUc7SUFDSCxZQUFZLENBQUMsT0FBZTtRQUN4QixJQUFJLE1BQU0sR0FBRyxJQUFJLENBQUMsU0FBUyxDQUFDLElBQUksQ0FBQyxTQUFTO1lBQ3RDLE1BQU0sQ0FBQyxTQUFTLENBQUMsSUFBSSxLQUFLLE9BQU8sQ0FBQTtRQUNyQyxDQUFDLENBQUMsQ0FBQTtRQUNGLE1BQU0sQ0FBQyxNQUFNLENBQUMsS0FBSyxDQUFBO0lBQ3ZCLENBQUM7SUFFRDs7T0FFRztJQUNILGFBQWE7UUFDVCxNQUFNLENBQUMsSUFBSSxDQUFDLFNBQVMsQ0FBQyxRQUFRLEVBQUUsQ0FBQTtJQUNwQyxDQUFDO0NBQ0o7QUExQkQsb0NBMEJDIn0=
|
25
test/test.js
25
test/test.js
@ -9,6 +9,29 @@ describe('smartinteract', function () {
|
||||
should(testInteract).be.instanceOf(smartinteract.SmartInteract);
|
||||
});
|
||||
it('should add question to SmartInteract instance', function () {
|
||||
testInteract.addQuestions([{
|
||||
name: 'testQuestion1',
|
||||
type: 'input',
|
||||
message: 'what is your favourite color? Answer is blue',
|
||||
default: 'blue'
|
||||
}]);
|
||||
testInteract.addQuestions([{
|
||||
name: 'testQuestion2',
|
||||
type: 'input',
|
||||
message: 'what is your second favourite color? Answer is red',
|
||||
default: 'red'
|
||||
}]);
|
||||
});
|
||||
it('should use default value when not in CI', function (done) {
|
||||
this.timeout(30000);
|
||||
testInteract.runQueue().then(answerBucket => {
|
||||
should(answerBucket.getAnswerFor('testQuestion1')).equal('blue');
|
||||
should(answerBucket.getAnswerFor('testQuestion2')).equal('red');
|
||||
done();
|
||||
}).catch(err => {
|
||||
console.log(err);
|
||||
throw err;
|
||||
});
|
||||
});
|
||||
});
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUNyQixpQ0FBZ0M7QUFFaEMsK0NBQThDO0FBRTlDLFFBQVEsQ0FBQyxlQUFlLEVBQUU7SUFDdEIsSUFBSSxZQUFZLENBQUE7SUFDaEIsRUFBRSxDQUFDLG9DQUFvQyxFQUFFO1FBQ3JDLFlBQVksR0FBRyxJQUFJLGFBQWEsQ0FBQyxhQUFhLEVBQUUsQ0FBQTtRQUNoRCxNQUFNLENBQUMsWUFBWSxDQUFDLENBQUMsRUFBRSxDQUFDLFVBQVUsQ0FBQyxhQUFhLENBQUMsYUFBYSxDQUFDLENBQUE7SUFDbkUsQ0FBQyxDQUFDLENBQUE7SUFDRixFQUFFLENBQUMsK0NBQStDLEVBQUU7SUFFcEQsQ0FBQyxDQUFDLENBQUE7QUFDTixDQUFDLENBQUMsQ0FBQSJ9
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUNyQixpQ0FBZ0M7QUFFaEMsK0NBQThDO0FBRTlDLFFBQVEsQ0FBQyxlQUFlLEVBQUU7SUFDdEIsSUFBSSxZQUF5QyxDQUFBO0lBQzdDLEVBQUUsQ0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WUFDL0QsSUFBSSxFQUFFLENBQUE7UUFDVixDQUFDLENBQUMsQ0FBQyxLQUFLLENBQUMsR0FBRztZQUNSLE9BQU8sQ0FBQyxHQUFHLENBQUMsR0FBRyxDQUFDLENBQUE7WUFDaEIsTUFBTSxHQUFHLENBQUE7UUFDYixDQUFDLENBQUMsQ0FBQTtJQUNOLENBQUMsQ0FBQyxDQUFBO0FBQ04sQ0FBQyxDQUFDLENBQUEifQ==
|
27
test/test.ts
27
test/test.ts
@ -4,13 +4,34 @@ import * as should from 'should'
|
||||
import * as smartinteract from '../dist/index'
|
||||
|
||||
describe('smartinteract', function(){
|
||||
let testInteract
|
||||
let testInteract: smartinteract.SmartInteract
|
||||
it('should create a valid new instance', function(){
|
||||
testInteract = new smartinteract.SmartInteract()
|
||||
should(testInteract).be.instanceOf(smartinteract.SmartInteract)
|
||||
})
|
||||
it('should add question to SmartInteract instance', function() {
|
||||
|
||||
testInteract.addQuestions([{
|
||||
name: 'testQuestion1',
|
||||
type: 'input',
|
||||
message: 'what is your favourite color? Answer is blue',
|
||||
default: 'blue'
|
||||
}])
|
||||
testInteract.addQuestions([{
|
||||
name: 'testQuestion2',
|
||||
type: 'input',
|
||||
message: 'what is your second favourite color? Answer is red',
|
||||
default: 'red'
|
||||
}])
|
||||
})
|
||||
it('should use default value when not in CI', function(done){
|
||||
this.timeout(30000)
|
||||
testInteract.runQueue().then(answerBucket => {
|
||||
should(answerBucket.getAnswerFor('testQuestion1')).equal('blue')
|
||||
should(answerBucket.getAnswerFor('testQuestion2')).equal('red')
|
||||
done()
|
||||
}).catch(err => {
|
||||
console.log(err)
|
||||
throw err
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -14,12 +14,13 @@ export interface IQuestionObject {
|
||||
type: questionType
|
||||
message: string
|
||||
default: any
|
||||
choices: string[] | IChoiceObject[]
|
||||
validate: IValidateFunction
|
||||
choices?: string[] | IChoiceObject[]
|
||||
validate?: IValidateFunction
|
||||
}
|
||||
|
||||
export interface IAnswerObject {
|
||||
|
||||
name: string,
|
||||
value: any
|
||||
}
|
||||
|
||||
export interface IValidateFunction {
|
||||
@ -48,19 +49,30 @@ export class SmartInteract {
|
||||
* allows you to ask a single question and returns the answer in a promise
|
||||
* skips the queue
|
||||
*/
|
||||
askQuestion(optionsArg: IQuestionObject): q.Promise<IAnswerObject[]> {
|
||||
let done = q.defer<IAnswerObject[]>()
|
||||
plugins.inquirer.prompt([{
|
||||
name: optionsArg.name,
|
||||
type: optionsArg.type,
|
||||
message: optionsArg.message,
|
||||
default: optionsArg.default,
|
||||
choices: optionsArg.choices,
|
||||
validate: optionsArg.validate
|
||||
}]).then((answers: IAnswerObject[]) => {
|
||||
console.log(answers)
|
||||
done.resolve(answers)
|
||||
})
|
||||
askQuestion(optionsArg: IQuestionObject): q.Promise<IAnswerObject> {
|
||||
let done = q.defer<IAnswerObject>()
|
||||
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: IAnswerObject[]) => {
|
||||
done.resolve({
|
||||
name: optionsArg.name,
|
||||
value: answers[optionsArg.name]
|
||||
})
|
||||
})
|
||||
} else {
|
||||
let answer: IAnswerObject = {
|
||||
name: optionsArg.name,
|
||||
value: optionsArg.default
|
||||
}
|
||||
done.resolve(answer)
|
||||
}
|
||||
|
||||
return done.promise
|
||||
}
|
||||
|
||||
@ -75,20 +87,62 @@ export class SmartInteract {
|
||||
* run the question queue
|
||||
*/
|
||||
runQueue() {
|
||||
let done = q.defer<IAnswerObject[]>()
|
||||
let answerMap = new Objectmap<IAnswerObject>()
|
||||
let done = q.defer<AnswerBucket>()
|
||||
let answerBucket = new AnswerBucket()
|
||||
let handleQuestion = () => {
|
||||
let oneQuestion = this.questionMap.getOneAndRemove()
|
||||
this.askQuestion(oneQuestion).then(x => {
|
||||
answerMap.addArray(x)
|
||||
this.askQuestion(oneQuestion).then((answerArg: IAnswerObject) => {
|
||||
answerBucket.addAnswer(answerArg)
|
||||
if (!this.questionMap.isEmpty()) {
|
||||
handleQuestion() // recursion: as questions until empty
|
||||
} else {
|
||||
done.resolve(answerMap.getArray()) // when empty, then resolve promise
|
||||
done.resolve(answerBucket) // when empty, then resolve promise
|
||||
}
|
||||
})
|
||||
}
|
||||
handleQuestion()
|
||||
return done.promise
|
||||
}
|
||||
|
||||
/**
|
||||
* checks if the current env is valid for userinput
|
||||
*/
|
||||
private isValidEnv(): boolean {
|
||||
if (!process.env.CI) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* class AnswerBucket holds answers
|
||||
*/
|
||||
export class AnswerBucket {
|
||||
answerMap = new Objectmap<IAnswerObject>()
|
||||
|
||||
/**
|
||||
* add an answer to the bucket
|
||||
*/
|
||||
addAnswer(answerArg: IAnswerObject) {
|
||||
this.answerMap.add(answerArg)
|
||||
}
|
||||
|
||||
/**
|
||||
* gets an answer for a specific name
|
||||
*/
|
||||
getAnswerFor(nameArg: string) {
|
||||
let answer = this.answerMap.find(answerArg => {
|
||||
return answerArg.name === nameArg
|
||||
})
|
||||
return answer.value
|
||||
}
|
||||
|
||||
/**
|
||||
* gets all answers as array
|
||||
*/
|
||||
getAllAnswers() {
|
||||
return this.answerMap.getArray()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user