Skip to content

@plinthjs/console ​

The Plinth console layer. A command base whose signature is parsed into typed arguments and options, an injectable output writer, and a kernel that registers, resolves, and dispatches commands.

All output flows through an injected sink, so tests capture lines with a BufferedOutput and nothing touches process or the real console by surprise.

Defining a command ​

ts
import { Command } from '@plinthjs/console'

class MailSendCommand extends Command {
  static signature = 'mail:send {user} {--queue} {--times=1}'
  static description = 'Send a mail to the given user'

  handle(): number {
    const user = this.argument('user')
    const times = Number(this.option('times'))
    for (let i = 0; i < times; i++) {
      this.info(`Sending mail to ${user}${this.option('queue') ? ' (queued)' : ''}`)
    }
    return 0 // exit code; returning void is treated as 0
  }
}

Signature tokens ​

TokenMeaning
{user}required positional argument
{user?}optional positional argument
{user=guest}optional positional argument with a default
{names*}variadic argument collecting the remaining tokens
{--queue}boolean flag
{--id=}option that takes a value (no default)
{--times=1}option that takes a value with a default

Running commands ​

ts
import { Kernel, BufferedOutput } from '@plinthjs/console'

const kernel = new Kernel()
kernel.register(MailSendCommand)

// Dispatch by name with explicit argv.
const output = new BufferedOutput()
const exitCode = await kernel.call('mail:send', ['taylor', '--queue', '--times=2'], output)
console.log(output.lines)

// Or drive from a full argv list - the first token is the command name.
await kernel.run(['mail:send', 'taylor'], output)

// With no tokens, the built-in `list` command prints the registry.
await kernel.run([], output)

Deterministic tests ​

BufferedOutput captures every written line into lines[], so command output is asserted in memory without printing:

ts
const output = new BufferedOutput()
await kernel.call('mail:send', ['taylor'], output)
expect(output.lines).toContain('Sending mail to taylor')

Released under the MIT License.