nx/docs/angular/generators/composing-generators.md
Isaac Mann b3aa5c3d25
docs(core): generator guides (#6070)
* docs(nxdev): generator guides

* docs(nxdev): cleanup

* docs(nxdev): more cleanup

* docs(nxdev): formatting

* docs(nxdev): code formatting

* docs(nxdev): only mention angular devkit in angular docs

* docs(nxdev): tidy

Co-authored-by: Isaac Mann <isaacplmann+git@gmail.com>
2021-06-22 15:09:54 -04:00

40 lines
1.5 KiB
Markdown

# 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.
```typescript
import { libraryGenerator } from '@nrwl/workspace';
export default async function (tree: Tree, schema: any) {
await libraryGenerator(
tree, // virtual file system tree
{ name: schema.name } // options for the generator
);
}
```
## 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.
```typescript
import { wrapAngularDevkitSchematic } from '@nrwl/devkit/ngcli-adapter';
export default async function (tree: Tree, schema: any) {
const libraryGenerator = wrapAngularDevkitSchematic(
'@nrwl/angular', // plugin name
'lib' // schematic name
);
await libraryGenerator(
tree, // virtual file system tree
{ name: schema.name } // options for the generator
);
}
```