Skip to content

Prompts API Reference

The prompts module provides interactive command-line prompts for gathering user input.

Core Prompt Functions

text(options)

Prompts the user to enter text.

ts
import { prompt } from '@stacksjs/clapp'

const name = await prompt.text('What is your name?')

Options

OptionTypeDescriptionDefault
messagestringThe prompt messageRequired
defaultstringDefault value''
placeholderstringPlaceholder text''
initialstringInitial value (shown and used as default)''
validateFunctionValidation functionundefined
transformFunctionTransform the final valueundefined
hintstringAdditional hint text''
requiredbooleanWhether input is requiredtrue

Returns

Returns a Promise that resolves to the user's input as a string.

password(options)

Prompts the user to enter a password (with masked input).

ts
const password = await prompt.password('Enter your password:')

Options

OptionTypeDescriptionDefault
messagestringThe prompt messageRequired
maskstringCharacter to display instead of actual input'*'
validateFunctionValidation functionundefined
requiredbooleanWhether input is requiredtrue

Returns

Returns a Promise that resolves to the user's input as a string.

confirm(options)

Prompts the user to confirm with a yes/no question.

ts
const shouldProceed = await prompt.confirm('Do you want to continue?')

Options

OptionTypeDescriptionDefault
messagestringThe prompt messageRequired
defaultbooleanDefault valuetrue
activestringText for the active/yes state'Yes'
inactivestringText for the inactive/no state'No'

Returns

Returns a Promise that resolves to a boolean.

select(options)

Prompts the user to select an option from a list.

ts
const color = await prompt.select('Choose a color:', [
  'red',
  'green',
  'blue',
])

// Or with labeled options
const framework = await prompt.select('Select a framework:', [
  { value: 'react', label: 'React.js' },
  { value: 'vue', label: 'Vue.js' },
  { value: 'svelte', label: 'Svelte' },
])

Options

OptionTypeDescriptionDefault
messagestringThe prompt messageRequired
optionsArrayArray of options to select fromRequired
initialnumberIndex of the default selected option0
hintstringAdditional hint text''
maxItemsnumberMax number of items to show at onceundefined

Option Items

Each option in the options array can be:

  • A string (used as both value and label)
  • An object with properties:
    • value: The value to return when selected
    • label: The display text (optional, defaults to value)
    • hint: Additional hint text for this option (optional)

Returns

Returns a Promise that resolves to the selected option's value.

multiselect(options)

Prompts the user to select multiple options from a list.

ts
const features = await prompt.multiselect('Select features:', [
  { name: 'TypeScript', value: 'ts', checked: true },
  { name: 'ESLint', value: 'eslint' },
  { name: 'Testing', value: 'tests' },
])

Options

OptionTypeDescriptionDefault
messagestringThe prompt messageRequired
optionsArrayArray of options to select fromRequired
initialValuesArrayArray of initially selected values[]
cursorAtanyValue to place cursor at initiallyundefined
requiredbooleanWhether at least one selection is requiredtrue
hintstringAdditional hint text''
maxItemsnumberMax number of items to show at onceundefined

Option Items

Each option in the options array should be an object with properties:

  • value: The value to return when selected
  • name or label: The display text
  • checked: Whether the option is initially selected (optional)
  • hint: Additional hint text for this option (optional)

Returns

Returns a Promise that resolves to an array of selected option values.

number(options)

Prompts the user to enter a number.

ts
const age = await prompt.number('How old are you?', {
  min: 1,
  max: 120,
})

Options

OptionTypeDescriptionDefault
messagestringThe prompt messageRequired
defaultnumberDefault valueundefined
minnumberMinimum allowed valueundefined
maxnumberMaximum allowed valueundefined
validateFunctionValidation functionundefined
hintstringAdditional hint text''

Returns

Returns a Promise that resolves to a number.

groupMultiselect(options)

Prompts the user to select multiple options from grouped lists.

ts
const selections = await prompt.groupMultiselect('Select packages:', {
  Frontend: [
    { value: 'react', label: 'React' },
    { value: 'vue', label: 'Vue' },
  ],
  Backend: [
    { value: 'express', label: 'Express' },
    { value: 'fastify', label: 'Fastify' },
  ],
})

Options

OptionTypeDescriptionDefault
messagestringThe prompt messageRequired
optionsObjectObject of option groupsRequired
initialValuesArrayArray of initially selected values[]
cursorAtanyValue to place cursor at initiallyundefined
requiredbooleanWhether at least one selection is requiredtrue
selectableGroupsbooleanWhether groups can be selected as a wholetrue
groupSpacingnumberNumber of lines between groups1

Returns

Returns a Promise that resolves to an array of selected option values.

note(message, title, options)

Displays a note to the user.

ts
prompt.note('This will modify your config files.', 'Warning')

Parameters

ParameterTypeDescriptionDefault
messagestringThe note messageRequired
titlestringTitle for the note''
optionsobjectAdditional options{}

Options

OptionTypeDescriptionDefault
formatFunctionFormat function for each lineundefined

Progress Indicators

spinner(options)

Creates a spinner for indicating progress during async operations.

ts
import { spinner } from '@stacksjs/clapp'

const spin = spinner('Loading dependencies')
spin.start()

