- Understand what workspace generators are
- How to create them
- How to invoke them
- How to use one to simplify usages of other, more powerful generators
We just learned how important tags are. But we don't want to constantly and manually
have to maintain them. In this workshop, we'll create a custom workspace generator
called util-lib that knows about the folders in our workspace and automatically tags the new project
with a correct scope and type tag.
-
Use the
@nrwl/workspace:workspace-generatorgenerator to generate a new workspace generator calledutil-lib
-
Inspect the files that got generated and then commit everything (you'll see in a bit why).
-
Try to run your generator (you can append
--dry-runto avoid reverting using Git)🐳 Hint
Inspect the Workspace generators docs for details on how to run it.
⚠️ It invokes the@nrwl/workspacelibrary generator by default. So it should generate some files when you run it. You can use Git to undo those changes (hence why it's recommended to commit before running a generator).
-
In
tools/generators/util-lib/index.tstry to make itconsole.log()the value of the--nameproperty you passed to it (can use--dry-runagain to test it)
-
Now that we're more familiar with how command line options are passed to the generator, let's revert all locally generated files, as we're about to start making actually useful changes to the generator.
-
The generator should prefix any name you give to your lib with
util-For example:
nx workspace-generator util-lib dates- Should generate a lib with the name
util-dates
⚠️ You can keep trying out your changes safely with the--dry-runflag.️
-
Add a new property to its schema called
directory. It should have only 3 possible values:"store", "api", "shared". If you do not pass--directoryas an option when invoking the schema it should prompt the user to select from the 3 different values (similar to when you got asked about which CSS framework to use when creating Angular libs).🐳 Hint
-
The generator should generate the lib in the directory you pass to it.
-
Because it's a
utillib, it should automatically be generated with thetype:utiltags.🐳 Hint
Consult the
@nrwl/workspace:libdocs for possible options you can pass to it.
-
We also need to add
scopetag to it. We can use thedirectoryvalue for this, since it signifies our scope.
-
Before testing your changes, remember to commit them, in case you need to revert locally generated files again.
-
Invoke your generator and test if the above requirements work
- Let's give it the name
notifications - Select
apias the directory
- Let's give it the name
-
Let's add some functionality to the lib you just created:
-
In
libs/api/util-notifications/src/lib/api-util-notifications.ts -
Add:
export function sendNotification(clientId: string) { console.log("sending notification to client: ", clientId); }
-
-
Now try to import the above function in
apps/api/src/app/app.service.ts- Try to lint all the apps
- It should work because everything is in the
apiscope
-
Try to import it in
apps/store/src/app/app.component.ts- It should fail because it's not within the same scope
- It should fail because it's not within the same scope
-
In
libs/api/util-notifications/src/lib/api-util-notifications.ts- Try to import a
featurelib - It should correctly fail because the type hierarchy is not respected
- Try to import a
-
BONUS - Currently, the schema passed to the generator is of type
any-tools/generators/util-lib/index.tsTry to create an interface for it that respects the generator's schema.
-
BONUS BONUS - try to create another similar generator, but for "feature" libs.
-
Commit everything before the next lab.
🎓If you get stuck, check out the solution