JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2068
  • Score
    100M100P100Q115660F
  • License ISC

Node.js user input library for command-line interfaces.

Package Exports

  • @topcli/prompts
  • @topcli/prompts/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@topcli/prompts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

prompts

version Maintenance isc build

Node.js user input library for command-line interfaces.

Requirements

Getting Started

Note This package is ESM only.

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i @topcli/prompts
# or
$ yarn add @topcli/prompts

Usage exemple

You can locally run node ./demo.js

import { prompt, confirm, select } from '@topcli/prompts';

const kTestRunner = ['node', 'tap', 'tape', 'vitest', 'mocha', 'ava'];

const name = await prompt('Project name ?');
const runner = await select('Choose a test runner', {
  choices: kTestRunner,
  maxVisible: 5
});
const isCLI = await confirm('Your project is a CLI ?', {
  initial: true
});

console.log(name, runner, isCLI);^

API

question()

question(message: string, options?: PromptOptions): Promise<string>

Simple prompt, similar to rl.question() with an improved UI. Use options.validators to handle user input.

Example

const packageName = await question('Package name', {
  validators: [
    {
      validate: (value) => !existsSync(join(process.cwd(), value)),
      error: (value) => `Folder ${value} already exists`
    }
  ]
});

This package provide some validators for common usage

  • required
import { prompt, required } from "@topcli/prompts";

const name = await prompt("What's your name ?", {
  validators: [required()]
});

select()

select(message: string, options: SelectOptions): Promise<string>

Scrollable select depending maxVisible (default 8). Use ignoreValues to skip result render & clear lines after a selected one.

confirm()

confirm(message: string, options?: ConfirmOptions): Promise<boolean>

Boolean prompt, return options.initial if user input is different from "y"/"yes"/"n"/"no", (default false).

Interfaces

export interface PromptOptions {
  validators?: {
    validate: (input: string) => boolean;
    error: (input: string) => string;
  }[];
}
export interface Choice {
  value: any;
  label: string;
  description?: string;
}
export interface SelectOptions {
  choices: (Choice | string)[];
  maxVisible?: number = 8;
  ignoreValues?: (string | number | boolean)[];
}
export interface ConfirmOptions {
  initial?: boolean = false;
}

Contributors

PierreDemailly
PierreDemailly

💻 ⚠️
Gentilhomme
Gentilhomme

👀
Tony Gorez
Tony Gorez

👀
Yefis
Yefis

💻 📖