Nest Commander¶
Expanding on the standalone application docs there's also the nest-commander package for writing command line applications in a structure similar to your typical Nest application.
Info
nest-commander
is a third party package and is not managed by the entirety of the NestJS core team. Please, report any issues found with the library in the appropriate repository
Installation¶
Just like any other package, you've got to install it before you can use it.
A Command file¶
nest-commander
makes it easy to write new command-line applications with decorators via the @Command()
decorator for classes and the @Option()
decorator for methods of that class. Every command file should implement the CommandRunner
interface and should be decorated with a @Command()
decorator.
Every command is seen as an @Injectable()
by Nest, so your normal Dependency Injection still works as you would expect it to. The only thing to take note of is the interface CommandRunner
, which should be implemented by each command. The CommandRunner
interface ensures that all commands have a run
method that returns a Promise<void>
and takes in the parameters string[], Record<string, any>
. The run
command is where you can kick all of your logic off from, it will take in whatever parameters did not match option flags and pass them in as an array, just in case you are really meaning to work with multiple parameters. As for the options, the Record<string, any>
, the names of these properties match the name
property given to the @Option()
decorators, while their value matches the return of the option handler. If you'd like better type safety, you are welcome to create an interface for your options as well.
Running the Command¶
Similar to how in a NestJS application we can use the NestFactory
to create a server for us, and run it using listen
, the nest-commander
package exposes a simple to use API to run your server. Import the CommandFactory
and use the static
method run
and pass in the root module of your application. This would probably look like below
By default, Nest's logger is disabled when using the CommandFactory
. It's possible to provide it though, as the second argument to the run
function. You can either provide a custom NestJS logger, or an array of log levels you want to keep - it might be useful to at least provide ['error']
here, if you only want to print out Nest's error logs.
And that's it. Under the hood, CommandFactory
will worry about calling NestFactory
for you and calling app.close()
when necessary, so you shouldn't need to worry about memory leaks there. If you need to add in some error handling, there's always try/catch
wrapping the run
command, or you can chain on some .catch()
method to the bootstrap()
call.
Testing¶
So what's the use of writing a super awesome command line script if you can't test it super easily, right? Fortunately, nest-commander
has some utilities you can make use of that fits in perfectly with the NestJS ecosystem, it'll feel right at home to any Nestlings out there. Instead of using the CommandFactory
for building the command in test mode, you can use CommandTestFactory
and pass in your metadata, very similarly to how Test.createTestingModule
from @nestjs/testing
works. In fact, it uses this package under the hood. You're also still able to chain on the overrideProvider
methods before calling compile()
so you can swap out DI pieces right in the test.
Putting it all together¶
The following class would equate to having a CLI command that can take in the subcommand basic
or be called directly, with -n
, -s
, and -b
(along with their long flags) all being supported and with custom parsers for each option. The --help
flag is also supported, as is customary with commander.
Make sure the command class is added to a module
And now to be able to run the CLI in your main.ts you can do the following
And just like that, you've got a command line application.
More Information¶
Visit the nest-commander docs site for more information, examples, and API documentation.