Composing Generators
Generators are useful individually, but reusing and composing generators allows you to build whole workflows out of simpler building blocks.
Using Nx Devkit Generators
Nx Devkit generators can be imported and invoked like any javascript function. They often return a Promise
, so they can be used with the await
keyword to mimic synchronous code. Because this is standard javascript, control flow logic can be adjusted with if
blocks and for
loops as usual.
1import { libraryGenerator } from '@nrwl/workspace';
2
3export default async function (tree: Tree, schema: any) {
4 await libraryGenerator(
5 tree, // virtual file system tree
6 { name: schema.name } // options for the generator
7 );
8}
Using Angular Devkit Schematics
If you need to use a generator written with the Angular devkit (also known as a schematic) inside of an Nx devkit generator, you need to wrap the schematic inside the wrapAngularDevkitSchematic
function.
Note: All Nrwl maintained schematics have been migrated over to the Nx Devkit, so this should only be necessary for third party schematics.
1import { wrapAngularDevkitSchematic } from '@nrwl/devkit/ngcli-adapter';
2
3export default async function (tree: Tree, schema: any) {
4 const libraryGenerator = wrapAngularDevkitSchematic(
5 '@nrwl/angular', // plugin name
6 'lib' // schematic name
7 );
8 await libraryGenerator(
9 tree, // virtual file system tree
10 { name: schema.name } // options for the generator
11 );
12}