function define
define(
commands: Record<string, ReplCommand>
): void

Registers commands on the server under their own names and every alias.

Three things happen here rather than in all thirty-seven commands, because all thirty-seven wanted them and any that forgot one was a bug:

  • clearBufferedCommand() first. node:repl needs it before a command's output, and without it the output landed in a half-drawn line.
  • The prompt afterwards. node:repl redraws after a SYNCHRONOUS command and nothing else, so every async one had to remember to draw its own — which is why they all ended in the same line, and why forgetting it left a terminal with no prompt.
  • A catch around the lot. node:repl cannot await an action, so the promise is floated — but caught. A command that rejects has failed, which is a line of output; unhandled, it is Node killing the session over one bad .doc.
import { define } from './command.ts';

import type { ReplContext } from './command.ts';

// Defined, not invoked: it writes to a live prompt.
function example(repl: ReplContext) {
  define(repl, { pwd: { description: 'Where you are', main: (it) => it.log(it.cwd) } });
}

Parameters

commands: Record<string, ReplCommand>

Return Type

void

Usage

import { define } from "lib/commands/repl/command.ts";