SOLUTION.md
May 31, 2023 ยท View on GitHub
Generate a internal-plugin plugin:
nx generate @nx/plugin:plugin internal-plugin --minimal
Generate a util-lib generator:
nx generate @nx/plugin:generator util-lib --project=internal-plugin
Running the generator in dry mode
nx generate @bg-hoard/internal-plugin:util-lib test --dry-run
Prefixing the name
import { formatFiles, installPackagesTask, Tree } from '@nx/devkit';
import { libraryGenerator } from '@nx/js';
import { UtilLibGeneratorSchema } from './schema';
export default async function (tree: Tree, schema: UtilLibGeneratorSchema) {
await libraryGenerator(tree, {
name: `util-${schema.name}`,
});
await formatFiles(tree);
return () => {
installPackagesTask(tree);
};
}
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"
}
]
}
}
}
Passing in tags
import { formatFiles, installPackagesTask, Tree } from '@nx/devkit';
import { libraryGenerator } from '@nx/js';
import { UtilLibGeneratorSchema } from './schema';
export default async function (tree: Tree, schema: UtilLibGeneratorSchema) {
await libraryGenerator(tree, {
name: `util-${schema.name}`,
directory: schema.directory,
tags: `type:util, scope:${schema.directory}`,
});
await formatFiles(tree);
return () => {
installPackagesTask(tree);
};
}
Typed Schema
export interface UtilLibGeneratorSchema {
name: string;
directory: 'store' | 'api' | 'shared';
}
BONUS: Testing
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { Tree, readProjectConfiguration } from '@nx/devkit';
import generator from './generator';
import { UtilLibGeneratorSchema } from './schema';
describe('util-lib generator', () => {
let appTree: Tree;
const options: UtilLibGeneratorSchema = { name: 'foo', directory: 'store' };
beforeEach(() => {
appTree = createTreeWithEmptyWorkspace();
});
it('should add util to the name and add appropriate tags', async () => {
await generator(appTree, options);
const config = readProjectConfiguration(appTree, 'store-util-foo');
expect(config).toBeDefined();
expect(config.tags).toEqual(['type:util', 'scope:store']);
});
});