Browse packages

Package · v0.32.0 · changelog

cli

Standard library CLI

Runtime support

  • Node
  • Deno
  • Bun
  • Browser

Browser: Imports safely, but every terminal utility throws when accessed.

This module provides foreground and background terminal colors, direct output, and common interactive prompts through one default utility aggregate.

CLI utilities throw when accessed in a browser.

Installation#

bash
npm install @rcompat/cli
bash
pnpm add @rcompat/cli
bash
yarn add @rcompat/cli
bash
bun add @rcompat/cli

Usage#

Colors#

Use cli.fg for foreground colors and text styles, and cli.bg for background colors:

js
import cli from "@rcompat/cli";

console.log(cli.fg.red("Error: something went wrong"));
console.log(cli.fg.green("Success!"));
console.log(cli.fg.bold(cli.fg.blue("Important information")));
console.log(cli.bg.yellow(cli.fg.black("Warning")));

Foreground styles include black, blue, bold, cyan, dim, gray, green, inverse, magenta, red, white, and yellow. Background colors include the same colors except for the text-only styles bold, dim, and inverse.

Output#

cli.print writes messages to standard output, separated by spaces, without adding a newline:

js
import cli from "@rcompat/cli";

cli.print("Loading", "...");
cli.print("\n");

Text and confirmation prompts#

Prompt methods live under cli.prompt. Text and confirmation prompts return a cancel symbol on end-of-input or interruption; use isCancel to distinguish it from a submitted value.

js
import cli from "@rcompat/cli";

const name = await cli.prompt.text({
  message: "What is your name?",
  initial: "Anonymous",
  validate: value => value.length < 2
    ? "Name must be at least 2 characters"
    : undefined,
});

if (cli.prompt.isCancel(name)) {
  cli.prompt.cancel("Setup cancelled");
} else {
  const proceed = await cli.prompt.confirm({
    message: `Continue as ${name}?`,
    initial: true,
  });

  if (proceed === true) cli.prompt.outro("Setup complete");
}

Selection prompts#

js
import cli from "@rcompat/cli";

const runtime = await cli.prompt.select({
  message: "Select a runtime",
  options: [
    { label: "Node", value: "node" },
    { label: "Bun", value: "bun" },
    { label: "Deno", value: "deno" },
  ],
  initial: 0,
});

const features = await cli.prompt.multiselect({
  message: "Select features",
  options: [
    { label: "TypeScript", value: "typescript" },
    { label: "Linting", value: "lint" },
    { label: "Testing", value: "test" },
  ],
  initial: [0, 1],
});

console.log(runtime, features);

Spinner#

js
import cli from "@rcompat/cli";

const spinner = cli.prompt.spinner();
spinner.start("Installing dependencies");

await new Promise(resolve => setTimeout(resolve, 1000));

spinner.stop("Dependencies installed");

The spinner animates on an interactive terminal and falls back to plain output when standard output is not a TTY.

Runtime compatibility#

Runtime Supported
Node.js Yes
Bun Yes
Deno Yes
Browser No

License#

MIT

Contributing#

See CONTRIBUTING.md in the repository root.