prompts
Node.js user input library for command-line interfaces.
Requirements
- Node.js v14 or higher
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 { question, confirm, select } from "@topcli/prompts";
const kTestRunner = ["node", "tap", "tape", "vitest", "mocha", "ava"];
const name = await question("Project name ?", { defaultValue: "foo" });
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 {
defaultValue?: string;
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 π» |
Gentilhomme π |
Tony Gorez π |
Yefis π» π |