Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(inquirer): implement createPromptSession #1557

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions packages/inquirer/inquirer.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,64 @@ describe('inquirer.prompt(...)', () => {
});
});

describe('createPromptSession', () => {
it('should expose a Reactive subject across a session', async () => {
const localPrompt = inquirer.createPromptModule<TestQuestions>();
localPrompt.registerPrompt('stub', StubPrompt);
const session = localPrompt.createPromptSession();
const spy = vi.fn();

await session.run([
{
type: 'stub',
name: 'nonSubscribed',
message: 'nonSubscribedMessage',
answer: 'nonSubscribedAnswer',
},
]);

session.process.subscribe(spy);
expect(spy).not.toHaveBeenCalled();

await session.run([
{
type: 'stub',
name: 'name1',
message: 'message',
answer: 'bar',
},
{
type: 'stub',
name: 'name',
message: 'message',
answer: 'doe',
},
]);

expect(spy).toHaveBeenCalledWith({ name: 'name1', answer: 'bar' });
expect(spy).toHaveBeenCalledWith({ name: 'name', answer: 'doe' });
});

it('should return proxy object as prefilled answers', async () => {
const localPrompt = inquirer.createPromptModule<TestQuestions>();
localPrompt.registerPrompt('stub', StubPrompt);

const proxy = new Proxy({ prefilled: 'prefilled' }, {});
const session = localPrompt.createPromptSession({ answers: proxy });

const answers = await session.run([
{
type: 'stub',
name: 'nonSubscribed',
message: 'nonSubscribedMessage',
answer: 'nonSubscribedAnswer',
},
]);

expect(answers).toBe(proxy);
});
});

describe('AbortSignal support', () => {
it('throws on aborted signal', async () => {
const localPrompt = inquirer.createPromptModule<TestQuestions>({
Expand Down
11 changes: 8 additions & 3 deletions packages/inquirer/src/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ export function createPromptModule<
questions: PromptSession<A>,
answers?: Partial<A>,
): PromptReturnType<A> {
const runner = new PromptsRunner<A>(promptModule.prompts, opt);

const promptPromise = runner.run(questions, answers);
const runner = promptModule.createPromptSession<A>({ answers });
const promptPromise = runner.run(questions);
return Object.assign(promptPromise, { ui: runner });
}

Expand All @@ -124,6 +123,12 @@ export function createPromptModule<
promptModule.prompts = { ...builtInPrompts };
};

promptModule.createPromptSession = function <A extends Answers>({
answers,
}: { answers?: Partial<A> } = {}) {
return new PromptsRunner<A>(promptModule.prompts, { ...opt, answers });
};

return promptModule;
}

Expand Down
Loading