Search code examples
nrwl-nxnrwl

How to generate an Angular app in a custom Nx generator?


I have an Nx Angular monorepository. I want to implement a custom generator so I can create apps quickly. I'm able to generate libraries by using the libraryGenerator function, from @nrwl/workspace. My generator is looking like this so far:

import { Tree, formatFiles } from '@nrwl/devkit';
import { libraryGenerator } from '@nrwl/workspace';

export default async function (host: Tree, schema: App) {
  await generateLibrary(host, schema, 'models', 'domain');
  await generateLibrary(host, schema, 'data-access', 'data');

  await formatFiles(host);
}

async function generateLibrary(
  host: Tree,
  schema: App,
  libraryName: string,
  type: string
) {
  const directory = `${schema.system.toLowerCase()}/${schema.name}`;

  const importPath = `@org/${schema.system.toUpperCase()}/${
    schema.name
  }/${libraryName}`;

  await libraryGenerator(host, {
    name: libraryName,
    directory,
    importPath,
    linter: 'eslint',
    simpleModuleName: true,
    strict: true,
    tags: `type:${type}', scope:${schema.scope}`,
  });
}

Now, I want to generate apps as well, but I'm not sure how one does that, since there's nothing like an "app generator" coming out of @nrwl/workspace, nor from @nrwl/devkit.


Solution

  • You can use applicationGenerator from @nrwl/angular/generators.

    import { applicationGenerator } from '@nrwl/angular/generators
    ....
    ....
    ....
    await applicationGenerator(host, {
      name: appName,
      directory: directoryName,
      style: 'scss',
      tags: ``,
      routing: false,
      e2eTestRunner: E2eTestRunner.None,
    });
    

    If you are looking for generating nest application, you can use :

    import { applicationGenerator } from '@nrwl/nest';
    ....
    ....
    ....
    await applicationGenerator(host, {
      name: appName,
      unitTestRunner: 'jest',
      linter: Linter.EsLint,
      directory: directoryName,
      tags: ``
    });