Skip to content

PromptGroupScriptHelper

This file defines the PromptGroupScriptHelper class, which provides a fluent interface for building and sending groups of interactive prompts (text, confirm, select, multiselect) in scripts. It is designed to batch prompt commands and send them together for processing, typically in a CLI or script-driven context.


Features

  • Prompt Grouping: Collects multiple prompt commands before sending them as a batch.
  • Prompt Types: Supports text, confirm, select, and multiselect prompts.
  • Validation: Ensures select and multiselect prompts have non-empty options.
  • Fluent API: Methods return this for easy chaining.
  • Send & Clear: Send all queued prompts and clear the queue when needed.

Usage

typescript
const promptGroup = new PromptGroupScriptHelper(async (commands) => {
	// Implementation for sending prompt commands and receiving responses
	return await sendPromptsToUser(commands);
});

promptGroup
	.addText('username', { message: 'Enter your username:' })
	.addConfirm('acceptTerms', { message: 'Do you accept the terms?' })
	.addSelect('color', { message: 'Pick a color:', options: [{ value: 'red', label: 'Red' }] })
	.addMultiselect('features', { message: 'Select features:', options: [{ value: 'a', label: 'Feature A' }] });

const responses = await promptGroup.send();
promptGroup.clear();

API

Constructor

typescript
constructor(
	private sendRequest: <T>(commands: IScriptPromptItem[]) => Promise<T | null>,
)
  • sendRequest: A function that sends the array of prompt commands and returns a promise with the responses.

Methods

addText(key: string, options: Omit<TextOptions, 'validate'>): this

Adds a text prompt to the group.

addConfirm(key: string, options: ConfirmOptions): this

Adds a confirm (yes/no) prompt to the group.

addSelect<T = string>(key: string, options: SelectOptions<T>): this

Adds a select prompt to the group. Throws if options.options is empty.

addMultiselect<T = string[]>(key: string, options: MultiSelectOptions<T>): this

Adds a multiselect prompt to the group. Throws if options.options is empty.

send<T = Record<string, any>>(): Promise<T | null>

Sends all queued prompts using the provided sendRequest function and returns the responses.

clear(): this

Clears all queued prompt commands.


Notes

  • Designed for use in script helpers or CLI tools that need to batch and process multiple prompts at once.
  • All methods (except send and clear) return this for method chaining.
  • Throws an error if select or multiselect prompts are added with empty options.

Released under the Apache-2.0 License.