Home > Enterprise >  How to make a cli typescript library that can generate whole files like NestJs?
How to make a cli typescript library that can generate whole files like NestJs?

Time:07-12

I have been using Nestjs a lot lately and have started to create my own libraries for things I work with a lot; however, I still can not figure out how to make a library that works on the CLI and create new files like using "nest g resource ....". I looked through the GitHub repository, but I still haven't found my answer. :(

CodePudding user response:

Using the generate command of the CLI you can generate a new Nest element. Under the hood it uses Angular Schematics to do so. When you generate a command, controller, service...etc. you are scaffolding it using an Angular Schematic. These schematics are provided in a separate package, also referred to as a collection. NestJS provides its schematics in the @nestjs/schematics package.

The nest generate or nest g command is basically just an alias to run an Angular Schematic. By default NestJS defaults to its own schematics. If you want to build your own you need to create your own package (collection). Then you can refer to this collection from the Nest CLI.

nest generate my-schematic --collection @my/collection

Alternatively you can just use your own schematics directly from the Angular CLI.

ng generate my-schematic:collection:my-schematic-name
  • Related