npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@topcli/prompts

v2.0.0

Published

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

Downloads

3,668

Readme

version Maintenance isc scorecard build

Requirements

Getting Started

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, multiselect } 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 });
const os = await multiselect("Choose OS", {
  choices: ["linux", "mac", "windows"],
  preSelectedChoices: ["linux"]
});

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

API

question()

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

Simple prompt, similar to rl.question() with an improved UI.

Use options.secure if you need to hide both input and answer.

Use options.signal to set an AbortSignal (throws a AbortError).

Use options.validators to handle user input.

Example

const packageName = await question('Package name', {
  validators: [
    {
      validate: (value) => {
        if (!existsSync(join(process.cwd(), value))) {
          return `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.

Use validators to handle user input.

Use autocomplete to allow filtered choices. This can be useful for a large list of choices.

Use caseSensitive to make autocomplete filters case sensitive. Default false

Use options.signal to set an AbortSignal (throws a AbortError).

multiselect()

multiselect(message: string, options: MultiselectOptions): Promise<[string]>

Scrollable multiselect depending maxVisible (default 8). Use preSelectedChoices to pre-select choices.

Use validators to handle user input.

Use showHint: false to disable hint (this option is truthy by default).

Use autocomplete to allow filtered choices. This can be useful for a large list of choices.

Use caseSensitive to make autocomplete filters case sensitive. Default false.

Use options.signal to set an AbortSignal (throws a AbortError).

confirm()

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

Boolean prompt, default to options.initial (false).

[!TIP] You can answer pressing Y or N

Use options.signal to set an AbortSignal (throws a AbortError).

PromptAgent

The PromptAgent class allows to programmatically set the next answers for any prompt function, this can be useful for testing.

const agent = PromptAgent.agent();
agent.nextAnswer("John");

const input = await question("What's your name?");
assert.equal(input, "John");

[!WARNING] Answers set with PromptAgent will bypass any logical & validation rules. Examples:

  • When using question(), validators functions will not be executed.
  • When using select(), the answer can be different from the available choices.
  • When using confirm(), the answer can be any type other than boolean.
  • etc Use with caution

Errors

AbortError

export class AbortError extends Error {
  constructor(message: string) {
    super(message);
    this.name = "AbortError";
  }
}

Interfaces

type Stdin = NodeJS.ReadStream & {
  fd: 0;
};

type Stdout = NodeJS.WriteStream & {
  fd: 1;
}

export interface AbstractPromptOptions {
  stdin?: Stdin;
  stdout?: Stdout;
  message: string;
  sginal?: AbortSignal;
}

export interface PromptValidator<T = string | string[] | boolean> {
    validate: (input: T) => boolean;
    error: (input: T) => string;
}

export interface QuestionOptions extends SharedOptions {
  defaultValue?: string;
  validators?: Validator[];
  secure?: boolean;
}

export interface Choice {
  value: any;
  label: string;
  description?: string;
}

export interface SelectOptions extends SharedOptions  {
  choices: (Choice | string)[];
  maxVisible?: number;
  ignoreValues?: (string | number | boolean)[];
  validators?: Validator[];
  autocomplete?: boolean;
  caseSensitive?: boolean;
}

export interface MultiselectOptions extends SharedOptions  {
  choices: (Choice | string)[];
  maxVisible?: number;
  preSelectedChoices?: (Choice | string)[];
  validators?: Validator[];
  autocomplete?: boolean;
  caseSensitive?: boolean;
  showHint?: boolean;
}

export interface ConfirmOptions extends SharedOptions  {
  initial?: boolean;
}

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Open an issue if you want to provide feedback such as bug reports or enchancements.

Contributors