Nx Devkit

Nx is a pluggable build tool, so most of its functionality is provided by plugins. The Nx Devkit is the underlying technology used to customize Nx to support different technologies and your own custom use-cases.

Plugins have:

  • Generators
    • Anytime you run nx generate ..., you invoke a generator.
    • Generators automate making changes to the file system.
    • They are used to create/update applications, libraries, components, etc..
  • Executors
    • Anytime you run nx run ... (or nx test, nx build), you invoke an executor.
    • Executors define how to perform an action on a project.
    • They are used to build applications and libraries, test them, lint them, etc..

All of the core plugins are written using Nx Devkit, and you can use the same utilities to write your own generators and executors.

Pay as You Go

As with most things in Nx, the core of Nx Devkit is very simple. It only uses language primitives and immutable objects (the tree being the only exception). See Simplest Generator and Simplest Executor. Most of what you will see in this guide are extra affordances -- things that are optional to use, but we found very handy when building plugins.

Generators

Generators automate making file changes. They can create new files, overwrite existing files, delete existing files, etc. For example, adding a new application often requires creating numerous files and updating configuration. Adding a new component may require adding a storybook configuration and a suite of e2e tests. Generators can automate all of these and help you focus on actually matters.

A generator has:

  • a schema describing the inputs (i.e., flags, args, options)
  • the implementation taking the inputs and making changes to the file system

Unlike many other tools Nx generators update the file system atomically at the end. This means that if an error occurs, the file system is not partially updated. It also means that you can preview the changes to the file system without actually modifying any files.

Schema

A generator's schema describes the inputs--what you can pass into it. The schema is used to validate inputs, to parse args (e.g., covert strings into numbers), to set defaults, and to power the VSCode plugin. It is written with JSON Schema.

Examples

1{
2  "cli": "nx",
3  "id": "CustomGenerator",
4  "description": "Create a custom generator",
5  "type": "object",
6  "properties": {
7    "name": {
8      "type": "string",
9      "description": "Generator name",
10      "x-prompt": "What name would you like to use for the workspace generator?"
11    },
12    "skipFormat": {
13      "description": "Skip formatting files",
14      "type": "boolean",
15      "alias": "sf",
16      "default": false
17    }
18  },
19  "required": ["name"]
20}

The schema above defines two fields: name and skipFormat. The name field is a string, skipFormat is a boolean. The x-prompt property tells Nx to ask for the name value if one isn't given. The skipFormat field has the default value set to false. The schema language is rich and lets you use lists, enums, references, etc.. A few more examples:

1{
2  "cli": "nx",
3  "id": "CustomGenerator",
4  "description": "Create a custom generator",
5  "type": "object",
6  "properties": {
7    "stringOrBoolean": {
8      "oneOf": [
9        {
10          "type": "string",
11          "default": "mystring!"
12        },
13        {
14          "type": "boolean"
15        }
16      ]
17    },
18    "innerObject": {
19      "type": "object",
20      "properties": {
21        "key": {
22          "type": "boolean"
23        }
24      }
25    },
26    "array": {
27      "type": "array",
28      "items": {
29        "type": "number"
30      }
31    },
32    "complexXPrompt": {
33      "type": "string",
34      "default": "css",
35      "x-prompt": {
36        "message": "Which stylesheet format would you like to use?",
37        "type": "list",
38        "items": [
39          {
40            "value": "css",
41            "label": "CSS"
42          },
43          {
44            "value": "scss",
45            "label": "SASS(.scss)"
46          },
47          {
48            "value": "styl",
49            "label": "Stylus(.styl)"
50          },
51          {
52            "value": "none",
53            "label": "None"
54          }
55        ]
56      }
57    },
58    "positionalArg": {
59      "type": "string",
60      "$default": {
61        "$source": "argv",
62        "index": 0
63      }
64    },
65    "currentProject": {
66      "type": "string",
67      "$default": {
68        "$source": "projectName"
69      }
70    }
71  }
72}

Sometimes, you may not know the schema or may not care, in this case, you can set the following:

1{
2  "cli": "nx",
3  "id": "CustomGenerator",
4  "description": "Create a custom generator",
5  "type": "object",
6  "properties": {
7    "name": {
8      "type": "string"
9    }
10  },
11  "required": ["name"],
12  "additionalProperties": true
13}

Because "additionalProperties" is true, the generator above will accept any extra parameters you pass. They, of course, won't be validated or transformed, but sometimes that's good enough.

If you want to learn more about the schema language, check out the core plugins at https://github.com/nrwl/nx for more examples.

Implementation

