Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nrwl/nx Workspace-Specific Schematics

I've been investigating the nrwl extensions and they look great. However, when I'm following their tutorial for Workspace Specific Schematics, the last step doesn't show me the command to run. Can you tell me how I run the schematic I created? I'm sure it's simple, but I can't find the command anywhere.

like image 368
Steve Avatar asked Jul 31 '18 10:07

Steve


People also ask

How do I add Angular materials to NX workspace?

Install the angular material lib: yarn add @angular/material. To see the avaible commands from the nx console: nx list @angular/material. To add angular material to the default project: nx generate @angular/material:ng-add.

Is NX workspace free?

Nx Cloud is a free platform that unlocks the full potential of your Nx Workspace.

What are NX workspaces?

Nx Workspace is a tool suite designed to architect, build and manage monorepos at any scale. It has out-of-the-box support for multiple frontend frameworks like Angular and React as well as backend technologies including Nest, Next, and Express.


1 Answers

(edit: see Stefan's answer for the correct Nx way to do it. If you want to publish your schematics, you still need to follow this answer)

The Nx generator for schematics doesn't do everything it needs to do to have your schematic work. What I have learned below is from creating a blank schematic in another directory by following this blog post by the Angular Team, which I recommend following to understand Schematics in general.

The following rough steps should help get you on your way:

  1. Generate the schematic using the Nx schematic ng g workspace-schematic
  2. Add a collection.json file in the tools/schematics directory, for example

    {
      "$schema": "../../node_modules/@angular-devkit/schematics/collection-schema.json",
      "name": "myCustomCollection",
      "version": "0.0.1",
      "extends": ["@nrwl/schematics"],
      "schematics": {
        "data-access-lib": {
          "factory": "./data-access-lib",
          "schema": "./data-access-lib/schema.json",
          "description": "Create a Data Access Library"
        }
      }
    }
    

    Note the extends property, which means your collection, if it doesn't have a particular schematic, will look to the Nx schematics to try to find it before failing

  3. Add a schematics field to your package.json that points to the relative location of your collection.json file (e.g. "schematics": "./tools/schematics/collection.json")
  4. Compile your Typescript to Javascript. I made a package.json script to run tsc -p tools/tsconfig.typescript.json (you should modify your tsconfig file to leave the compiled js in place)
  5. (Optional) Modify your angular.json to point to your collection by default. cli > defaultCollection = "."(it was @nrwl/schematics for me initially)
    • Note: Setting the default collection to "." makes it only work in the root directory. If you need to run your collection in a subfoler, you will need to run it like ng g ../..:data-access-lib
  6. Run your schematic with ng g data-access-lib (if you completed step 5) or ng g .:data-access-lib

The Nx schematic generator seems to have some major gaps, but hopefully this will help get you back on track.

like image 96
jamesthollowell Avatar answered May 12 '23 02:05

jamesthollowell