From 92adec80fdbc3da9697ec7bddcae7c9a007dc628 Mon Sep 17 00:00:00 2001 From: AgentEnder Date: Thu, 6 Apr 2023 12:56:06 -0400 Subject: [PATCH] feat(nx-plugin): slim down default generated nx-plugin --- docs/generated/manifests/packages.json | 2 +- docs/generated/packages-metadata.json | 2 +- .../nx-plugin/generators/e2e-project.json | 2 +- .../nx-plugin/generators/executor.json | 5 ++ .../nx-plugin/generators/generator.json | 5 ++ .../nx-plugin/generators/migration.json | 5 ++ .../packages/nx-plugin/generators/plugin.json | 8 +-- e2e/nx-plugin/src/nx-plugin.test.ts | 51 +++++++-------- packages/nx-plugin/generators.json | 6 +- .../src/generators/e2e-project/e2e.spec.ts | 18 ------ .../src/generators/e2e-project/e2e.ts | 1 - .../tests/__pluginName__.spec.ts__tmpl__ | 44 +++---------- .../src/generators/e2e-project/schema.d.ts | 1 - .../src/generators/executor/executor.ts | 29 +++++++-- .../src/generators/executor/schema.d.ts | 1 + .../src/generators/executor/schema.json | 5 ++ .../generators/generator/generator.spec.ts | 2 - .../src/generators/generator/generator.ts | 28 ++++++-- .../src/generators/generator/schema.d.ts | 1 + .../src/generators/generator/schema.json | 5 ++ .../generators/lint-checks/generator.spec.ts | 48 +++++++++----- .../src/generators/migration/schema.json | 5 ++ .../src/generators/plugin/plugin.spec.ts | 64 ++----------------- .../nx-plugin/src/generators/plugin/plugin.ts | 16 ----- .../src/generators/plugin/schema.d.ts | 1 - .../src/generators/plugin/schema.json | 10 +-- .../src/generators/preset/generator.ts | 10 ++- .../specify-output-capture.spec.ts | 9 ++- 28 files changed, 173 insertions(+), 211 deletions(-) diff --git a/docs/generated/manifests/packages.json b/docs/generated/manifests/packages.json index f27d3dc71566ff..6766bd4b47fe4b 100644 --- a/docs/generated/manifests/packages.json +++ b/docs/generated/manifests/packages.json @@ -1887,7 +1887,7 @@ "/packages/nx-plugin/generators/e2e-project": { "description": "Create a E2E application for a Nx Plugin.", "file": "generated/packages/nx-plugin/generators/e2e-project.json", - "hidden": true, + "hidden": false, "name": "e2e-project", "originalFilePath": "/packages/nx-plugin/src/generators/e2e-project/schema.json", "path": "/packages/nx-plugin/generators/e2e-project", diff --git a/docs/generated/packages-metadata.json b/docs/generated/packages-metadata.json index 47fadabbb437a4..545505de059133 100644 --- a/docs/generated/packages-metadata.json +++ b/docs/generated/packages-metadata.json @@ -1865,7 +1865,7 @@ { "description": "Create a E2E application for a Nx Plugin.", "file": "generated/packages/nx-plugin/generators/e2e-project.json", - "hidden": true, + "hidden": false, "name": "e2e-project", "originalFilePath": "/packages/nx-plugin/src/generators/e2e-project/schema.json", "path": "nx-plugin/generators/e2e-project", diff --git a/docs/generated/packages/nx-plugin/generators/e2e-project.json b/docs/generated/packages/nx-plugin/generators/e2e-project.json index 84497269109df4..b3af7aa2c8eaa0 100644 --- a/docs/generated/packages/nx-plugin/generators/e2e-project.json +++ b/docs/generated/packages/nx-plugin/generators/e2e-project.json @@ -57,9 +57,9 @@ "presets": [] }, "description": "Create a E2E application for a Nx Plugin.", - "hidden": true, "implementation": "/packages/nx-plugin/src/generators/e2e-project/e2e.ts", "aliases": [], + "hidden": false, "path": "/packages/nx-plugin/src/generators/e2e-project/schema.json", "type": "generator" } diff --git a/docs/generated/packages/nx-plugin/generators/executor.json b/docs/generated/packages/nx-plugin/generators/executor.json index 9ee2bc4cc7f07a..f149c115e8ef35 100644 --- a/docs/generated/packages/nx-plugin/generators/executor.json +++ b/docs/generated/packages/nx-plugin/generators/executor.json @@ -44,6 +44,11 @@ "type": "boolean", "default": false, "description": "Should the boilerplate for a custom hasher be generated?" + }, + "skipLintChecks": { + "type": "boolean", + "default": false, + "description": "Do not eslint configuration for plugin json files." } }, "required": ["project", "name"], diff --git a/docs/generated/packages/nx-plugin/generators/generator.json b/docs/generated/packages/nx-plugin/generators/generator.json index cf7f13dbe30731..225102d9d2bee0 100644 --- a/docs/generated/packages/nx-plugin/generators/generator.json +++ b/docs/generated/packages/nx-plugin/generators/generator.json @@ -40,6 +40,11 @@ "enum": ["jest", "none"], "description": "Test runner to use for unit tests.", "default": "jest" + }, + "skipLintChecks": { + "type": "boolean", + "default": false, + "description": "Do not eslint configuration for plugin json files." } }, "required": ["project", "name"], diff --git a/docs/generated/packages/nx-plugin/generators/migration.json b/docs/generated/packages/nx-plugin/generators/migration.json index 58582d0cff637f..9f95532c3e4f1c 100644 --- a/docs/generated/packages/nx-plugin/generators/migration.json +++ b/docs/generated/packages/nx-plugin/generators/migration.json @@ -45,6 +45,11 @@ "description": "Whether or not to include `package.json` updates.", "alias": "p", "default": false + }, + "skipLintChecks": { + "type": "boolean", + "default": false, + "description": "Do not eslint configuration for plugin json files." } }, "required": ["project", "packageVersion"], diff --git a/docs/generated/packages/nx-plugin/generators/plugin.json b/docs/generated/packages/nx-plugin/generators/plugin.json index e6df079e466627..2bbd845fbe5633 100644 --- a/docs/generated/packages/nx-plugin/generators/plugin.json +++ b/docs/generated/packages/nx-plugin/generators/plugin.json @@ -69,7 +69,7 @@ "type": "string", "enum": ["jest", "none"], "description": "Test runner to use for end to end (E2E) tests.", - "default": "jest" + "default": "none" }, "standaloneConfig": { "description": "Split the project configuration into `/project.json` rather than including it inside `workspace.json`.", @@ -87,15 +87,9 @@ "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"], - "additionalProperties": false, "presets": [] }, "description": "Create a Nx Plugin.", diff --git a/e2e/nx-plugin/src/nx-plugin.test.ts b/e2e/nx-plugin/src/nx-plugin.test.ts index 6988acc304e742..6492c361891f1d 100644 --- a/e2e/nx-plugin/src/nx-plugin.test.ts +++ b/e2e/nx-plugin/src/nx-plugin.test.ts @@ -2,7 +2,6 @@ import { ProjectConfiguration } from '@nrwl/devkit'; import { checkFilesExist, expectTestsPass, - isNotWindows, killPorts, newProject, readJson, @@ -12,8 +11,6 @@ import { uniq, updateFile, createFile, - readFile, - removeFile, cleanupProject, runCommand, getPackageManagerCommand, @@ -39,33 +36,16 @@ describe('Nx Plugin', () => { const lintResults = runCLI(`lint ${plugin}`); expect(lintResults).toContain('All files pass linting.'); - expectTestsPass(await runCLIAsync(`test ${plugin}`)); - const buildResults = runCLI(`build ${plugin}`); expect(buildResults).toContain('Done compiling TypeScript files'); checkFilesExist( `dist/libs/${plugin}/package.json`, - `dist/libs/${plugin}/generators.json`, - `dist/libs/${plugin}/executors.json`, - `dist/libs/${plugin}/src/index.js`, - `dist/libs/${plugin}/src/generators/${plugin}/schema.json`, - `dist/libs/${plugin}/src/generators/${plugin}/schema.d.ts`, - `dist/libs/${plugin}/src/generators/${plugin}/generator.js`, - `dist/libs/${plugin}/src/generators/${plugin}/files/src/index.ts__template__`, - `dist/libs/${plugin}/src/executors/build/executor.js`, - `dist/libs/${plugin}/src/executors/build/schema.d.ts`, - `dist/libs/${plugin}/src/executors/build/schema.json` + `dist/libs/${plugin}/src/index.js` ); const project = readJson(`libs/${plugin}/project.json`); expect(project).toMatchObject({ tags: [], }); - const e2eProject = readJson(`apps/${plugin}-e2e/project.json`); - - expect(e2eProject).toMatchObject({ - tags: [], - implicitDependencies: [`${plugin}`], - }); }, 90000); // the test invoke ensureNxProject, which points to @nrwl/workspace collection @@ -75,7 +55,9 @@ describe('Nx Plugin', () => { // TODO: Re-enable this to work with pnpm it(`should run the plugin's e2e tests`, async () => { const plugin = uniq('plugin-name'); - runCLI(`generate @nrwl/nx-plugin:plugin ${plugin} --linter=eslint`); + runCLI( + `generate @nrwl/nx-plugin:plugin ${plugin} --linter=eslint --e2eTestRunner jest` + ); const e2eResults = runCLI(`e2e ${plugin}-e2e`); expect(e2eResults).toContain('Successfully ran target e2e'); expect(await killPorts()).toBeTruthy(); @@ -195,7 +177,9 @@ describe('Nx Plugin', () => { const plugin = uniq('plugin'); const goodGenerator = uniq('good-generator'); const goodExecutor = uniq('good-executor'); + const badExecutorBadImplPath = uniq('bad-executor'); const goodMigration = uniq('good-migration'); + const badFactoryPath = uniq('bad-generator'); const badMigrationVersion = uniq('bad-version'); const missingMigrationVersion = uniq('missing-version'); @@ -207,10 +191,18 @@ describe('Nx Plugin', () => { `generate @nrwl/nx-plugin:generator ${goodGenerator} --project=${plugin}` ); + runCLI( + `generate @nrwl/nx-plugin:generator ${badFactoryPath} --project=${plugin}` + ); + runCLI( `generate @nrwl/nx-plugin:executor ${goodExecutor} --project=${plugin}` ); + runCLI( + `generate @nrwl/nx-plugin:executor ${badExecutorBadImplPath} --project=${plugin}` + ); + runCLI( `generate @nrwl/nx-plugin:migration ${badMigrationVersion} --project=${plugin} --packageVersion="invalid"` ); @@ -226,7 +218,9 @@ describe('Nx Plugin', () => { updateFile(`libs/${plugin}/generators.json`, (f) => { const json = JSON.parse(f); // @proj/plugin:plugin has an invalid implementation path - json.generators[plugin].factory = `./generators/${plugin}/bad-path`; + json.generators[ + badFactoryPath + ].factory = `./generators/${plugin}/bad-path`; // @proj/plugin:non-existant has a missing implementation path amd schema json.generators['non-existant-generator'] = {}; return JSON.stringify(json); @@ -234,8 +228,9 @@ describe('Nx Plugin', () => { updateFile(`libs/${plugin}/executors.json`, (f) => { const json = JSON.parse(f); - // @proj/plugin:build has an invalid implementation path - json.executors['build'].implementation = './executors/build/bad-path'; + // @proj/plugin:badExecutorBadImplPath has an invalid implementation path + json.executors[badExecutorBadImplPath].implementation = + './executors/bad-path'; // @proj/plugin:non-existant has a missing implementation path amd schema json.executors['non-existant-executor'] = {}; return JSON.stringify(json); @@ -249,7 +244,7 @@ describe('Nx Plugin', () => { const results = runCLI(`lint ${plugin}`, { silenceError: true }); expect(results).toContain( - `${plugin}: Implementation path should point to a valid file` + `${badFactoryPath}: Implementation path should point to a valid file` ); expect(results).toContain( `non-existant-generator: Missing required property - \`schema\`` @@ -260,7 +255,7 @@ describe('Nx Plugin', () => { expect(results).not.toContain(goodGenerator); expect(results).toContain( - `build: Implementation path should point to a valid file` + `${badExecutorBadImplPath}: Implementation path should point to a valid file` ); expect(results).toContain( `non-existant-executor: Missing required property - \`schema\`` @@ -392,7 +387,7 @@ describe('Nx Plugin', () => { it('should create a plugin in the specified directory', () => { const plugin = uniq('plugin'); runCLI( - `generate @nrwl/nx-plugin:plugin ${plugin} --linter=eslint --directory subdir ` + `generate @nrwl/nx-plugin:plugin ${plugin} --linter=eslint --directory subdir --e2eTestRunner=jest` ); checkFilesExist(`libs/subdir/${plugin}/package.json`); const pluginProject = readProjectConfig(`subdir-${plugin}`); diff --git a/packages/nx-plugin/generators.json b/packages/nx-plugin/generators.json index 31c8ebbe99d89b..efd54834e676d2 100644 --- a/packages/nx-plugin/generators.json +++ b/packages/nx-plugin/generators.json @@ -11,8 +11,7 @@ "e2e-project": { "factory": "./src/generators/e2e-project/e2e", "schema": "./src/generators/e2e-project/schema.json", - "description": "Create a E2E application for a Nx Plugin.", - "hidden": true + "description": "Create a E2E application for a Nx Plugin." }, "migration": { "factory": "./src/generators/migration/migration", @@ -51,8 +50,7 @@ "e2e-project": { "factory": "./src/generators/e2e-project/e2e#e2eProjectSchematic", "schema": "./src/generators/e2e-project/schema.json", - "description": "Create a E2E application for a Nx Plugin.", - "hidden": true + "description": "Create a E2E application for a Nx Plugin." }, "migration": { "factory": "./src/generators/migration/migration#migrationSchematic", 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 4fd839f2dddcfa..7cb0fd39696058 100644 --- a/packages/nx-plugin/src/generators/e2e-project/e2e.spec.ts +++ b/packages/nx-plugin/src/generators/e2e-project/e2e.spec.ts @@ -140,24 +140,6 @@ describe('NxPlugin e2e-project Generator', () => { 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', - 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 "); - }); - it('should setup the eslint builder', async () => { await e2eProjectGenerator(tree, { pluginName: 'my-plugin', diff --git a/packages/nx-plugin/src/generators/e2e-project/e2e.ts b/packages/nx-plugin/src/generators/e2e-project/e2e.ts index ab576298a36f96..6e42ed3e5790f5 100644 --- a/packages/nx-plugin/src/generators/e2e-project/e2e.ts +++ b/packages/nx-plugin/src/generators/e2e-project/e2e.ts @@ -46,7 +46,6 @@ function normalizeOptions(host: Tree, options: Schema): NormalizedSchema { return { ...options, - minimal: options.minimal ?? false, projectName, linter: options.linter ?? Linter.EsLint, pluginPropertyName, 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 e4c62d874ea8d2..8927e918431316 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__ @@ -3,6 +3,7 @@ import { ensureNxProject, readJson, runNxCommandAsync, + runNxCommand, uniq, } from '@nrwl/nx-plugin/testing'; @@ -23,39 +24,14 @@ describe('<%= pluginName %> e2e', () => { runNxCommandAsync('reset'); }); - <% if(!minimal) { %> - it('should create <%= pluginName %>', async () => { - const project = uniq('<%= pluginName %>'); - await runNxCommandAsync( - `generate <%=npmPackageName%>:<%= pluginName %> ${project}` - ); - const result = await runNxCommandAsync(`build ${project}`); - expect(result.stdout).toContain('Executor ran'); - }, 120000); - - describe('--directory', () => { - it('should create src in the specified directory', async () => { - const project = uniq('<%= pluginName %>'); - await runNxCommandAsync( - `generate <%=npmPackageName%>:<%= pluginName %> ${project} --directory subdir` - ); - expect(() => - checkFilesExist(`libs/subdir/${project}/src/index.ts`) - ).not.toThrow(); - }, 120000); - }); - - describe('--tags', () => { - it('should add tags to the project', async () => { - const projectName = uniq('<%= pluginName %>'); - ensureNxProject('<%= npmPackageName %>', '<%= pluginOutputPath %>'); - await runNxCommandAsync( - `generate <%=npmPackageName%>:<%= pluginName %> ${projectName} --tags e2etag,e2ePackage` - ); - const project = readJson(`libs/${projectName}/project.json`); - expect(project.tags).toEqual(['e2etag', 'e2ePackage']); - }, 120000); - }); - <% } %> + // Add some tests here to check that your plugin functionality works as expected. + // A sample test is included below to give you some ideas. + // + // it('should be able to build generated projects', async () => { + // const name = uniq('proj') + // await runNxCommandAsync(`generate <%= npmPackageName %>:{my-generator} --name ${name}`); + // expect(() => runNxCommand('build ${proj}')).not.toThrow(); + // expect(() => checkFilesExist([`dist/${name}/index.js`])).not.toThrow(); + // }); }); 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 f9c0f5cbaac1ee..9efea9254b4dc9 100644 --- a/packages/nx-plugin/src/generators/e2e-project/schema.d.ts +++ b/packages/nx-plugin/src/generators/e2e-project/schema.d.ts @@ -6,7 +6,6 @@ export interface Schema { projectDirectory?: string; pluginOutputPath?: string; jestConfig?: string; - minimal?: boolean; linter?: Linter; skipFormat?: boolean; rootProject?: boolean; diff --git a/packages/nx-plugin/src/generators/executor/executor.ts b/packages/nx-plugin/src/generators/executor/executor.ts index 5b7b5209402a50..749e2a78c83d19 100644 --- a/packages/nx-plugin/src/generators/executor/executor.ts +++ b/packages/nx-plugin/src/generators/executor/executor.ts @@ -14,6 +14,7 @@ import type { Tree } from '@nrwl/devkit'; import type { Schema } from './schema'; import * as path from 'path'; import { PackageJson } from 'nx/src/utils/package-json'; +import pluginLintCheckGenerator from '../lint-checks/generator'; interface NormalizedSchema extends Schema { fileName: string; @@ -70,10 +71,15 @@ function addHasherFiles(host: Tree, options: NormalizedSchema) { } } -function createExecutorsJson(host: Tree, options: NormalizedSchema) { +export async function createExecutorsJson( + host: Tree, + projectRoot: string, + projectName: string, + skipLintChecks?: boolean +) { updateJson( host, - joinPathFragments(options.projectRoot, 'package.json'), + joinPathFragments(projectRoot, 'package.json'), (json) => { json.executors ??= './executors.json'; return json; @@ -81,18 +87,24 @@ function createExecutorsJson(host: Tree, options: NormalizedSchema) { ); writeJson( host, - joinPathFragments(options.projectRoot, 'executors.json'), + joinPathFragments(projectRoot, 'executors.json'), { executors: {}, } ); + if (!skipLintChecks) { + await pluginLintCheckGenerator(host, { + projectName, + }); + } } -function updateExecutorJson(host: Tree, options: NormalizedSchema) { +async function updateExecutorJson(host: Tree, options: NormalizedSchema) { const packageJson = readJson( host, joinPathFragments(options.projectRoot, 'package.json') ); + const packageJsonExecutors = packageJson.executors ?? packageJson.builders; let executorsPath = packageJsonExecutors ? joinPathFragments(options.projectRoot, packageJsonExecutors) @@ -102,7 +114,12 @@ function updateExecutorJson(host: Tree, options: NormalizedSchema) { executorsPath = joinPathFragments(options.projectRoot, 'executors.json'); } if (!host.exists(executorsPath)) { - createExecutorsJson(host, options); + await createExecutorsJson( + host, + options.projectRoot, + options.project, + options.skipLintChecks + ); } return updateJson(host, executorsPath, (json) => { @@ -158,7 +175,7 @@ export async function executorGenerator(host: Tree, schema: Schema) { addHasherFiles(host, options); } - updateExecutorJson(host, options); + await updateExecutorJson(host, options); } export default executorGenerator; diff --git a/packages/nx-plugin/src/generators/executor/schema.d.ts b/packages/nx-plugin/src/generators/executor/schema.d.ts index 7b67bda9197de6..6946c85d5bbba4 100644 --- a/packages/nx-plugin/src/generators/executor/schema.d.ts +++ b/packages/nx-plugin/src/generators/executor/schema.d.ts @@ -4,4 +4,5 @@ export interface Schema { description?: string; unitTestRunner: 'jest' | 'none'; includeHasher: boolean; + skipLintChecks?: boolean; } diff --git a/packages/nx-plugin/src/generators/executor/schema.json b/packages/nx-plugin/src/generators/executor/schema.json index 79050a4d5f723a..fb89cc3a8c5d97 100644 --- a/packages/nx-plugin/src/generators/executor/schema.json +++ b/packages/nx-plugin/src/generators/executor/schema.json @@ -46,6 +46,11 @@ "type": "boolean", "default": false, "description": "Should the boilerplate for a custom hasher be generated?" + }, + "skipLintChecks": { + "type": "boolean", + "default": false, + "description": "Do not eslint configuration for plugin json files." } }, "required": ["project", "name"], diff --git a/packages/nx-plugin/src/generators/generator/generator.spec.ts b/packages/nx-plugin/src/generators/generator/generator.spec.ts index 0ef4a1e40098b5..a5be5b24a59fee 100644 --- a/packages/nx-plugin/src/generators/generator/generator.spec.ts +++ b/packages/nx-plugin/src/generators/generator/generator.spec.ts @@ -179,8 +179,6 @@ describe('NxPlugin Generator Generator', () => { 'libs/my-plugin/generators.json' ); - console.log(generatorJson.generators['preset']); - expect( generatorJson.generators['preset']['x-use-standalone-layout'] ).toEqual(true); diff --git a/packages/nx-plugin/src/generators/generator/generator.ts b/packages/nx-plugin/src/generators/generator/generator.ts index f3205ec67753f2..3f8541513a8f25 100644 --- a/packages/nx-plugin/src/generators/generator/generator.ts +++ b/packages/nx-plugin/src/generators/generator/generator.ts @@ -15,6 +15,7 @@ import { } from '@nrwl/devkit'; import { PackageJson } from 'nx/src/utils/package-json'; import * as path from 'path'; +import pluginLintCheckGenerator from '../lint-checks/generator'; import type { Schema } from './schema'; interface NormalizedSchema extends Schema { @@ -86,10 +87,15 @@ function addFiles(host: Tree, options: NormalizedSchema) { } } -function createGeneratorsJson(host: Tree, options: NormalizedSchema) { +export async function createGeneratorsJson( + host: Tree, + projectRoot: string, + projectName: string, + skipLintChecks?: boolean +) { updateJson( host, - joinPathFragments(options.projectRoot, 'package.json'), + joinPathFragments(projectRoot, 'package.json'), (json) => { json.generators ??= './generators.json'; return json; @@ -97,14 +103,19 @@ function createGeneratorsJson(host: Tree, options: NormalizedSchema) { ); writeJson( host, - joinPathFragments(options.projectRoot, 'generators.json'), + joinPathFragments(projectRoot, 'generators.json'), { generators: {}, } ); + if (!skipLintChecks) { + await pluginLintCheckGenerator(host, { + projectName, + }); + } } -function updateGeneratorJson(host: Tree, options: NormalizedSchema) { +async function updateGeneratorJson(host: Tree, options: NormalizedSchema) { const packageJson = readJson( host, joinPathFragments(options.projectRoot, 'package.json') @@ -119,7 +130,12 @@ function updateGeneratorJson(host: Tree, options: NormalizedSchema) { generatorsPath = joinPathFragments(options.projectRoot, 'generators.json'); } if (!host.exists(generatorsPath)) { - createGeneratorsJson(host, options); + await createGeneratorsJson( + host, + options.projectRoot, + options.project, + options.skipLintChecks + ); } return updateJson(host, generatorsPath, (json) => { @@ -145,7 +161,7 @@ export async function generatorGenerator(host: Tree, schema: Schema) { addFiles(host, options); - updateGeneratorJson(host, options); + await updateGeneratorJson(host, options); } export default generatorGenerator; diff --git a/packages/nx-plugin/src/generators/generator/schema.d.ts b/packages/nx-plugin/src/generators/generator/schema.d.ts index c0088067da3af8..de575df6129595 100644 --- a/packages/nx-plugin/src/generators/generator/schema.d.ts +++ b/packages/nx-plugin/src/generators/generator/schema.d.ts @@ -3,4 +3,5 @@ export interface Schema { name: string; description?: string; unitTestRunner: 'jest' | 'none'; + skipLintChecks?: boolean; } diff --git a/packages/nx-plugin/src/generators/generator/schema.json b/packages/nx-plugin/src/generators/generator/schema.json index 046a5dc14ebd79..d7a945ee8d5db4 100644 --- a/packages/nx-plugin/src/generators/generator/schema.json +++ b/packages/nx-plugin/src/generators/generator/schema.json @@ -42,6 +42,11 @@ "enum": ["jest", "none"], "description": "Test runner to use for unit tests.", "default": "jest" + }, + "skipLintChecks": { + "type": "boolean", + "default": false, + "description": "Do not eslint configuration for plugin json files." } }, "required": ["project", "name"], diff --git a/packages/nx-plugin/src/generators/lint-checks/generator.spec.ts b/packages/nx-plugin/src/generators/lint-checks/generator.spec.ts index bb54ee9009e61f..36e9fc5b8e7359 100644 --- a/packages/nx-plugin/src/generators/lint-checks/generator.spec.ts +++ b/packages/nx-plugin/src/generators/lint-checks/generator.spec.ts @@ -13,15 +13,17 @@ import { Schema as EsLintExecutorOptions } from '@nrwl/linter/src/executors/esli import generator from './generator'; import pluginGenerator from '../plugin/plugin'; +import generatorGenerator from '../generator/generator'; +import executorGenerator from '../executor/executor'; import { Linter } from '@nrwl/linter'; import { PackageJson } from 'nx/src/utils/package-json'; describe('lint-checks generator', () => { - let appTree: Tree; + let tree: Tree; beforeEach(async () => { - appTree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); - await pluginGenerator(appTree, { + tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); + await pluginGenerator(tree, { name: 'plugin', importPath: '@acme/plugin', compiler: 'tsc', @@ -31,14 +33,28 @@ describe('lint-checks generator', () => { skipLintChecks: true, // we manually call it s.t. we can update config files first unitTestRunner: 'jest', }); + await generatorGenerator(tree, { + name: 'my-generator', + project: 'plugin', + unitTestRunner: 'jest', + skipLintChecks: true, + }); + await executorGenerator(tree, { + name: 'my-executor', + project: 'plugin', + unitTestRunner: 'jest', + includeHasher: false, + skipLintChecks: true, + }); }); it('should update configuration files for default plugin', async () => { - await generator(appTree, { projectName: 'plugin' }); - const projectConfig = readProjectConfiguration(appTree, 'plugin'); + await generator(tree, { projectName: 'plugin' }); + + const projectConfig = readProjectConfiguration(tree, 'plugin'); const targetConfig = projectConfig.targets?.['lint']; const eslintConfig: ESLint.Config = readJson( - appTree, + tree, `${projectConfig.root}/.eslintrc.json` ); @@ -66,13 +82,13 @@ describe('lint-checks generator', () => { }); it('should not duplicate configuration', async () => { - await generator(appTree, { projectName: 'plugin' }); - await generator(appTree, { projectName: 'plugin' }); - const projectConfig = readProjectConfiguration(appTree, 'plugin'); + await generator(tree, { projectName: 'plugin' }); + await generator(tree, { projectName: 'plugin' }); + const projectConfig = readProjectConfiguration(tree, 'plugin'); const targetConfig = projectConfig.targets?.['lint'] .options as EsLintExecutorOptions; const eslintConfig: ESLint.Config = readJson( - appTree, + tree, `${projectConfig.root}/.eslintrc.json` ); @@ -90,9 +106,9 @@ describe('lint-checks generator', () => { }); it('should update configuration files for angular-style plugin', async () => { - const startingProjectConfig = readProjectConfiguration(appTree, 'plugin'); + const startingProjectConfig = readProjectConfiguration(tree, 'plugin'); updateJson( - appTree, + tree, joinPathFragments(startingProjectConfig.root, 'package.json'), (json: PackageJson) => { json.schematics = './collection.json'; @@ -104,15 +120,15 @@ describe('lint-checks generator', () => { } ); writeJson( - appTree, + tree, joinPathFragments(startingProjectConfig.root, 'migrations.json'), {} ); - await generator(appTree, { projectName: 'plugin' }); - const projectConfig = readProjectConfiguration(appTree, 'plugin'); + await generator(tree, { projectName: 'plugin' }); + const projectConfig = readProjectConfiguration(tree, 'plugin'); const targetConfig = projectConfig.targets?.['lint']; const eslintConfig: ESLint.Config = readJson( - appTree, + tree, `${projectConfig.root}/.eslintrc.json` ); diff --git a/packages/nx-plugin/src/generators/migration/schema.json b/packages/nx-plugin/src/generators/migration/schema.json index 5c4b82f2ca4266..73c4282cc4636f 100644 --- a/packages/nx-plugin/src/generators/migration/schema.json +++ b/packages/nx-plugin/src/generators/migration/schema.json @@ -47,6 +47,11 @@ "description": "Whether or not to include `package.json` updates.", "alias": "p", "default": false + }, + "skipLintChecks": { + "type": "boolean", + "default": false, + "description": "Do not eslint configuration for plugin json files." } }, "required": ["project", "packageVersion"], diff --git a/packages/nx-plugin/src/generators/plugin/plugin.spec.ts b/packages/nx-plugin/src/generators/plugin/plugin.spec.ts index 964adc85bd9ff2..5db4fa9aed5d5c 100644 --- a/packages/nx-plugin/src/generators/plugin/plugin.spec.ts +++ b/packages/nx-plugin/src/generators/plugin/plugin.spec.ts @@ -30,7 +30,7 @@ describe('NxPlugin Plugin Generator', () => { tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); }); - it('should update the workspace.json file', async () => { + it('should update the project configuration', async () => { await pluginGenerator(tree, getSchema()); const project = readProjectConfiguration(tree, 'my-plugin'); expect(project.root).toEqual('libs/my-plugin'); @@ -72,9 +72,7 @@ describe('NxPlugin Plugin Generator', () => { options: { lintFilePatterns: expect.arrayContaining([ 'libs/my-plugin/**/*.ts', - 'libs/my-plugin/generators.json', 'libs/my-plugin/package.json', - 'libs/my-plugin/executors.json', ]), }, }); @@ -108,52 +106,6 @@ describe('NxPlugin Plugin Generator', () => { expect(projectE2e.root).toEqual('apps/plugins/my-plugin-e2e'); }); - it('should create schematic and builder files', async () => { - await pluginGenerator(tree, getSchema({ name: 'myPlugin' })); - - [ - 'libs/my-plugin/project.json', - 'libs/my-plugin/generators.json', - 'libs/my-plugin/executors.json', - '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)).toBeTruthy()); - - expect( - tree.read( - 'libs/my-plugin/src/generators/my-plugin/files/src/index.ts__template__', - 'utf-8' - ) - ).toContain('const variable = "<%= projectName %>";'); - }); - - it('should not create generator and executor files for minimal setups', async () => { - await pluginGenerator(tree, getSchema({ name: 'myPlugin', minimal: true })); - - expect(tree.exists('libs/my-plugin/project.json')).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()); - }); - describe('--unitTestRunner', () => { describe('none', () => { it('should not generate test files', async () => { @@ -165,15 +117,13 @@ describe('NxPlugin Plugin Generator', () => { }) ); - [ - 'libs/my-plugin/src/generators/my-plugin/generator.ts', - 'libs/my-plugin/src/executors/build/executor.ts', - ].forEach((path) => expect(tree.exists(path)).toBeTruthy()); + ['libs/my-plugin/jest.config.ts'].forEach((path) => + expect(tree.exists(path)).toBeFalsy() + ); - [ - 'libs/my-plugin/src/generators/my-plugin/generator.spec.ts', - 'libs/my-plugin/src/executors/build/executor.spec.ts', - ].forEach((path) => expect(tree.exists(path)).toBeFalsy()); + expect( + readProjectConfiguration(tree, 'my-plugin').targets.test + ).not.toBeDefined(); }); }); }); diff --git a/packages/nx-plugin/src/generators/plugin/plugin.ts b/packages/nx-plugin/src/generators/plugin/plugin.ts index 6853eb0e8ad3b0..85718872a4a598 100644 --- a/packages/nx-plugin/src/generators/plugin/plugin.ts +++ b/packages/nx-plugin/src/generators/plugin/plugin.ts @@ -36,21 +36,6 @@ async function addFiles(host: Tree, options: NormalizedSchema) { tmpl: '', } ); - - if (options.minimal) { - return; - } - await generatorGenerator(host, { - project: options.name, - name: options.name, - unitTestRunner: options.unitTestRunner, - }); - await executorGenerator(host, { - project: options.name, - name: 'build', - unitTestRunner: options.unitTestRunner, - includeHasher: false, - }); } function updatePluginConfig(host: Tree, options: NormalizedSchema) { @@ -124,7 +109,6 @@ export async function pluginGenerator(host: Tree, schema: Schema) { projectDirectory: options.projectDirectory, pluginOutputPath: `dist/${options.libsDir}/${options.projectDirectory}`, npmPackageName: options.npmPackageName, - minimal: options.minimal ?? false, skipFormat: true, rootProject: options.rootProject, }); diff --git a/packages/nx-plugin/src/generators/plugin/schema.d.ts b/packages/nx-plugin/src/generators/plugin/schema.d.ts index 2596b29095cd07..9a3c87585d51b1 100644 --- a/packages/nx-plugin/src/generators/plugin/schema.d.ts +++ b/packages/nx-plugin/src/generators/plugin/schema.d.ts @@ -13,6 +13,5 @@ export interface Schema { linter: Linter; setParserOptionsProject?: boolean; compiler: 'swc' | 'tsc'; - minimal?: boolean; rootProject?: boolean; } diff --git a/packages/nx-plugin/src/generators/plugin/schema.json b/packages/nx-plugin/src/generators/plugin/schema.json index e3fa9902bc4bb6..f8ef7fadfa248d 100644 --- a/packages/nx-plugin/src/generators/plugin/schema.json +++ b/packages/nx-plugin/src/generators/plugin/schema.json @@ -69,7 +69,7 @@ "type": "string", "enum": ["jest", "none"], "description": "Test runner to use for end to end (E2E) tests.", - "default": "jest" + "default": "none" }, "standaloneConfig": { "description": "Split the project configuration into `/project.json` rather than including it inside `workspace.json`.", @@ -87,13 +87,7 @@ "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"], - "additionalProperties": false + "required": ["name"] } diff --git a/packages/nx-plugin/src/generators/preset/generator.ts b/packages/nx-plugin/src/generators/preset/generator.ts index 066746ac9b2b68..b0c8573302ce83 100644 --- a/packages/nx-plugin/src/generators/preset/generator.ts +++ b/packages/nx-plugin/src/generators/preset/generator.ts @@ -1,13 +1,14 @@ import { Tree, - readJson, - joinPathFragments, updateJson, updateNxJson, readNxJson, + readProjectConfiguration, } from '@nrwl/devkit'; import { Linter } from '@nrwl/linter'; import { PackageJson } from 'nx/src/utils/package-json'; +import { createExecutorsJson } from '../executor/executor'; +import { createGeneratorsJson } from '../generator/generator'; import { pluginGenerator } from '../plugin/plugin'; import { PresetGeneratorSchema } from './schema'; @@ -24,8 +25,13 @@ export default async function (tree: Tree, options: PresetGeneratorSchema) { unitTestRunner: 'jest', importPath: options.pluginName, rootProject: true, + e2eTestRunner: 'jest', }); + const { root } = readProjectConfiguration(tree, options.pluginName); + await createExecutorsJson(tree, root, options.pluginName); + await createGeneratorsJson(tree, root, options.pluginName); + removeNpmScope(tree); moveNxPluginToDevDeps(tree); diff --git a/packages/nx-plugin/src/migrations/update-15-0-0/specify-output-capture.spec.ts b/packages/nx-plugin/src/migrations/update-15-0-0/specify-output-capture.spec.ts index 81f9736bbe6b68..80f3c7fec0882a 100644 --- a/packages/nx-plugin/src/migrations/update-15-0-0/specify-output-capture.spec.ts +++ b/packages/nx-plugin/src/migrations/update-15-0-0/specify-output-capture.spec.ts @@ -2,12 +2,13 @@ import { readJson, updateJson } from '@nrwl/devkit'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; import { Linter } from '@nrwl/linter'; import { ExecutorConfig } from 'nx/src/config/misc-interfaces'; +import executorGenerator from '../../generators/executor/executor'; import pluginGenerator from '../../generators/plugin/plugin'; import update from './specify-output-capture'; const schemaPath = `libs/plugin/src/executors/build/schema.json`; -describe('update-14-2-0-split-create-empty-tree', () => { +describe('update-15-0-0-specify-output-capture', () => { it('should not change outputCapture if already present', async () => { const { tree } = await createTreeWithBoilerplate(); updateJson(tree, schemaPath, (json) => { @@ -62,5 +63,11 @@ async function createTreeWithBoilerplate() { skipTsConfig: false, unitTestRunner: 'jest', }); + await executorGenerator(tree, { + name: 'build', + project: 'plugin', + unitTestRunner: 'jest', + includeHasher: false, + }); return { tree }; }