inquirer-scene
v1.2.5
Published
Library for easy management of inquirer
Downloads
6
Readme
inquirer-scene
Library for easy management of inquirer
Example
import { Inquirer } from 'inquirer-scene';
import { QuestionType } from 'inquirer-scene';
interface IState {
externalQuestion: string;
firstQuestion: string;
secondQuestion: string;
}
const registerQuestion = <
S extends Record<string, any> = Record<string, any>,
I extends string = string,
Q extends QuestionType = QuestionType
>(
inquirer: Inquirer<S, I, Q>,
id: I,
stateKey: keyof S
) => {
return inquirer.addQuestion({
id,
action: async (context, answer) => {
context.state[stateKey] = answer as S[keyof S];
},
configureQuestion: () => ({ type: 'input' } as Q),
});
};
const test = async () => {
const inq = new Inquirer<IState, keyof IState>(
{ firstQuestion: '', secondQuestion: '', externalQuestion: '' },
{ defaultBack: false }
);
registerQuestion(inq, 'externalQuestion', 'externalQuestion');
inq.addQuestion({
async action(context, answer) {
context.state.firstQuestion = answer as string;
if (answer === 'second') {
await context.getQuestion('secondQuestion');
}
},
configureQuestion: () => ({ type: 'list', message: 'Choice:', choices: ['second', 'exit'] }),
id: 'firstQuestion',
}).addQuestion({
async action(context, answer) {
context.state.secondQuestion = answer as string;
if (answer === 'external') {
await context.getQuestion('externalQuestion');
}
},
configureQuestion: () => ({ type: 'list', message: 'Choice:', choices: ['break', 'external', 'exit'] }),
id: 'secondQuestion',
parentId: 'firstQuestion',
});
await inq.start('firstQuestion');
console.log(inq.context.state);
};
test();