Skip to content

Latest commit

 

History

History
103 lines (87 loc) · 2.09 KB

SOLUTION.md

File metadata and controls

103 lines (87 loc) · 2.09 KB
Generate a util-lib workspace generator:
nx generate @nrwl/workspace:workspace-generator util-lib
Running the generator in dry mode
nx workspace-generator util-lib test --dry-run
Prefixing the name
import { Tree, formatFiles, installPackagesTask } from '@nrwl/devkit';
import { libraryGenerator } from '@nrwl/workspace/generators';

export default async function(host: Tree, schema: any) {
  await libraryGenerator(host, {
    name: `util-${schema.name}`,
  });
  await formatFiles(host);
  return () => {
    installPackagesTask(host);
  };
}
Adding an enum to a generator that prompts when empty
{
  "directory": {
    "type": "string",
    "description": "The scope of your lib.",
    "x-prompt": {
      "message": "Which directory do you want the lib to be in?",
      "type": "list",
      "items": [
        {
          "value": "store",
          "label": "store"
        },
        {
          "value": "api",
          "label": "api"
        },
        {
          "value": "shared",
          "label": "shared"
        }
      ]
    }
  }
}
Choosing the directory
import { Tree, formatFiles, installPackagesTask } from '@nrwl/devkit';
import { libraryGenerator } from '@nrwl/workspace/generators';

export default async function(host: Tree, schema: any) {
  await libraryGenerator(host, {
    name: `util-${schema.name}`,
    directory: schema.directory
  });
  await formatFiles(host);
  return () => {
    installPackagesTask(host);
  };
}
Passing in tags
import { Tree, formatFiles, installPackagesTask } from '@nrwl/devkit';
import { libraryGenerator } from '@nrwl/workspace/generators';

export default async function(host: Tree, schema: any) {
  await libraryGenerator(host, {
    name: `util-${schema.name}`,
    directory: schema.directory,
    tags: `type:util, scope:${schema.directory}`
  });
  await formatFiles(host);
  return () => {
    installPackagesTask(host);
  };
}
Typed Schema
interface Schema {
  name: string;
  directory: 'store' | 'api' | 'shared';
}