From b8e66790a6ff6b93b81b8b59d661ba64e6574c1b Mon Sep 17 00:00:00 2001 From: Dmitriy Stepanenko <33101123+dmitry-stepanenko@users.noreply.github.com> Date: Tue, 21 Feb 2023 00:00:14 +0200 Subject: [PATCH] feat(nx-plugin): add lint option for the e2e generator (#15140) --- .../nx-plugin/generators/e2e-project.json | 6 ++++ .../src/generators/e2e-project/e2e.spec.ts | 19 +++++++++- .../src/generators/e2e-project/e2e.ts | 35 +++++++++++++++++++ .../src/generators/e2e-project/schema.d.ts | 3 ++ .../src/generators/e2e-project/schema.json | 6 ++++ 5 files changed, 68 insertions(+), 1 deletion(-) diff --git a/docs/generated/packages/nx-plugin/generators/e2e-project.json b/docs/generated/packages/nx-plugin/generators/e2e-project.json index f7a69308e23b9..7e117bad789ad 100644 --- a/docs/generated/packages/nx-plugin/generators/e2e-project.json +++ b/docs/generated/packages/nx-plugin/generators/e2e-project.json @@ -35,6 +35,12 @@ "default": true, "x-deprecated": "Nx only supports standaloneConfig" }, + "linter": { + "description": "The tool to use for running lint checks.", + "type": "string", + "enum": ["eslint", "none"], + "default": "eslint" + }, "minimal": { "type": "boolean", "description": "Generate the e2e project with a minimal setup. This would involve not generating tests for a default executor and generator.", 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 91248b0b8e2aa..4fd839f2dddcf 100644 --- a/packages/nx-plugin/src/generators/e2e-project/e2e.spec.ts +++ b/packages/nx-plugin/src/generators/e2e-project/e2e.spec.ts @@ -28,7 +28,7 @@ describe('NxPlugin e2e-project Generator', () => { pluginOutputPath: `dist/libs/my-plugin`, npmPackageName: '@proj/my-plugin', }) - ).resolves.not.toThrow(); + ).resolves.toBeDefined(); await expect( e2eProjectGenerator(tree, { @@ -157,4 +157,21 @@ describe('NxPlugin e2e-project Generator', () => { 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', + pluginOutputPath: `dist/libs/my-plugin`, + npmPackageName: '@proj/my-plugin', + }); + + const projectsConfigurations = getProjects(tree); + expect(projectsConfigurations.get('my-plugin-e2e').targets.lint).toEqual({ + executor: '@nrwl/linter:eslint', + outputs: ['{options.outputFile}'], + options: { + lintFilePatterns: ['apps/my-plugin-e2e/**/*.ts'], + }, + }); + }); }); diff --git a/packages/nx-plugin/src/generators/e2e-project/e2e.ts b/packages/nx-plugin/src/generators/e2e-project/e2e.ts index a1edf3183c171..e4b43f11f40c0 100644 --- a/packages/nx-plugin/src/generators/e2e-project/e2e.ts +++ b/packages/nx-plugin/src/generators/e2e-project/e2e.ts @@ -4,6 +4,7 @@ import { extractLayoutDirectory, formatFiles, generateFiles, + GeneratorCallback, getWorkspaceLayout, joinPathFragments, names, @@ -16,12 +17,15 @@ import { getRelativePathToRootTsConfig } from '@nrwl/js'; import * as path from 'path'; import type { Schema } from './schema'; +import { Linter, lintProjectGenerator } from '@nrwl/linter'; +import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial'; interface NormalizedSchema extends Schema { projectRoot: string; projectName: string; pluginPropertyName: string; npmScope: string; + linter: Linter; } function normalizeOptions(host: Tree, options: Schema): NormalizedSchema { @@ -41,6 +45,7 @@ function normalizeOptions(host: Tree, options: Schema): NormalizedSchema { ...options, minimal: options.minimal ?? false, projectName, + linter: options.linter ?? Linter.EsLint, pluginPropertyName, projectRoot, npmScope, @@ -101,14 +106,44 @@ async function addJest(host: Tree, options: NormalizedSchema) { updateProjectConfiguration(host, options.projectName, project); } +async function addLintingToApplication( + tree: Tree, + options: NormalizedSchema +): Promise { + const lintTask = await lintProjectGenerator(tree, { + linter: options.linter, + project: options.projectName, + tsConfigPaths: [ + joinPathFragments(options.projectRoot, 'tsconfig.app.json'), + ], + eslintFilePatterns: [`${options.projectRoot}/**/*.ts`], + unitTestRunner: 'jest', + skipFormat: true, + setParserOptionsProject: false, + }); + + return lintTask; +} + export async function e2eProjectGenerator(host: Tree, schema: Schema) { + const tasks: GeneratorCallback[] = []; + validatePlugin(host, schema.pluginName); const options = normalizeOptions(host, schema); addFiles(host, options); updateWorkspaceConfiguration(host, options); await addJest(host, options); + if (options.linter !== Linter.None) { + const lintTask = await addLintingToApplication(host, { + ...options, + }); + tasks.push(lintTask); + } + await formatFiles(host); + + return runTasksInSerial(...tasks); } export default e2eProjectGenerator; 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 6971d0171d675..2afbe5a0fc63b 100644 --- a/packages/nx-plugin/src/generators/e2e-project/schema.d.ts +++ b/packages/nx-plugin/src/generators/e2e-project/schema.d.ts @@ -1,3 +1,5 @@ +import { Linter } from '@nrwl/linter'; + export interface Schema { pluginName: string; npmPackageName: string; @@ -5,4 +7,5 @@ export interface Schema { pluginOutputPath?: string; jestConfig?: string; minimal?: boolean; + linter?: Linter; } diff --git a/packages/nx-plugin/src/generators/e2e-project/schema.json b/packages/nx-plugin/src/generators/e2e-project/schema.json index 56dc84612778d..fcc8a618a4255 100644 --- a/packages/nx-plugin/src/generators/e2e-project/schema.json +++ b/packages/nx-plugin/src/generators/e2e-project/schema.json @@ -35,6 +35,12 @@ "default": true, "x-deprecated": "Nx only supports standaloneConfig" }, + "linter": { + "description": "The tool to use for running lint checks.", + "type": "string", + "enum": ["eslint", "none"], + "default": "eslint" + }, "minimal": { "type": "boolean", "description": "Generate the e2e project with a minimal setup. This would involve not generating tests for a default executor and generator.",