@hodfords/nestjs-command
nestjs-command simplifies creating and managing CLI commands in NestJS applications. It offers an easy way to define and execute commands, streamlining CLI integration and boosting productivity with minimal configuration.
Requirements πβ
- This package is ESM-only. Your project must be able to
importit β CommonJSrequire()of it is not supported. - Node.js
>= 20.19.0(or>= 22.12,>= 24.15,>= 26). - NestJS 12.
@hodfords/nestjs-command | NestJS |
|---|---|
12.x | 12.x |
11.x | 11.x |
Installation π€β
Install the nestjs-command package with:
npm install @hodfords/nestjs-command --save
Set up in your codebase:
src/config/command.config.ts
import { CommandModule } from '@hodfords/nestjs-command';
export const commandConfig = CommandModule.register();
// export const = CommandModule.register(false) if typeorm is disabled
src/app.module.ts
import { Module } from '@nestjs/common';
import { commandConfig } from '~config/command.config';
@Module({
imports: [commandConfig],
controllers: [],
providers: []
})
export class AppModule {}
src/cli.ts
import { NestFactory } from '@nestjs/core';
import { CommandService } from '@hodfords/nestjs-command';
import { commandConfig } from '~config/command.config';
async function bootstrap() {
const app = await NestFactory.createApplicationContext(AppModule);
const commandService: CommandService = app.select(commandConfig).get(CommandService, { strict: true });
await commandService.exec();
await app.close();
}
bootstrap();
package.json
"wz-command": "wz-command"
The entry file is resolved from CLI_PATH (default ./src/cli.ts), relative to the directory you run the command from. When the entry is a TypeScript file, install ts-node (or tsx) in your application: it is registered as an ESM loader for projects with "type": "module", and through ts-node/register plus tsconfig-paths for CommonJS projects.
Usage πβ
Hereβs how you can use them. For each type of component, you can use one of the two available command formats: with npm run or directly with wz-command
Make a commandβ
npm run wz-command make-command <file-name> -- --module <module-name>
wz-command make-command <file-name> --module <module-name>
Make a controllerβ
npm run wz-command make-controller <file-name> -- --module <module-name>
wz-command make-controller <file-name> --module <module-name>
Make a dtoβ
npm run wz-command make-dto <file-name> -- --module <module-name>
wz-command make-dto <file-name> --module <module-name>
Make an e2e testβ
npm run wz-command make-e2e-test <file-name> -- --module <module-name>
wz-command make-e2e-test <file-name> --module <module-name>
Make an entityβ
npm run wz-command make-entity <file-name> -- --module <module-name>
wz-command make-entity <file-name> --module <module-name>
Make a migrationβ
Create tableβ
npm run wz-command make-migration <file-name> -- --module <module-name> --create=<entity-name>
wz-command make-migration <file-name> --module <module-name> --create=<entity-name>
Update tableβ
npm run wz-command make-migration <file-name> -- --module <module-name> --update=<entity-name>
wz-command make-migration <file-name> --module <module-name> --update=<entity-name>
Make a moduleβ
npm run wz-command make-module <module-name>
wz-command make-module <file-name>
Make a repositoryβ
npm run wz-command make-repository <file-name> -- --module <module-name>
wz-command make-repository <file-name> --module <module-name>
Make a serviceβ
npm run wz-command make-service <file-name> -- --module <module-name>
wz-command make-service <file-name> --module <module-name>
List all scheduled cron jobsβ
npm run wz-command list-cron-jobs
wz-command list-cron-jobs
Run specific cron jobsβ
npm run wz-command run-cron-jobs -- --jobs <jobName>
wz-command run-cron-jobs --jobs <jobName>
License πβ
This project is licensed under the MIT License