Sync Generators
In Nx 19.7, you can use sync generators to update code before a task is run or before code is sent to CI. Sync generators are designed to perform maintenance work that is required for a particular task or for CI.
Sync Generator Examples:
- Ensure code is formatted in a specific way before CI is run
- Update a custom CI script with binning strategies based on the current project graph
- Update TypeScript config files with project references based on the current project graph
Task Sync Generators
Sync generators can be associated with a particular task. Nx will ensure that the sync generator is run before the task is executed. This is similar to the dependsOn
property, but for generators instead of task dependencies.
To register a generator as a sync generator for a particular task, add the generator to the syncGenerators
property of the task configuration.
Global Sync Generators
Global sync generators are not associated with a particular task and are executed only when the nx sync
or nx sync:check
command is explicitly run. They are registered in the nx.json
file with the sync.globalGenerators
property.
Use the Project Graph in a Sync Generator
Nx processes the file system in order to create the project graph which is used run tasks in the correct order and determine project dependencies. Sync generators allow you to also go the other direction and use the project graph to update the file system.
1└─ myorg
2 ├─ apps
3 │ ├─ app1
4 │ └─ app1
5 ├─ libs
6 │ └─ lib
7 ├─ nx.json
8 └─ package.json
9
Example Usage
When Nx detects that the repository is out of sync, the user will be asked if sync generators should be run. To skip this prompt, set the sync.applyChanges
property in nx.json
to either true
or false
.
Task sync generators are executed whenever their task is run, but global sync generators need to be triggered manually with nx sync
. We recommend running nx sync
in a pre-commit or pre-push Git hook to ensure that all committed code is in sync. Also, add nx sync:check
to the beginning of your CI scripts so that CI can fail quickly if the code is out of sync.