nestjs-console-oclif
is a NestJS module that provides a CLI with Oclif.
Getting started
Generate your NestJS application
The architecture is based on NestJS architecture. So you can generate your application with nestcli : https://docs.nestjs.com/first-steps
Install from NPM
npm install @oclif/command @oclif/config @oclif/errors @oclif/parser nestjs-console-oclif# or unig yarn yarn add @oclif/command @oclif/config @oclif/errors @oclif/parser nestjs-console-oclif
⚠️ Don't forget to specify Oclif options in package.json
:
Override NestJS bootstrap
Replace in file src/main.ts
with AppModule
as your main NestJS module:
#!/usr/bin/env node ;; bootstrap;
Options :
- nestModule : The class of the NestJS module
- singleCommand: The class of the single command
- enableShutdownHooks:
true
if you want enable shutdown hooks on the NestJS app. See NestJS lifecycle events
Import OclifModule
Import OclifModule
in your main NestJS module (in file src/app.module.ts
).
;;
Define your commands
Multi commands
You define your command in src/commands/myCommand.ts
like oclif.
But :
- Extends your command from
BaseCommand
- You don't need to implement the
run
function.
;;
Single command
Define your command in src/commands/myCommand.ts
, but specify the command in bootstrap function :
#!/usr/bin/env node ;;; bootstrap;
Register your commands entrypoints
Define the entrypoint of the command defined in MyCommand
.
⚠️ The entrypoint does not take arguments.
;;
Read Args and flags
Load your parameters from NestJS context with the decorators OclifArgs
and OclifFlags
:
;;