feat(nx-plugin): add lint option for the e2e generator (#15140)

This commit is contained in:
Dmitriy Stepanenko 2023-02-21 00:00:14 +02:00 committed by GitHub
parent 576bec2553
commit b8e66790a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 1 deletions

View File

@ -35,6 +35,12 @@
"default": true, "default": true,
"x-deprecated": "Nx only supports standaloneConfig" "x-deprecated": "Nx only supports standaloneConfig"
}, },
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
"enum": ["eslint", "none"],
"default": "eslint"
},
"minimal": { "minimal": {
"type": "boolean", "type": "boolean",
"description": "Generate the e2e project with a minimal setup. This would involve not generating tests for a default executor and generator.", "description": "Generate the e2e project with a minimal setup. This would involve not generating tests for a default executor and generator.",

View File

@ -28,7 +28,7 @@ describe('NxPlugin e2e-project Generator', () => {
pluginOutputPath: `dist/libs/my-plugin`, pluginOutputPath: `dist/libs/my-plugin`,
npmPackageName: '@proj/my-plugin', npmPackageName: '@proj/my-plugin',
}) })
).resolves.not.toThrow(); ).resolves.toBeDefined();
await expect( await expect(
e2eProjectGenerator(tree, { e2eProjectGenerator(tree, {
@ -157,4 +157,21 @@ describe('NxPlugin e2e-project Generator', () => {
tree.read(joinPathFragments(root, 'tests/my-plugin.spec.ts'), 'utf-8') tree.read(joinPathFragments(root, 'tests/my-plugin.spec.ts'), 'utf-8')
).not.toContain("it('should create "); ).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'],
},
});
});
}); });

View File

@ -4,6 +4,7 @@ import {
extractLayoutDirectory, extractLayoutDirectory,
formatFiles, formatFiles,
generateFiles, generateFiles,
GeneratorCallback,
getWorkspaceLayout, getWorkspaceLayout,
joinPathFragments, joinPathFragments,
names, names,
@ -16,12 +17,15 @@ import { getRelativePathToRootTsConfig } from '@nrwl/js';
import * as path from 'path'; import * as path from 'path';
import type { Schema } from './schema'; 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 { interface NormalizedSchema extends Schema {
projectRoot: string; projectRoot: string;
projectName: string; projectName: string;
pluginPropertyName: string; pluginPropertyName: string;
npmScope: string; npmScope: string;
linter: Linter;
} }
function normalizeOptions(host: Tree, options: Schema): NormalizedSchema { function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
@ -41,6 +45,7 @@ function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
...options, ...options,
minimal: options.minimal ?? false, minimal: options.minimal ?? false,
projectName, projectName,
linter: options.linter ?? Linter.EsLint,
pluginPropertyName, pluginPropertyName,
projectRoot, projectRoot,
npmScope, npmScope,
@ -101,14 +106,44 @@ async function addJest(host: Tree, options: NormalizedSchema) {
updateProjectConfiguration(host, options.projectName, project); updateProjectConfiguration(host, options.projectName, project);
} }
async function addLintingToApplication(
tree: Tree,
options: NormalizedSchema
): Promise<GeneratorCallback> {
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) { export async function e2eProjectGenerator(host: Tree, schema: Schema) {
const tasks: GeneratorCallback[] = [];
validatePlugin(host, schema.pluginName); validatePlugin(host, schema.pluginName);
const options = normalizeOptions(host, schema); const options = normalizeOptions(host, schema);
addFiles(host, options); addFiles(host, options);
updateWorkspaceConfiguration(host, options); updateWorkspaceConfiguration(host, options);
await addJest(host, options); await addJest(host, options);
if (options.linter !== Linter.None) {
const lintTask = await addLintingToApplication(host, {
...options,
});
tasks.push(lintTask);
}
await formatFiles(host); await formatFiles(host);
return runTasksInSerial(...tasks);
} }
export default e2eProjectGenerator; export default e2eProjectGenerator;

View File

@ -1,3 +1,5 @@
import { Linter } from '@nrwl/linter';
export interface Schema { export interface Schema {
pluginName: string; pluginName: string;
npmPackageName: string; npmPackageName: string;
@ -5,4 +7,5 @@ export interface Schema {
pluginOutputPath?: string; pluginOutputPath?: string;
jestConfig?: string; jestConfig?: string;
minimal?: boolean; minimal?: boolean;
linter?: Linter;
} }

View File

@ -35,6 +35,12 @@
"default": true, "default": true,
"x-deprecated": "Nx only supports standaloneConfig" "x-deprecated": "Nx only supports standaloneConfig"
}, },
"linter": {
"description": "The tool to use for running lint checks.",
"type": "string",
"enum": ["eslint", "none"],
"default": "eslint"
},
"minimal": { "minimal": {
"type": "boolean", "type": "boolean",
"description": "Generate the e2e project with a minimal setup. This would involve not generating tests for a default executor and generator.", "description": "Generate the e2e project with a minimal setup. This would involve not generating tests for a default executor and generator.",