1.2.5 • Published 2 months ago

inquirer-scene v1.2.5

Weekly downloads
-
License
MIT
Repository
-
Last release
2 months ago

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();
1.2.5

2 months ago

1.2.4

2 months ago

1.2.3

2 months ago

1.2.2

2 months ago

1.2.1

2 months ago

1.2.0

2 months ago

1.1.0

3 months ago

1.0.2

9 months ago

1.0.8

9 months ago

1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.10

9 months ago

1.0.14

9 months ago

1.0.12

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago