From c486b30c4d25a0e19b84a1d583afbd17a27c7d88 Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Thu, 16 Jun 2022 22:15:27 +0100 Subject: [PATCH] feat(nx-plugin): add a minimal setup flag to produce an empty nx-plugin (#10718) * feat(nx-plugin): add a minimal setup flag to produce an empty nx-plugin * feat(nx-plugin): add e2e minimal setup --- docs/generated/packages/nx-plugin.json | 10 +++++ .../src/generators/e2e-project/e2e.spec.ts | 20 +++++++++ .../src/generators/e2e-project/e2e.ts | 1 + .../tests/__pluginName__.spec.ts__tmpl__ | 3 ++ .../src/generators/e2e-project/schema.d.ts | 1 + .../src/generators/e2e-project/schema.json | 5 +++ .../src/generators/plugin/plugin.spec.ts | 42 +++++++++++++++++++ .../nx-plugin/src/generators/plugin/plugin.ts | 4 ++ .../src/generators/plugin/schema.d.ts | 1 + .../src/generators/plugin/schema.json | 5 +++ 10 files changed, 92 insertions(+) diff --git a/docs/generated/packages/nx-plugin.json b/docs/generated/packages/nx-plugin.json index 4bf90da35c..d1c22bd9c1 100644 --- a/docs/generated/packages/nx-plugin.json +++ b/docs/generated/packages/nx-plugin.json @@ -93,6 +93,11 @@ "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"], @@ -144,6 +149,11 @@ "standaloneConfig": { "description": "Split the project configuration into `/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"], diff --git a/packages/nx-plugin/src/generators/e2e-project/e2e.spec.ts b/packages/nx-plugin/src/generators/e2e-project/e2e.spec.ts index a765284adc..719edff701 100644 --- a/packages/nx-plugin/src/generators/e2e-project/e2e.spec.ts +++ b/packages/nx-plugin/src/generators/e2e-project/e2e.spec.ts @@ -4,6 +4,7 @@ import { readProjectConfiguration, readJson, getProjects, + joinPathFragments, } from '@nrwl/devkit'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; import { e2eProjectGenerator } from './e2e'; @@ -147,4 +148,23 @@ describe('NxPlugin e2e-project Generator', () => { expect(tree.exists('apps/my-plugin-e2e/tsconfig.spec.json')).toBeTruthy(); expect(tree.exists('apps/my-plugin-e2e/jest.config.ts')).toBeTruthy(); }); + + it('should not generate tests when minimal flag is passed', async () => { + // ARRANGE & ACT + await e2eProjectGenerator(tree, { + pluginName: 'my-plugin', + pluginOutputPath: `dist/libs/my-plugin`, + npmPackageName: '@proj/my-plugin', + standaloneConfig: false, + minimal: true, + }); + + const { root } = readProjectConfiguration(tree, 'my-plugin-e2e'); + + // ASSERT + + expect( + tree.read(joinPathFragments(root, 'tests/my-plugin.spec.ts'), 'utf-8') + ).not.toContain("it('should create "); + }); }); diff --git a/packages/nx-plugin/src/generators/e2e-project/e2e.ts b/packages/nx-plugin/src/generators/e2e-project/e2e.ts index 5fd08ec7fc..dc6e33d2ba 100644 --- a/packages/nx-plugin/src/generators/e2e-project/e2e.ts +++ b/packages/nx-plugin/src/generators/e2e-project/e2e.ts @@ -34,6 +34,7 @@ function normalizeOptions(host: Tree, options: Schema): NormalizedSchema { return { ...options, + minimal: options.minimal ?? false, projectName, pluginPropertyName, projectRoot, diff --git a/packages/nx-plugin/src/generators/e2e-project/files/tests/__pluginName__.spec.ts__tmpl__ b/packages/nx-plugin/src/generators/e2e-project/files/tests/__pluginName__.spec.ts__tmpl__ index bc077cf6a2..e4c62d874e 100644 --- a/packages/nx-plugin/src/generators/e2e-project/files/tests/__pluginName__.spec.ts__tmpl__ +++ b/packages/nx-plugin/src/generators/e2e-project/files/tests/__pluginName__.spec.ts__tmpl__ @@ -23,6 +23,8 @@ describe('<%= pluginName %> e2e', () => { runNxCommandAsync('reset'); }); + <% if(!minimal) { %> + it('should create <%= pluginName %>', async () => { const project = uniq('<%= pluginName %>'); await runNxCommandAsync( @@ -55,4 +57,5 @@ describe('<%= pluginName %> e2e', () => { expect(project.tags).toEqual(['e2etag', 'e2ePackage']); }, 120000); }); + <% } %> }); diff --git a/packages/nx-plugin/src/generators/e2e-project/schema.d.ts b/packages/nx-plugin/src/generators/e2e-project/schema.d.ts index ae365e0e7f..43dbadd9bb 100644 --- a/packages/nx-plugin/src/generators/e2e-project/schema.d.ts +++ b/packages/nx-plugin/src/generators/e2e-project/schema.d.ts @@ -5,4 +5,5 @@ export interface Schema { pluginOutputPath?: string; jestConfig?: string; standaloneConfig?: boolean; + minimal?: boolean; } diff --git a/packages/nx-plugin/src/generators/e2e-project/schema.json b/packages/nx-plugin/src/generators/e2e-project/schema.json index 024a1c402d..d3c40b989a 100644 --- a/packages/nx-plugin/src/generators/e2e-project/schema.json +++ b/packages/nx-plugin/src/generators/e2e-project/schema.json @@ -34,6 +34,11 @@ "standaloneConfig": { "description": "Split the project configuration into `/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"], diff --git a/packages/nx-plugin/src/generators/plugin/plugin.spec.ts b/packages/nx-plugin/src/generators/plugin/plugin.spec.ts index e053f6b3c0..143f399375 100644 --- a/packages/nx-plugin/src/generators/plugin/plugin.spec.ts +++ b/packages/nx-plugin/src/generators/plugin/plugin.spec.ts @@ -128,6 +128,48 @@ describe('NxPlugin Plugin Generator', () => { ).toContain('const variable = "<%= projectName %>";'); }); + it('should not create generator and executor files for minimal setups', async () => { + await pluginGenerator(tree, getSchema({ name: 'myPlugin', minimal: true })); + + [ + 'libs/my-plugin/project.json', + 'libs/my-plugin/generators.json', + 'libs/my-plugin/executors.json', + ].forEach((path) => expect(tree.exists(path)).toBeTruthy()); + + [ + 'libs/my-plugin/src/generators/my-plugin/schema.d.ts', + 'libs/my-plugin/src/generators/my-plugin/generator.ts', + 'libs/my-plugin/src/generators/my-plugin/generator.spec.ts', + 'libs/my-plugin/src/generators/my-plugin/schema.json', + 'libs/my-plugin/src/generators/my-plugin/schema.d.ts', + 'libs/my-plugin/src/generators/my-plugin/files/src/index.ts__template__', + 'libs/my-plugin/src/executors/build/executor.ts', + 'libs/my-plugin/src/executors/build/executor.spec.ts', + 'libs/my-plugin/src/executors/build/schema.json', + 'libs/my-plugin/src/executors/build/schema.d.ts', + ].forEach((path) => expect(tree.exists(path)).toBeFalsy()); + + expect(tree.read('libs/my-plugin/generators.json', 'utf-8')) + .toMatchInlineSnapshot(` + "{ + \\"$schema\\": \\"http://json-schema.org/schema\\", + \\"name\\": \\"my-plugin\\", + \\"version\\": \\"0.0.1\\", + \\"generators\\": {} + } + " + `); + expect(tree.read('libs/my-plugin/executors.json', 'utf-8')) + .toMatchInlineSnapshot(` + "{ + \\"$schema\\": \\"http://json-schema.org/schema\\", + \\"executors\\": {} + } + " + `); + }); + describe('--unitTestRunner', () => { describe('none', () => { it('should not generate test files', async () => { diff --git a/packages/nx-plugin/src/generators/plugin/plugin.ts b/packages/nx-plugin/src/generators/plugin/plugin.ts index b142bb194b..3c63fb4fd9 100644 --- a/packages/nx-plugin/src/generators/plugin/plugin.ts +++ b/packages/nx-plugin/src/generators/plugin/plugin.ts @@ -37,6 +37,9 @@ async function addFiles(host: Tree, options: NormalizedSchema) { } ); + if (options.minimal) { + return; + } await generatorGenerator(host, { project: options.name, name: options.name, @@ -118,6 +121,7 @@ export async function pluginGenerator(host: Tree, schema: Schema) { pluginOutputPath: `dist/${options.libsDir}/${options.projectDirectory}`, npmPackageName: options.npmPackageName, standaloneConfig: options.standaloneConfig ?? true, + minimal: options.minimal ?? false, }); if (options.linter === Linter.EsLint && !options.skipLintChecks) { await pluginLintCheckGenerator(host, { projectName: options.name }); diff --git a/packages/nx-plugin/src/generators/plugin/schema.d.ts b/packages/nx-plugin/src/generators/plugin/schema.d.ts index 04ce184d49..ee83483a68 100644 --- a/packages/nx-plugin/src/generators/plugin/schema.d.ts +++ b/packages/nx-plugin/src/generators/plugin/schema.d.ts @@ -13,4 +13,5 @@ export interface Schema { standaloneConfig?: boolean; setParserOptionsProject?: boolean; compiler: 'swc' | 'tsc'; + minimal?: boolean; } diff --git a/packages/nx-plugin/src/generators/plugin/schema.json b/packages/nx-plugin/src/generators/plugin/schema.json index 87a5ef4856..afaa8282e2 100644 --- a/packages/nx-plugin/src/generators/plugin/schema.json +++ b/packages/nx-plugin/src/generators/plugin/schema.json @@ -77,6 +77,11 @@ "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"],