rhythmjs

Search documentation

Search guides, the tutorial and every package.

On this page

Provide the prompt#

createPrompt() from @rhythmjs/cli/run returns { prompt, close }, reading answer lines through Bun's async-iterable console. Startup values live on the host Rhythm app, not on the cli: create the prompt before serving, assign it to the app's context, and call close() when the command finishes. The cli declares that its handlers expect the prompt through its context generic.

TypeScript
import { Rhythm } from "@rhythmjs/rhythm";
import { RhythmCli } from "@rhythmjs/cli";
import { createPrompt, toCliHandler } from "@rhythmjs/cli/run";
import type { RhythmCliContext } from "@rhythmjs/cli/context";
import type { RhythmPrompt } from "@rhythmjs/cli/prompt";

const cli = new RhythmCli<RhythmCliContext & { prompt: RhythmPrompt }>()
  .command("init", async (ctx) => {
    const name = await ctx.prompt.text("Project name?", { default: "my-app" });
    const confirmed = await ctx.prompt.confirm("Continue?", { default: true });
    if (confirmed) ctx.response.print(`Created ${name}`);
  });

const { prompt, close } = createPrompt();

const app = new Rhythm<RhythmCliContext, { prompt: RhythmPrompt }>().use(cli.middleware());
app.context.prompt = prompt;

try {
  process.exitCode = await toCliHandler(app)(process.argv.slice(2));
} finally {
  close();
}

Choose an input type#

  • text(message, { default? }) returns trimmed text.
  • confirm(message, { default? }) accepts yes/no input and retries invalid answers.
  • select(message, choices, { allowCustom? }) asks for one numbered choice.
  • multiSelect(message, choices, { allowCustom? }) accepts comma-separated choice numbers.

Allowing custom values adds an “Other” choice and a follow-up text prompt. Selection results may therefore be arbitrary strings.

Supply custom I/O#

For custom environments or tests, import createPrompt from @rhythmjs/cli/prompt instead. This lower-level function requires an I/O object with ask(query), write(text), and optional close(); it is exactly what the Bun runner wires to console and process.stdout.