nx/docs/generated/packages/nx-plugin.json
2022-06-30 15:30:38 -04:00

397 lines
25 KiB
JSON

{
"githubRoot": "https://github.com/nrwl/nx/blob/master",
"name": "nx-plugin",
"description": "Plugin for creating plugins for Nx :)",
"root": "/packages/nx-plugin",
"source": "/packages/nx-plugin/src",
"documentation": [
{
"id": "overview",
"name": "Overview",
"path": "/packages/nx-plugin",
"file": "shared/nx-plugin",
"content": "Nx plugins are npm packages that contain generators and executors to extend a Nx workspace. Generators are blueprints to create new files from templates, and executors run those files. These plugins also update the `nx.json` when generating new libs or apps.\n\n> A list of plugins that is maintained by Nrwl is found in the [Nrwl/nx repo](https://github.com/nrwl/nx/tree/master/packages). \\\n> A list of custom plugins created by the community is found in the [Community](/community) section.\n> Plugins are written using Nx Devkit. **Read [Nx Devkit](/devkit/index) for more information.**\n\n{% youtube\nsrc=\"https://www.youtube.com/embed/fC1-4fAZDP4\"\ntitle=\"Nx Tutorial: Building Custom Plugins for Nx\"\nwidth=\"100%\" /%}\n\n## Generating a Plugin\n\nTo get started with building a Nx Plugin, run the following command:\n\n```bash\nnpx create-nx-plugin my-org --pluginName my-plugin\n```\n\nThis command creates a brand-new workspace, and sets up a pre-configured plugin with the specified name.\n\n> Note, the command above will create a plugin the package name set to `@my-org/my-plugin`. You can pass `--importPath` to provide a different package name.\n\n> If you do not want to create a new workspace, install the `@nrwl/nx-plugin` dependency in an already existing workspace with npm or yarn. Then run `nx g @nrwl/nx-plugin:plugin [pluginName]`.\n\nA new plugin is created with a default generator, executor, and e2e app.\n\n## Generator\n\nThe created generator contains boilerplate that will do the following:\n\n- Normalize a schema (the options that the generator accepts)\n- Update the `workspace.json`\n- Add the plugin's project to the `nx.json` file\n- Add files to the disk using templates\n\nThere will be an exported default function that will be the main entry for the generator.\n\n### Generator options\n\nThe `schema.d.ts` file contains all the options that the generator supports. By default, it includes `directory`, `tags`, and `name` as the options. If more options need to be added, please update this file and the `schema.json` file.\n\n> Note: The `schema.d.ts` file is used for type checking inside the implementation file. It should match the properties in `schema.json`.\n\n### Adding more generators\n\nTo add more generators to the plugin, run the following command:\n`nx generate @nrwl/nx-plugin:generator [generatorName] --project=[pluginName]`.\n\nThis will scaffold out a new generator and update the necessary files to support it.\n\n### Generator Testing\n\nThe generator spec file includes boilerplate to help get started with testing. This includes setting up an empty workspace.\n\nThese tests should ensure that files within the tree (created with `createTreeWithEmptyWorkspace`) are in the correct place, and contain the right content.\n\nFull E2Es are supported (and recommended) and will run everything on the file system like a user would.\n\n## Executor\n\nThe default executor is set up to just emit a console log. Some examples of what an executor can do are:\n\n- Support different languages, (Java, Go, Python, C#)\n- Compile new UI framework components\n- Deploy an app on a CDN\n- Publish to NPM\n- and many more!\n\n### Adding more executors\n\nTo add more executors to the plugin, run the following command:\n`nx generate @nrwl/nx-plugin:executor [executor] --project=[pluginName]`.\n\nThis will scaffold out a new generator and update the necessary files to support it.\n\n### Executor testing\n\nThe executor spec file contains boilerplate to run the default exported function from the executor.\n\nThese tests should make sure that the executor is executing and calling the functions that it relies on.\n\nFull E2Es are supported (and recommended) and will run everything on the file system like a user would.\n\n## Testing your plugin\n\nOne of the biggest benefits that the Nx Plugin package provides is support for E2E and unit testing.\n\nWhen the E2E app runs, a temporary E2E directory is created in the root of the workspace. This directory is a blank Nx workspace, and will have the plugin's built package installed locally.\n\n### E2E Testing file\n\nWhen the plugin is generated, a test file is created in the `my-plugin-e2e` app. Inside this test file, there are already tests for making sure that the executor ran, checking if directories are created with the `--directory` option, and checking if tags are added to the project configuration.\n\nWe'll go over a few parts of a test file below:\n\n```typescript\nit('should create my-plugin', async (done) => {\n const plugin = uniq('my-plugin');\n ensureNxProject('@my-org/my-plugin', 'dist/packages/my-plugin');\n await runNxCommandAsync(`generate @my-org/my-plugin:myPlugin ${plugin}`);\n\n const result = await runNxCommandAsync(`build ${plugin}`);\n expect(result.stdout).toContain('Executor ran');\n\n done();\n});\n```\n\n- The `uniq` function creates a random name with the prefix and a random number.\n- The `ensureNxProject` is the function that will create the temporary directory. It takes two arguments, the plugin package name and the dist directory of when it's built.\n- The `runNxCommandAsync` will execute a `nx` command in the E2E directory.\n\nThere are additional functions that the `@nrwl/nx-plugin/testing` package exports. Most of them are file utilities to manipulate and read files in the E2E directory.\n\n## Including Assets\n\nSometimes you might want to include some assets with the plugin. This might be a image or some additional binaries.\n\nTo make sure that assets are copied to the dist folder, open the plugin's `project.json` file. Inside the `build` property, add additional assets. By default, all `.md` files in the root, all non-ts files in folders, and the `generators.json` and `executors.json` files are included.\n\n```json\n\"build\": {\n \"executor\": \"@nrwl/node:package\",\n \"options\": {\n // shortened...\n \"assets\": [\n \"packages/my-plugin/*.md\",\n {\n \"input\": \"./packages/my-plugin/src\",\n \"glob\": \"**/*.!(ts)\",\n \"output\": \"./src\"\n },\n {\n \"input\": \"./packages/my-plugin\",\n \"glob\": \"generators.json\",\n \"output\": \".\"\n },\n {\n \"input\": \"./packages/my-plugin\",\n \"glob\": \"executors.json\",\n \"output\": \".\"\n }\n ]\n }\n}\n```\n\n## Using your Nx Plugin\n\nTo use your plugin, simply list it in `nx.json` or use its generators and executors as you would for any other plugin. This could look like `nx g @my-org/my-plugin:lib` for generators or `\"executor\": \"@my-org/my-plugin:build\"` for executors. It should be usable in all of the same ways as published plugins in your local workspace immediately after generating it. This includes setting it up as the default collection in `nx.json`, which would allow you to run `nx g lib` and hit your plugin's generator.\n\n## Publishing your Nx Plugin\n\nIn order to use your plugin in other workspaces or share it with the community, you will need to publish it to an npm registry. To publish your plugin follow these steps:\n\n1. Build your plugin with the command `nx run my-plugin:build`\n1. `npm publish ./dist/package/my-plugin` and follow the prompts from npm.\n1. That's it!\n\n> Note: currently you will have to modify the `package.json` version by yourself or with a tool.\n\nAfter that, you can then install your plugin like any other npm package,\n`npm i -D @my-org/my-plugin` or `yarn add -D @my-org/my-plugin`.\n\n### Listing your Nx Plugin\n\nNx provides a utility (`nx list`) that lists both core and community plugins. To submit your plugin, please follow the steps below:\n\n- Fork the [Nx repo](https://github.com/nrwl/nx/fork) (if you haven't already)\n- Update the [`community/approved-plugins.json` file](https://github.com/nrwl/nx/blob/master/community/approved-plugins.json) with a new entry for your plugin that includes name, url and description\n- Use the following commit message template: `chore(core): nx plugin submission [PLUGIN_NAME]`\n- push your changes, and run `yarn submit-plugin`\n\n> The `yarn submit-plugin` command automatically opens the Github pull request process with the correct template.\n\nWe will then verify the plugin, offer suggestions or merge the pull request!\n\n## Preset\n\nA Preset is a customization option which you provide when creating a new workspace. TS, Node, React are some internal presets that Nx provides by default.\n\n{% youtube\nsrc=\"https://www.youtube.com/embed/yGUrF0-uqaU\"\ntitle=\"Develop a Nx Preset for your Nx Plugin\"\nwidth=\"100%\" /%}\n\n### Custom Preset\n\nAt its core a preset is a generator, which we can create inside of a plugin.\nIf you **don't** have an existing plugin you can create one by running\n\n```bash\n npx create-nx-plugin my-org --pluginName my-plugin\n```\n\nTo create our preset inside of our plugin we can run\n\n```bash\n nx generate @nrwl/nx-plugin:generator --name=preset --project=happynrwl\n```\n\n> Note: the word `preset` is required for the name of this generator\n\nYou should have a similar structure to this:\n\n```treeview\nhappynrwl/\n\t├── e2e\n\t├── jest.config.js\n\t├── jest.preset.js\n\t├── nx.json\n\t├── package-lock.json\n\t├── package.json\n\t├── packages\n\t│ └── happynrwl\n\t│ ├── src\n\t│ │ ├── executors\n\t│ │ ├── generators\n\t│ │ │ ├── happynrwl\n\t│ │ │ └── preset \t\t// <------------- Here\n\t│ │ └── index.ts\n\t├── tools\n\t├── tsconfig.base.json\n\t└── workspace.json\n```\n\nAfter the command is finished, the preset generator is created under the folder named **preset**.\nThe **generator.ts** provides an entry point to the generator. This file contains a function that is called to perform manipulations on a tree that represents the file system. The **schema.json** provides a description of the generator, available options, validation information, and default values.\n\nHere is the sample generator function which you can customize to meet your needs.\n\n```typescript\nexport default async function (tree: Tree, options: PresetGeneratorSchema) {\n const normalizedOptions = normalizeOptions(tree, options);\n addProjectConfiguration(tree, normalizedOptions.projectName, {\n root: normalizedOptions.projectRoot,\n projectType: 'application',\n sourceRoot: `${normalizedOptions.projectRoot}/src`,\n targets: {\n exec: {\n executor: 'nx:run-commands',\n options: {\n command: `node ${projectRoot}/src/index.js`,\n },\n },\n },\n tags: normalizedOptions.parsedTags,\n });\n addFiles(tree, normalizedOptions);\n await formatFiles(tree);\n}\n```\n\nTo get an in-depth guide on customizing/running or debugging your generator see [workspace generators](https://nx.dev/generators/workspace-generators#running-a-workspace-generator).\n\n#### Usage\n\nBefore you are able to use your newly created preset you must package and publish it to a registry.\n\nAfter you have published your plugin to a registry you can now use your preset when creating a new workspace\n\n```bash\nnpx create-nx-workspace my-workspace --preset=my-plugin-name\n```\n"
}
],
"generators": [
{
"name": "plugin",
"factory": "./src/generators/plugin/plugin",
"schema": {
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "NxPluginPlugin",
"title": "Create a Plugin for Nx",
"description": "Create a Plugin for Nx.",
"type": "object",
"examples": [
{
"command": "g plugin my-plugin --directory=plugins --importPath=@myorg/my-plugin",
"description": "Generate `libs/plugins/my-plugin`"
}
],
"properties": {
"name": {
"type": "string",
"description": "Plugin name",
"$default": { "$source": "argv", "index": 0 },
"x-prompt": "What name would you like to use for the plugin?"
},
"directory": {
"type": "string",
"description": "A directory where the plugin is placed.",
"alias": "d"
},
"importPath": {
"type": "string",
"description": "How the plugin will be published, like `@myorg/my-awesome-plugin`. Note this must be a valid NPM name."
},
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
"enum": ["eslint", "tslint"],
"default": "eslint",
"x-deprecated": "TSLint support is deprecated and will be removed"
},
"unitTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"description": "Test runner to use for unit tests.",
"default": "jest"
},
"tags": {
"type": "string",
"description": "Add tags to the library (used for linting).",
"alias": "t"
},
"skipFormat": {
"description": "Skip formatting files.",
"type": "boolean",
"default": false
},
"skipTsConfig": {
"type": "boolean",
"default": false,
"description": "Do not update tsconfig.json for development experience."
},
"skipLintChecks": {
"type": "boolean",
"default": false,
"description": "Do not eslint configuration for plugin json files."
},
"standaloneConfig": {
"description": "Split the project configuration into `<projectRoot>/project.json` rather than including it inside `workspace.json`.",
"type": "boolean"
},
"setParserOptionsProject": {
"type": "boolean",
"description": "Whether or not to configure the ESLint `parserOptions.project` option. We do not do this by default for lint performance reasons.",
"default": false
},
"compiler": {
"type": "string",
"enum": ["tsc", "swc"],
"default": "tsc",
"description": "The compiler used by the build and test targets."
},
"minimal": {
"type": "boolean",
"description": "Generate the plugin with a minimal setup. This would involve not generating a default executor and generator.",
"default": false
}
},
"required": ["name"],
"additionalProperties": false,
"presets": []
},
"description": "Create a Nx Plugin.",
"implementation": "/packages/nx-plugin/src/generators/plugin/plugin.ts",
"aliases": [],
"hidden": false,
"path": "/packages/nx-plugin/src/generators/plugin/schema.json"
},
{
"name": "e2e-project",
"factory": "./src/generators/e2e-project/e2e",
"schema": {
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "NxPluginE2E",
"title": "Create an E2E app for a Nx Plugin",
"description": "Create an E2E app for a Nx Plugin.",
"type": "object",
"properties": {
"pluginName": {
"type": "string",
"description": "the name of the plugin to be tested."
},
"npmPackageName": {
"type": "string",
"description": "the name of the package that would be published to NPM."
},
"projectDirectory": {
"type": "string",
"description": "the directory where the plugin is placed."
},
"pluginOutputPath": {
"type": "string",
"description": "the output path of the plugin after it builds."
},
"jestConfig": {
"type": "string",
"description": "Jest config file."
},
"tsSpecConfig": {
"type": "string",
"description": "Spec `tsconfig` file.",
"x-deprecated": true
},
"standaloneConfig": {
"description": "Split the project configuration into `<projectRoot>/project.json` rather than including it inside `workspace.json`.",
"type": "boolean"
},
"minimal": {
"type": "boolean",
"description": "Generate the e2e project with a minimal setup. This would involve not generating tests for a default executor and generator.",
"default": false
}
},
"required": ["pluginName", "npmPackageName"],
"additionalProperties": false,
"presets": []
},
"description": "Create a E2E application for a Nx Plugin.",
"hidden": true,
"implementation": "/packages/nx-plugin/src/generators/e2e-project/e2e.ts",
"aliases": [],
"path": "/packages/nx-plugin/src/generators/e2e-project/schema.json"
},
{
"name": "migration",
"factory": "./src/generators/migration/migration",
"schema": {
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "NxPluginMigration",
"title": "Create a Migration for an Nx Plugin",
"description": "Create a Migration for an Nx Plugin.",
"type": "object",
"examples": [
{
"command": "nx g migration my-migration --project=my-plugin --version=1.0.0",
"description": "Generate `libs/my-plugin/src/migrations/my-migration`"
}
],
"properties": {
"project": {
"type": "string",
"description": "The name of the project.",
"alias": "p",
"$default": { "$source": "projectName" },
"x-prompt": "What is the name of the project for the migration?"
},
"name": {
"type": "string",
"description": "Migration name.",
"$default": { "$source": "argv", "index": 0 }
},
"description": {
"type": "string",
"description": "Migration description.",
"alias": "d"
},
"packageVersion": {
"type": "string",
"description": "Version to use for the migration.",
"alias": "v",
"x-prompt": "What version would you like to use for the migration?"
},
"packageJsonUpdates": {
"type": "boolean",
"description": "Whether or not to include `package.json` updates.",
"alias": "p",
"default": false
}
},
"required": ["project", "packageVersion"],
"additionalProperties": false,
"presets": []
},
"description": "Create a migration for an Nx Plugin.",
"implementation": "/packages/nx-plugin/src/generators/migration/migration.ts",
"aliases": [],
"hidden": false,
"path": "/packages/nx-plugin/src/generators/migration/schema.json"
},
{
"name": "generator",
"factory": "./src/generators/generator/generator",
"schema": {
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "NxPluginGenerator",
"title": "Create a Generator for an Nx Plugin",
"description": "Create a Generator for an Nx Plugin.",
"type": "object",
"examples": [
{
"command": "nx g generator my-generator --project=my-plugin",
"description": "Generate `libs/my-plugin/src/generators/my-generator`"
}
],
"properties": {
"project": {
"type": "string",
"description": "The name of the project.",
"alias": "p",
"$default": { "$source": "projectName" },
"x-prompt": "What is the name of the project for the generator?"
},
"name": {
"type": "string",
"description": "Generator name.",
"$default": { "$source": "argv", "index": 0 },
"x-prompt": "What name would you like to use for the generator?"
},
"description": {
"type": "string",
"description": "Generator description.",
"alias": "d"
},
"unitTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"description": "Test runner to use for unit tests.",
"default": "jest"
}
},
"required": ["project", "name"],
"additionalProperties": false,
"presets": []
},
"description": "Create a generator for an Nx Plugin.",
"implementation": "/packages/nx-plugin/src/generators/generator/generator.ts",
"aliases": [],
"hidden": false,
"path": "/packages/nx-plugin/src/generators/generator/schema.json"
},
{
"name": "executor",
"factory": "./src/generators/executor/executor",
"schema": {
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "NxPluginExecutor",
"title": "Create an Executor for an Nx Plugin",
"description": "Create an Executor for an Nx Plugin.",
"type": "object",
"examples": [
{
"command": "nx g executor my-executor --project=my-plugin",
"description": "Generate `libs/my-plugin/src/executors/my-executor`"
}
],
"properties": {
"project": {
"type": "string",
"description": "The name of the project.",
"alias": "p",
"$default": { "$source": "projectName" },
"x-prompt": "What is the name of the project for the executor?"
},
"name": {
"type": "string",
"description": "Executor name.",
"$default": { "$source": "argv", "index": 0 },
"x-prompt": "What name would you like to use for the executor?"
},
"description": {
"type": "string",
"description": "Executor description.",
"alias": "d"
},
"unitTestRunner": {
"type": "string",
"enum": ["jest", "none"],
"description": "Test runner to use for unit tests.",
"default": "jest"
},
"includeHasher": {
"type": "boolean",
"default": false,
"description": "Should the boilerplate for a custom hasher be generated?"
}
},
"required": ["project", "name"],
"additionalProperties": false,
"presets": []
},
"description": "Create an executor for an Nx Plugin.",
"implementation": "/packages/nx-plugin/src/generators/executor/executor.ts",
"aliases": [],
"hidden": false,
"path": "/packages/nx-plugin/src/generators/executor/schema.json"
},
{
"name": "plugin-lint-checks",
"factory": "./src/generators/lint-checks/generator",
"schema": {
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "PluginLint",
"title": "",
"type": "object",
"description": "Adds linting configuration to validate common json files for nx plugins.",
"properties": {
"projectName": {
"type": "string",
"description": "Which project should be the configuration be added to?",
"$default": { "$source": "projectName" }
}
},
"required": ["projectName"],
"presets": []
},
"description": "Adds linting configuration to validate common json files for nx plugins.",
"implementation": "/packages/nx-plugin/src/generators/lint-checks/generator.ts",
"aliases": [],
"hidden": false,
"path": "/packages/nx-plugin/src/generators/lint-checks/schema.json"
}
],
"executors": [
{
"name": "e2e",
"implementation": "/packages/nx-plugin/src/executors/e2e/e2e.impl.ts",
"schema": {
"title": "Nx Plugin Playground Target",
"description": "Creates a playground for a Nx Plugin.",
"cli": "nx",
"type": "object",
"properties": {
"target": {
"description": "The build target for the Nx Plugin project.",
"type": "string"
},
"jestConfig": {
"type": "string",
"description": "Jest config file."
},
"tsSpecConfig": {
"type": "string",
"description": "The tsconfig file for specs.",
"x-deprecated": "Use the `tsconfig` property for `ts-jest` in the e2e project `jest.config.js` file. It will be removed in the next major release."
}
},
"additionalProperties": false,
"required": ["target", "jestConfig"],
"presets": []
},
"description": "Creates and runs the E2E tests for an Nx Plugin.",
"aliases": [],
"hidden": false,
"path": "/packages/nx-plugin/src/executors/e2e/schema.json"
}
]
}