2018-07-25 14:58:18 +00:00
|
|
|
import * as plugins from './smartinteract.plugins';
|
2018-07-25 15:37:34 +00:00
|
|
|
import * as smartpromise from '@pushrocks/smartpromise';
|
2020-02-02 13:45:59 +00:00
|
|
|
import { AnswerBucket } from './smartinteract.classes.answerbucket';
|
2016-11-20 00:11:22 +00:00
|
|
|
|
2018-07-25 14:58:18 +00:00
|
|
|
/**
|
|
|
|
* the availeable question types
|
|
|
|
*/
|
|
|
|
export type questionType =
|
|
|
|
| 'input'
|
|
|
|
| 'confirm'
|
|
|
|
| 'list'
|
|
|
|
| 'rawlist'
|
|
|
|
| 'expand'
|
|
|
|
| 'checkbox'
|
|
|
|
| 'password'
|
|
|
|
| 'editor';
|
2016-11-20 00:11:22 +00:00
|
|
|
|
2018-07-25 14:58:51 +00:00
|
|
|
/**
|
2020-02-07 16:36:35 +00:00
|
|
|
* a choice
|
2018-07-25 14:58:51 +00:00
|
|
|
*/
|
2016-11-20 00:11:22 +00:00
|
|
|
export interface IChoiceObject {
|
2018-07-25 14:58:18 +00:00
|
|
|
name: string;
|
|
|
|
value: any;
|
2016-11-20 00:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface IQuestionObject {
|
2018-07-25 14:58:18 +00:00
|
|
|
name: string;
|
|
|
|
type: questionType;
|
|
|
|
message: string;
|
|
|
|
default: any;
|
|
|
|
choices?: string[] | IChoiceObject[];
|
|
|
|
validate?: IValidateFunction;
|
2016-11-20 00:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface IAnswerObject {
|
2018-07-25 14:58:18 +00:00
|
|
|
name: string;
|
|
|
|
value: any;
|
2016-11-20 00:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface IValidateFunction {
|
2018-07-25 14:58:18 +00:00
|
|
|
(anyObject: any): boolean;
|
2016-11-20 00:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* class SmartInteract - allows to specify an user interaction during runtime
|
|
|
|
*/
|
|
|
|
export class SmartInteract {
|
2020-08-31 19:23:51 +00:00
|
|
|
// STATIC
|
2020-08-31 19:25:25 +00:00
|
|
|
public static async getCliConfirmation(
|
|
|
|
questionArg: string,
|
|
|
|
defaultArg: boolean
|
|
|
|
): Promise<boolean> {
|
2020-08-31 19:23:51 +00:00
|
|
|
const smartinteractInstance = new SmartInteract();
|
|
|
|
const response = await smartinteractInstance.askQuestion({
|
|
|
|
default: defaultArg,
|
|
|
|
message: questionArg,
|
|
|
|
name: 'question',
|
2020-08-31 19:25:25 +00:00
|
|
|
type: 'confirm',
|
2020-08-31 19:23:51 +00:00
|
|
|
});
|
|
|
|
return response.value;
|
2020-08-31 19:25:25 +00:00
|
|
|
}
|
2020-08-31 19:23:51 +00:00
|
|
|
|
|
|
|
// INSTANCE
|
2017-07-28 12:27:09 +00:00
|
|
|
/**
|
|
|
|
* holds the qestion queue, that is emptied once you call
|
|
|
|
*/
|
2020-08-31 19:23:51 +00:00
|
|
|
private questionMap = new plugins.lik.ObjectMap<IQuestionObject>();
|
2017-07-28 12:27:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor of class SmartInteract
|
|
|
|
*/
|
2018-07-25 14:58:18 +00:00
|
|
|
constructor(questionArrayArg?: IQuestionObject[]) {
|
2017-07-28 12:27:09 +00:00
|
|
|
if (questionArrayArg) {
|
2018-07-25 14:58:18 +00:00
|
|
|
this.addQuestions(questionArrayArg);
|
2016-11-20 00:11:22 +00:00
|
|
|
}
|
2017-07-28 12:27:09 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* allows you to ask a single question and returns the answer in a promise
|
|
|
|
* skips the queue
|
|
|
|
*/
|
2018-07-25 14:58:18 +00:00
|
|
|
askQuestion(optionsArg: IQuestionObject): Promise<IAnswerObject> {
|
2020-02-07 16:36:35 +00:00
|
|
|
const done = smartpromise.defer<IAnswerObject>();
|
2017-07-28 12:27:09 +00:00
|
|
|
if (this.isValidEnv()) {
|
2018-07-25 14:58:18 +00:00
|
|
|
plugins.inquirer
|
|
|
|
.prompt([
|
|
|
|
{
|
|
|
|
name: optionsArg.name,
|
|
|
|
type: optionsArg.type,
|
|
|
|
message: optionsArg.message,
|
|
|
|
default: optionsArg.default,
|
|
|
|
choices: optionsArg.choices,
|
2020-08-31 19:23:51 +00:00
|
|
|
validate: optionsArg.validate,
|
|
|
|
},
|
2018-07-25 14:58:18 +00:00
|
|
|
])
|
2020-08-31 19:23:51 +00:00
|
|
|
.then((answers) => {
|
2018-07-25 14:58:18 +00:00
|
|
|
// adjust to the fact that now dots define paths for inquirer
|
2020-02-07 16:36:35 +00:00
|
|
|
const answerValue = plugins.smartparam.smartGet(answers, optionsArg.name);
|
2018-07-25 14:58:18 +00:00
|
|
|
done.resolve({
|
|
|
|
name: optionsArg.name,
|
2020-08-31 19:23:51 +00:00
|
|
|
value: answerValue,
|
2018-07-25 14:58:18 +00:00
|
|
|
});
|
2017-07-28 12:27:09 +00:00
|
|
|
})
|
2020-08-31 19:23:51 +00:00
|
|
|
.catch((err) => {
|
2018-07-25 14:58:18 +00:00
|
|
|
console.log(err);
|
|
|
|
});
|
2017-07-28 12:27:09 +00:00
|
|
|
} else {
|
2020-02-07 16:36:35 +00:00
|
|
|
const answer: IAnswerObject = {
|
2017-07-28 12:27:09 +00:00
|
|
|
name: optionsArg.name,
|
2020-08-31 19:23:51 +00:00
|
|
|
value: optionsArg.default,
|
2018-07-25 14:58:18 +00:00
|
|
|
};
|
|
|
|
done.resolve(answer);
|
2016-11-20 00:11:22 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 14:58:18 +00:00
|
|
|
return done.promise;
|
2017-07-28 12:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add questions to queue
|
|
|
|
*/
|
2018-07-25 14:58:18 +00:00
|
|
|
addQuestions(questionArrayArg: IQuestionObject[]) {
|
|
|
|
this.questionMap.addArray(questionArrayArg);
|
2017-07-28 12:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* run the question queue
|
|
|
|
*/
|
2018-07-25 14:58:18 +00:00
|
|
|
runQueue() {
|
2020-02-07 16:36:35 +00:00
|
|
|
const done = smartpromise.defer<AnswerBucket>();
|
|
|
|
const answerBucket = new AnswerBucket();
|
|
|
|
const handleQuestion = async () => {
|
2017-07-28 12:27:09 +00:00
|
|
|
if (!this.questionMap.isEmpty()) {
|
2020-02-07 16:36:35 +00:00
|
|
|
const oneQuestion = this.questionMap.getOneAndRemove();
|
|
|
|
const answer: IAnswerObject = await this.askQuestion(oneQuestion);
|
2018-07-25 14:58:18 +00:00
|
|
|
answerBucket.addAnswer(answer);
|
|
|
|
handleQuestion(); // recursion: as questions until empty
|
2017-07-28 12:27:09 +00:00
|
|
|
} else {
|
2018-07-25 14:58:18 +00:00
|
|
|
done.resolve(answerBucket); // when empty, then resolve promise
|
2017-07-28 12:27:09 +00:00
|
|
|
}
|
2018-07-25 14:58:18 +00:00
|
|
|
};
|
|
|
|
handleQuestion();
|
|
|
|
return done.promise;
|
2017-07-28 12:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* checks if the current env is valid for userinput
|
|
|
|
*/
|
2018-07-25 14:58:18 +00:00
|
|
|
private isValidEnv(): boolean {
|
2017-07-28 12:27:09 +00:00
|
|
|
if (!process.env.CI) {
|
2018-07-25 14:58:18 +00:00
|
|
|
return true;
|
2017-07-28 12:27:09 +00:00
|
|
|
} else {
|
2018-07-25 14:58:18 +00:00
|
|
|
return false;
|
2016-11-20 21:24:09 +00:00
|
|
|
}
|
2017-07-28 12:27:09 +00:00
|
|
|
}
|
2016-11-20 21:24:09 +00:00
|
|
|
}
|