termprompt

group

Chain multiple prompts into a form-like workflow with shared state.

Preview

In progress (third prompt active)

group
Project name?
my-app
Framework?
Next.js
Init git for my-app?
Yes / No

All completed

group
Project name?
my-app
Framework?
Next.js
Init git for my-app?
Yes

Usage

import { group, select, input, confirm, log } from 'termprompt';

const project = await group({
  name: () => input({ message: 'Project name?', placeholder: 'my-app' }),
  framework: () => select({
    message: 'Framework?',
    options: [
      { value: 'next', label: 'Next.js' },
      { value: 'hono', label: 'Hono' },
    ],
  }),
  git: ({ results }) => confirm({
    message: `Init git for ${results.name}?`,
  }),
});

// project = { name: 'my-app', framework: 'next', git: true }

Each prompt function receives a results object with all previous answers. This lets you reference earlier values in later prompts.

Props

group(prompts, options?)

ParamTypeDescription
promptsGroupConfig<T>Object of prompt functions
optionsGroupOptions?Optional callbacks

GroupOptions

FieldTypeDescription
onCancel() => voidCalled when any prompt is cancelled

Cancel Behavior

If the user cancels any prompt in the group, the chain stops immediately and returns the partial results collected so far.

const result = await group(
  {
    name: () => input({ message: 'Name?' }),
    color: () => select({
      message: 'Color?',
      options: [
        { value: 'red', label: 'Red' },
        { value: 'blue', label: 'Blue' },
      ],
    }),
  },
  {
    onCancel() {
      log.warn('Setup cancelled.');
      process.exit(0);
    },
  },
);
cancelled group
Name?
my-app
Color?
cancelled
Setup cancelled.

Accessing Previous Results

The results parameter is a Partial<T>, so each value may or may not exist yet depending on where you are in the chain.

const answers = await group({
  database: () => select({
    message: 'Database?',
    options: [
      { value: 'pg', label: 'PostgreSQL' },
      { value: 'sqlite', label: 'SQLite' },
    ],
  }),
  port: ({ results }) => input({
    message: 'Port?',
    placeholder: results.database === 'pg' ? '5432' : '',
  }),
});

On this page