Skip to content

PromptScriptHelper

This file defines the PromptScriptHelper class, which provides methods for sending interactive prompts (text, confirm, select, multiselect) to users in a script-driven context. It communicates with the user via a socket and supports both single and grouped prompts.


Features

  • Prompt Types: Supports text, confirm, select, and multiselect prompts.
  • Validation: Ensures select and multiselect prompts have non-empty options.
  • Prompt Grouping: Allows batching multiple prompts using PromptGroupScriptHelper.
  • Async Responses: Handles asynchronous user responses via a socket and unique request IDs.

Usage

typescript
const prompt = new PromptScriptHelper(sendFunction, socket);

// Single prompt usage
const name = await prompt.text({ message: 'Enter your name:' });
const confirmed = await prompt.confirm({ message: 'Are you sure?' });
const color = await prompt.select({ message: 'Pick a color:', options: [{ value: 'red', label: 'Red' }] });
const features = await prompt.multiselect({ message: 'Select features:', options: [{ value: 'a', label: 'Feature A' }] });

// Grouped prompts
const group = prompt.createGroup();
group
	.addText('username', { message: 'Enter your username:' })
	.addConfirm('acceptTerms', { message: 'Do you accept the terms?' });
const responses = await group.send();

API

Constructor

typescript
constructor(
	private send: (command: string, context?: Record<string, any>) => void,
	private socket: Socket<IScriptSocketData>,
)
  • send: Function to send commands and context.
  • socket: Bun socket for handling prompt responses.

Methods

async text(options: Omit<TextOptions, 'validate'>): Promise<string | null>

Sends a text prompt and returns the user's input or null.

async confirm(options: ConfirmOptions): Promise<boolean | null>

Sends a confirm prompt and returns the user's response or null.

async select<T = string>(options: SelectOptions<T>): Promise<T | null>

Sends a select prompt and returns the selected value or null. Throws if options are empty.

async multiselect<T = string[]>(options: MultiSelectOptions<T>): Promise<T | null>

Sends a multiselect prompt and returns the selected values or null. Throws if options are empty.

createGroup(): PromptGroupScriptHelper

Creates a new PromptGroupScriptHelper for batching multiple prompts.


Private Methods

private async sendRequest<T>(commands: IScriptPromptItem[]): Promise<T | null>

Sends an array of prompt commands, waits for the user's response, and resolves the result using a unique request ID.


Notes

  • Designed for use in script helpers or CLI tools that need to interactively prompt users.
  • All prompt methods are asynchronous and return the user's response or null if cancelled or invalid.
  • The createGroup method enables batching multiple prompts for a single interaction round-trip.

Released under the Apache-2.0 License.