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
npm i @stone-js/node-cli-adapterimport { 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.
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)
]node app tasks:prune 14 # run it
node app prune 14 # via its alias#Command options
| Option | Type | Description |
|---|---|---|
| name* | string | The command name, e.g. "tasks:prune". |
| alias | string | string[] | Alternative name(s). |
| args | string | string[] | Declared arguments, e.g. "[days]" (optional) or "<id>" (required). |
| description | string | Shown in help output. |