Stone.jsDocs
Paradigm

Adapters

Node CLI

@stone-js/node-cli-adapter lets the same domain answer command-line invocations. A command is another cause: the adapter normalises argv into an intention, and your handler runs exactly as it would for an HTTP request.

#Install & enable

terminalbash
npm i @stone-js/node-cli-adapter
app/Application.tsdeclarativeimperative
import { NodeConsole } from '@stone-js/node-cli-adapter'

@NodeConsole()
@StoneApp({ name: 'tasks' })
export class Application {}
import { nodeConsoleAdapterBlueprint } from '@stone-js/node-cli-adapter'

export const App = defineStoneApp({ name: 'tasks' }, [nodeConsoleAdapterBlueprint])

#Defining a command

Mark a class with @Command (or register a defineCommand). It reads arguments off the event with event.get(), resolves services from the container, and returns output.

app/commands/Prune.tsdeclarativeimperative
import { NodeConsole, Command } from '@stone-js/node-cli-adapter'

@Command({
  name: 'tasks:prune',
  alias: 'prune',
  args: ['[days]'],
  description: 'Delete tasks done more than [days] ago (default 30).'
})
export class PruneCommand {
  constructor ({ tasks }: { tasks: TaskService }) { this.tasks = tasks }

  handle (event: NodeCliEvent) {
    const days = Number(event.get('days', 30))
    const removed = this.tasks.prune(days)
    return `Pruned ${removed} tasks`
  }
}
import { defineCommand } from '@stone-js/node-cli-adapter'

const PruneCommand = ({ tasks }) => ({
  handle: (event) => `Pruned ${tasks.prune(Number(event.get('days', 30)))} tasks`
})

export const commands = [
  defineCommand(PruneCommand, { name: 'tasks:prune', args: ['[days]'] }, true)
]
terminalbash
node app tasks:prune 14     # run it
node app prune 14           # via its alias

#Command options

OptionTypeDescription
name*stringThe command name, e.g. "tasks:prune".
aliasstring | string[]Alternative name(s).
argsstring | string[]Declared arguments, e.g. "[days]" (optional) or "<id>" (required).
descriptionstringShown in help output.

Stone.js

Your app exists in every runtime. Until you run it.

An open-source project by Stone Foundation
Created by Mr. Stone (Evens Pierre)