inquirer-scene
TypeScript icon, indicating that this package has built-in type declarations

1.2.5 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i inquirer-scene

Weekly Downloads

1

Version

1.2.5

License

MIT

Unpacked Size

34.8 kB

Total Files

8

Last publish

Collaborators

  • jullic