Tree), you can preview every change before it is written to disk.
Creating a generator
1
Add the plugin package if you don't have one yet
2
Scaffold a new generator
Generator function signature
Every generator exports a default async function that receives aTree and a typed options object:
The
Tree is a virtual file system. No changes are written to disk until the generator finishes successfully. This is what makes --dry-run possible.Defining the schema
Theschema.json file describes available options, their types, default values, and CLI prompt behavior.
Schema property extensions
Nx recognizes several extensions beyond standard JSON Schema:$default — dynamic default values
$default — dynamic default values
Maps positional CLI arguments and workspace context to schema properties.
x-prompt — interactive CLI prompts
x-prompt — interactive CLI prompts
Defines a prompt shown when the option is not provided on the command line.Use the shorthand
"x-prompt": "What name would you like?" for a simple text prompt.x-priority — field ordering in Nx Console
x-priority — field ordering in Nx Console
Controls visibility and order in the Nx Console generator form.
Fields are ordered: required > important > regular > deprecated > internal.
x-deprecated — mark options as deprecated
x-deprecated — mark options as deprecated
Deprecated options appear at the bottom of the Nx Console form with a warning.
x-dropdown — populate from workspace data
x-dropdown — populate from workspace data
Tells Nx Console to populate a dropdown with live workspace data.Currently
"projects" is the only supported value.Using generateFiles to template files
generateFiles copies a directory of EJS template files into the workspace, substituting variables along the way.
__variable__ placeholders:
Modifying existing files
Update JSON files
String replacement
Running a generator
name field from tools/my-plugin/package.json, not the folder path.
Debugging generators
To use VS Code’s debugger:- Open the Command Palette and choose Debug: Create JavaScript Debug Terminal.
- Set breakpoints in
generator.ts. - Run
nx g my-generatorin the debug terminal.
