angular-schematics

Angular Schematics confusion


Not sure to fully understand a concept with Angular Schematics. More precisly how to execute a schematic. Reading from some articles, I saw that we have to use ng g my-schematics-collection:simple-schematic --name="some name". In other articles I saw schematics my-schematics-collection:simple-schematic --name="Angular Community". And again using NX, they simple use ng g component, exactly like the base Angular Schematics to create a component. How can they overwrite the existing Angular Schematics? The fact that they doesn't point to their own schematics collection confuses me.

Can someone explain what to use for what use cases? Thanks!


Solution

  • Angular schematics is just a library. There are different tools that use the library. You can read more about what is expected from the tooling in the docs.

    When using ng g my-schematics-collection:simple-schematic --name="some name" you are using the tooling provided by angular-cli.

    When using schematics my-schematics-collection:simple-schematic --name="Angular Community" you are using the reference cli tooling.

    Both tools leverage nodejs module resolution to find schematic files. So my-schematics-collection is actually a name of a node_module. For example the default angular schematics are defined in this module. Notice that in the package.json there's a key called schematics. It will point to a json configuration file.

    So in general if you are using Angular, stick with the angular-cli tooling. If you are developing custom schematics, the reference CLI is useful for testing. If you don't mind writing some custom tooling, then you can just import the library directly.