// Do some work
await someAsyncOperation()

// Update spinner text
spin.message('Finalizing installation')

// Complete the spinner
spin.stop('Dependencies loaded successfully')

Options

OptionTypeDescriptionDefault
messagestringInitial spinner message''
framesstring[]Custom animation framesDefault spinner frames
intervalnumberAnimation speed in milliseconds80
indicatorstringIndicator type ('spinner' or 'timer')'spinner'
colorstring or FunctionColor for the spinnerDefault color
successColorstring or FunctionColor for success messageGreen
errorColorstring or FunctionColor for error messageRed
cancelMessagestringMessage when cancelled'Operation cancelled'
errorMessagestringMessage when errored'Operation failed'

Returns

Returns a spinner object with the following methods:

  • start(message?): Start the spinner with an optional message
  • stop(message?, code?): Stop the spinner with an optional message and code (0: success, 1: cancel, 2+: error)
  • message(text): Update the spinner message

progress.bar(options)

Creates a progress bar for indicating progress with percentage.

ts
import { progress } from '@stacksjs/clapp'

const bar = progress.bar({
  title: 'Downloading files',
  total: 100,
})

// Update progress as work completes
for (let i = 0; i <= 100; i += 10) {
  bar.update(i)
  await sleep(200) // simulate work
}

bar.stop()

Options

OptionTypeDescriptionDefault
titlestringTitle of the progress bar''
totalnumberTotal steps (100%)Required
widthnumberWidth of the progress bar30
completestringCharacter for completed portion'█'
incompletestringCharacter for incomplete portion'░'
showPercentbooleanShow percentage numbertrue

Returns

Returns a progress bar object with the following methods:

  • update(value): Update the progress to a specific value
  • increment(delta?): Increment the progress by delta (default: 1)
  • stop(message?): Stop the progress bar with an optional message

Utility Functions

isCancel(value)

Checks if a prompt was cancelled.

ts
const name = await prompt.text('What is your name?')

if (prompt.isCancel(name)) {
  console.log('Operation cancelled')
  process.exit(0)
}

Parameters

ParameterTypeDescriptionDefault
valueanyThe value to checkRequired

Returns

Returns true if the value indicates the prompt was cancelled.

createPrompt(options)

Creates a custom prompt.

ts
import { createPrompt } from '@stacksjs/clapp'

const emailPrompt = createPrompt({
  name: 'email',
  validate: (value) => {
    if (!value.includes('@'))
      return 'Please enter a valid email address'
    return true
  },
  transform: value => value.toLowerCase().trim(),
})

const email = await emailPrompt('Enter your email:')

Options

OptionTypeDescriptionDefault
namestringName of the prompt typeRequired
initialValueanyInitial valueundefined
renderFunctionCustom render functionDefault renderer
validateFunctionValidation functionundefined
transformFunctionTransform functionundefined
keybindingsObjectCustom key handlers{}
submitFunctionSubmit handlerDefault handler
cancelFunctionCancel handlerDefault handler

Returns

Returns a function that can be called to display the custom prompt.

Advanced Usage Examples

Validation

Add custom validation to any prompt:

ts
const username = await prompt.text('Username:', {
  validate: (value) => {
    if (value.length < 3)
      return 'Username must be at least 3 characters'
    if (!/^[\w-]+$/.test(value))
      return 'Username can only contain letters, numbers, underscores, and hyphens'
    return true
  },
})

Transformation

Transform user input before returning:

ts
const tag = await prompt.text('Git tag:', {
  transform: value => `v${value}`.replace(/^vv/, 'v'),
})

Custom Prompts

Create entirely custom prompts:

ts
import { createPrompt, style } from '@stacksjs/clapp'

// Create a rating prompt (1-5 stars)
const ratingPrompt = createPrompt({
  name: 'rating',
  initialValue: 3,
  render: (state) => {
    // Custom rendering logic here
    const stars = '★'.repeat(state.value) + '☆'.repeat(5 - state.value)
    console.log(`${state.message}\n${style.yellow(stars)} (${state.value}/5)`)
  },
  keybindings: {
    // Custom key handlers
    up: (state) => {
      state.value = Math.min(5, state.value + 1)
    },
    down: (state) => {
      state.value = Math.max(1, state.value - 1)
    },
  },
})

const rating = await ratingPrompt('Rate your experience:')

Multi-step Flows

Chain prompts to create multi-step flows:

ts
async function setupProject() {
  const name = await prompt.text('Project name:')

  const type = await prompt.select('Project type:', [
    'library',
    'application',
    'plugin',
  ])

  let framework = null
  if (type === 'application') {
    framework = await prompt.select('Framework:', [
      'react',
      'vue',
      'angular',
    ])
  }

  const features = await prompt.multiselect('Features:', [
    { name: 'TypeScript', value: 'typescript', checked: true },
    { name: 'Testing', value: 'testing' },
    { name: 'Linting', value: 'linting' },
  ])

  const confirm = await prompt.confirm('Create project with these settings?')

  if (confirm) {
    const spin = spinner('Creating project')
    spin.start()

    // Project creation logic here...
    await sleep(2000)

    spin.stop('Project created successfully!')
    return { name, type, framework, features }
  }

  return null
}

const project = await setupProject()

Released under the MIT License.