smartinteract/ts/smartinteract.classes.smartinteract.ts

154 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-07-13 12:07:12 +00:00
import * as plugins from './smartinteract.plugins.js';
import { AnswerBucket } from './smartinteract.classes.answerbucket.js';
2016-11-20 00:11:22 +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 {
name: string;
value: any;
2016-11-20 00:11:22 +00:00
}
export interface IQuestionObject {
name: string;
type: questionType;
message: string;
default: any;
choices?: string[] | IChoiceObject[];
validate?: IValidateFunction;
2016-11-20 00:11:22 +00:00
}
export interface IAnswerObject {
name: string;
value: any;
2016-11-20 00:11:22 +00:00
}
export interface IValidateFunction {
(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
/**
* 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>();
/**
* constructor of class SmartInteract
*/
constructor(questionArrayArg?: IQuestionObject[]) {
if (questionArrayArg) {
this.addQuestions(questionArrayArg);
2016-11-20 00:11:22 +00:00
}
}
/**
* allows you to ask a single question and returns the answer in a promise
* skips the queue
*/
askQuestion(optionsArg: IQuestionObject): Promise<IAnswerObject> {
2023-07-13 12:07:12 +00:00
const done = plugins.smartpromise.defer<IAnswerObject>();
if (this.isValidEnv()) {
2023-07-13 12:07:12 +00:00
plugins.inquirer.default
.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,
},
])
2020-08-31 19:23:51 +00:00
.then((answers) => {
// adjust to the fact that now dots define paths for inquirer
2023-07-13 12:07:12 +00:00
const answerValue = plugins.smartobject.smartGet(answers, optionsArg.name);
done.resolve({
name: optionsArg.name,
2020-08-31 19:23:51 +00:00
value: answerValue,
});
})
2020-08-31 19:23:51 +00:00
.catch((err) => {
console.log(err);
});
} else {
2020-02-07 16:36:35 +00:00
const answer: IAnswerObject = {
name: optionsArg.name,
2020-08-31 19:23:51 +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: IQuestionObject[]) {
this.questionMap.addArray(questionArrayArg);
}
/**
* run the question queue
*/
runQueue() {
2023-07-13 12:07:12 +00:00
const done = plugins.smartpromise.defer<AnswerBucket>();
2020-02-07 16:36:35 +00:00
const answerBucket = new AnswerBucket();
const handleQuestion = async () => {
if (!this.questionMap.isEmpty()) {
2020-02-07 16:36:35 +00:00
const oneQuestion = this.questionMap.getOneAndRemove();
const answer: IAnswerObject = await this.askQuestion(oneQuestion);
answerBucket.addAnswer(answer);
handleQuestion(); // recursion: as questions until empty
} else {
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;
2016-11-20 21:24:09 +00:00
}
}
2016-11-20 21:24:09 +00:00
}