The implementation is a function that takes two arguments:

  • tree: an implementation of the file system
    • It allows you to read/write files, list children, etc.
    • It's recommended to use the tree instead of directly interacting with the file system.
    • This enables the --dry-run mode so you can try different sets of options before actually making changes to the files.
  • options
    • This is a combination of the options from workspace.json, command-line overrides, and schema defaults.
    • All the options are validated and transformed in accordance with the schema.
    • You normally don't have to validate anything in the implementation function because it won't be invoked unless the schema validation passes.

The implementation can return a callback which is invoked after changes have been made to the file system.

Examples

1import {
2  Tree,
3  generateFiles,
4  formatFiles,
5  installPackagesTask,
6} from '@nrwl/devkit';
7
8interface Schema {
9  name: string;
10  skipFormat: boolean;
11}
12
13export default async function (tree: Tree, options: Schema) {
14  generateFiles(
15    tree,
16    path.join(__dirname, 'files'),
17    path.join('tools/generators', schema.name),
18    options
19  );
20
21  if (!schema.skipFormat) {
22    await formatFiles(tree);
23  }
24
25  return () => {
26    installPackagesTask(tree);
27  };
28}

The generator is an async function. You could create new projects and generate new files, but you could also update existing files and refactor things. It's recommended to limit all the side-effects to interacting with the tree and printing to the console. Sometimes generators perform other side affects (e.g., installing npm packages). Perform them in the function returned from the generator. Nx won't run the returned function in the dry run mode.

Composing Generators

Generators are just async functions so they can be easily composed together. For instance, to write a generator that generates two React libraries:

1import {
2  Tree,
3  generateFiles,
4  formatFiles,
5  installPackagesTask,
6} from '@nrwl/devkit';
7import { libraryGenerator } from '@nrwl/react';
8
9export default async function (tree: Tree, options: Schema) {
10  const libSideEffects1 = libraryGenerator(tree, { name: options.name1 });
11  const libSideEffects2 = libraryGenerator(tree, { name: options.name2 });
12  await performOperationsOnTheTree(tree);
13  return () => {
14    libSideEffects1();
15    libSideEffects2();
16  };
17}

Testing Generators

The Nx Devkit provides the createTreeWithEmptyWorkspace utility to create a tree with an empty workspace that can be used in tests. Other than that, the tests simply invoke the generator and check the changes are made in the tree.

1import { readProjectConfiguration } from '@nrwl/devkit';
2import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
3import createLib from './lib';
4
5describe('lib', () => {
6  it('should create a lib', async () => {
7    const tree = createTreeWithEmptyWorkspace();
8    // update tree before invoking the generator
9    await createLib(tree, { name: 'lib' });
10
11    expect(readProjectConfiguration(tree, 'lib')).toBeDefined();
12  });
13});

Devkit Helper Functions

Nx provides helpers several functions for writing generators:

  • readProjectConfiguration -- Read the project configuration stored in workspace.json and nx.json.
  • addProjectConfiguration -- Add the project configuration stored in workspace.json and nx.json.
  • removeProjectConfiguration -- Remove the project configuration stored in workspace.json and nx.json.
  • updateProjectConfiguration -- Update the project configuration stored in workspace.json and nx.json.
  • readWorkspaceConfiguration -- Read general workspace configuration such as the default project or cli settings.
  • updateWorkspaceConfiguration -- Update general workspace configuration such as the default project or cli settings.
  • getProjects -- Returns the list of projects.
  • generateFiles -- Generate a folder of files based on provided templates.
  • formatFiles -- Format all the created or updated files using Prettier.
  • readJson -- Read a json file.
  • writeJson -- Write a json file.
  • updateJson -- Update a json file.
  • addDependenciesToPackageJson -- Add dependencies and dev dependencies to package.json
  • installPackagesTask -- Runs npm install/yarn install/pnpm install depending on what is used by the workspaces.
  • names -- Util function to generate different strings based off the provided name.
  • getWorkspaceLayout -- Tells where new libs and should be generated.
  • offestFromRoot -- Calculates an offset from the root of the workspace, which is useful for constructing relative URLs.
  • stripIndents -- Strips indents form a multiline string.
  • normalizePath -- Coverts an os specific path to a unix style path.
  • joinPathFragments -- Normalize fragments and joins them with a /.
  • toJS -- Coverts a TypeScript file to JavaScript. Useful for generators that support both.
  • visitNotIgnoredFiles -- Utility to act on all files in a tree that are not ignored by git.
  • applyChangesToString-- Applies a list of changes to a string's original value. This is useful when working with ASTs

Each of those have detailed API docs. Check the API for more information.

It's also important to stress that those are just utility functions. You can use them but you don't have to. You can instead write your own functions that take the tree and do whatever you want to do with it.

Simplest Generator

1{
2  "cli": "nx",
3  "id": "CustomGenerator",
4  "description": "Create a custom generator",
5  "type": "object",
6  "properties": {},
7  "additionalProperties": true
8}
1export default async function (tree, opts) {
2  console.log('options', opts);
3}

Executors

