* 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>
2.8 KiB
Customizing generator options
Adding a TypeScript schema
To create a TypeScript schema to use in your generator function, define a TypeScript file next to your schema.json named schema.ts. Inside the schema.ts, define an interface to match the properties in your schema.json file, and whether they are required.
export interface GeneratorOptions {
name: string;
type?: string;
}
Import the TypeScript schema into your generator file and replace the any in your generator function with the interface.
import { Tree, formatFiles, installPackagesTask } from '@nrwl/devkit';
import { libraryGenerator } from '@nrwl/workspace';
export default async function (tree: Tree, schema: GeneratorOptions) {
await libraryGenerator(tree, { name: `${schema.name}-${schema.type || ''}` });
await formatFiles(tree);
return () => {
installPackagesTask(tree);
};
}
Adding static options
Static options for a generator don't prompt the user for input. To add a static option, define a key in the schema.json file with the option name, and define an object with its type, description, and optional default value.
{
"$schema": "http://json-schema.org/schema",
"id": "my-generator",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Library name",
"$default": {
"$source": "argv",
"index": 0
}
},
"type": {
"type": "string",
"description": "Provide the library type, such as 'data-access' or 'state'"
}
},
"required": ["name"]
}
If you run the generator without providing a value for the type, it is not included in the generated name of the library.
Adding dynamic prompts
Dynamic options can prompt the user to select from a list of options. To define a prompt, add an x-prompt property to the option object, set the type to list, and define an items array for the choices.
{
"$schema": "http://json-schema.org/schema",
"id": "my-generator",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Library name",
"$default": {
"$source": "argv",
"index": 0
}
},
"type": {
"type": "string",
"description": "Provide the library type",
"x-prompt": {
"message": "Which type of library would you like to generate?",
"type": "list",
"items": [
{
"value": "data-access",
"label": "Data Access"
},
{
"value": "feature",
"label": "Feature"
},
{
"value": "state",
"label": "State Management"
}
]
}
}
},
"required": ["name"]
}
Running the generator without providing a value for the type will prompt the user to make a selection.