Executors act on a project. Some examples including: building projects, testing projects, serving projects.

An executor consists of the following:

  • a schema describing the inputs (i.e., flags, args, options).
  • the implementation taking the inputs and acting on the project.

Schema

A generator's schema describes the inputs--what you can pass into it. The schema is used to validate inputs, to parse args (e.g., covert strings into numbers), to set defaults, and to power the VSCode plugin. It is written with JSON Schema.

1{
2  "cli": "nx",
3  "id": "Echo",
4  "description": "echo given string",
5  "type": "object",
6  "properties": {
7    "message": {
8      "type": "string",
9      "description": "Message to echo"
10    },
11    "upperCase": {
12      "type": "boolean",
13      "description": "Covert to all upper case",
14      "default": false
15    }
16  },
17  "required": ["message"]
18}

The schema above defines two fields: message and upperCase. The message field is a string, upperCase is a boolean. The schema support for executors and generators is identical. See the section on generators above for more information.

Implementation

The implementation function takes two arguments (the options and the executor context) and returns a promise (or an async iterable) with the success property. The context params contains information about the workspace and the invoked target.

Most of the time executors return a promise.

1interface Schema {
2  message: string;
3  upperCase: boolean;
4}
5
6export default async function printAllCaps(
7  options: Schema,
8  context: ExecutorContext
9): Promise<{ success: true }> {
10  if (options.upperCase) {
11    console.log(options.message.toUpperCase());
12  } else {
13    console.log(options.message);
14  }
15  return { success: true };
16}

But you can also return an async iterable that can yield several values.

1async function wait() {
2  return new Promise((res) => {
3    setTimeout(() => res(), 1000);
4  });
5}
6
7export default async function* counter(opts: { to: number; result: boolean }) {
8  for (let i = 0; i < opts.to; ++i) {
9    console.log(i);
10    yield { success: false };
11    await wait();
12  }
13  yield { success: opts.result };
14}

Composing Executors

An executor is just a function, so you can import and invoke it directly, as follows:

1import printAllCaps from 'print-all-caps';
2
3export default async function (
4  options: Schema,
5  context: ExecutorContext
6): Promise<{ success: true }> {
7  // do something before
8  await printAllCaps({ message: 'All caps' });
9  // do something after
10}

This only works when you know what executor you want to invoke. Sometimes, however, you need to invoke a target. For instance, the e2e target is often configured like this:

1{
2  "e2e": {
3    "builder": "@nrwl/cypress:cypress",
4    "options": {
5      "cypressConfig": "apps/myapp-e2e/cypress.json",
6      "tsConfig": "apps/myapp-e2e/tsconfig.e2e.json",
7      "devServerTarget": "myapp:serve"
8    }
9  }
10}

In this case we need to invoke the target configured in devSeverTarget. We can do it as follows:

1async function* startDevServer(
2  opts: CypressExecutorOptions,
3  context: ExecutorContext
4) {
5  const { project, target, configuration } = parseTargetString(
6    opts.devServerTarget
7  );
8  for await (const output of await runExecutor<{
9    success: boolean;
10    baseUrl?: string;
11  }>(
12    { project, target, configuration },
13    {
14      watch: opts.watch,
15    },
16    context
17  )) {
18    if (!output.success && !opts.watch)
19      throw new Error('Could not compile application files');
20    yield opts.baseUrl || (output.baseUrl as string);
21  }
22}

The runExecutor utility will find the target in the configuration, find the executor, construct the options (as if you invoked it in the terminal) and invoke the executor. Note that runExecutor always returns an iterable instead of a promise.

Devkit Helper Functions

  • logger -- Wraps console to add some formatting.
  • getPackageManagerCommand -- Returns commands for the package manager used in the workspace.
  • parseTargetString -- Parses a target string into {project, target, configuration}.
  • readTargetOptions -- Reads and combines options for a given target.
  • runExecutor -- Constructs options and invokes an executor.

Simplest Executor

1{
2  "cli": "nx",
3  "id": "CustomExecutor",
4  "type": "object",
5  "properties": {},
6  "additionalProperties": true
7}
1export default async function (opts) {
2  console.log('options', opts);
3}

Using RxJS Observables

The Nx devkit only uses language primitives (promises and async iterables). It doesn't use RxJS observables, but you can use them and convert them to a Promise or an async iterable.

You can convert Observables to a Promise with toPromise.

1import { of } from 'rxjs';
2
3export default async function (opts) {
4  return of({ success: true }).toPromise();
5}

You can use the rxjs-for-await library to convert an Observable into an async iterable.

1import { of } from 'rxjs';
2import { eachValueFrom } from 'rxjs-for-await-async';
3
4export default async function (opts) {
5  return eachValueFrom(of({ success: true }));
6}

Using Generators and Executors

There are three main ways to use generators and executors: