|
| 1 | +import { |
| 2 | + createProjectGraphAsync, |
| 3 | + readProjectsConfigurationFromProjectGraph, |
| 4 | +} from '@nx/devkit'; |
| 5 | +import type { ESLint } from 'eslint'; |
| 6 | +import type { ESLintPluginConfig } from '../config'; |
| 7 | +import { |
| 8 | + findCodePushupEslintrc, |
| 9 | + getEslintConfig, |
| 10 | + getLintFilePatterns, |
| 11 | +} from './utils'; |
| 12 | + |
| 13 | +export async function eslintConfigFromNxProjects(): Promise<ESLintPluginConfig> { |
| 14 | + // find Nx projects with lint target |
| 15 | + const projectGraph = await createProjectGraphAsync({ exitOnError: false }); |
| 16 | + const projectsConfiguration = |
| 17 | + readProjectsConfigurationFromProjectGraph(projectGraph); |
| 18 | + const projects = Object.values(projectsConfiguration.projects).filter( |
| 19 | + project => 'lint' in (project.targets ?? {}), |
| 20 | + ); |
| 21 | + |
| 22 | + // create single ESLint config with project-specific overrides |
| 23 | + const eslintConfig: ESLint.ConfigData = { |
| 24 | + root: true, |
| 25 | + overrides: await Promise.all( |
| 26 | + projects.map(async project => ({ |
| 27 | + files: getLintFilePatterns(project), |
| 28 | + extends: |
| 29 | + (await findCodePushupEslintrc(project)) ?? getEslintConfig(project), |
| 30 | + })), |
| 31 | + ), |
| 32 | + }; |
| 33 | + |
| 34 | + // include patterns from each project |
| 35 | + const patterns = projects.flatMap(project => [ |
| 36 | + ...getLintFilePatterns(project), |
| 37 | + // HACK: ESLint.calculateConfigForFile won't find rules included only for subsets of *.ts when globs used |
| 38 | + // so we explicitly provide additional patterns used by @code-pushup/eslint-config to ensure those rules are included |
| 39 | + // this workaround won't be necessary once flat configs are stable (much easier to find all rules) |
| 40 | + `${project.sourceRoot}/*.test.ts`, // jest/* and vitest/* rules |
| 41 | + `${project.sourceRoot}/*.cy.ts`, // cypress/* rules |
| 42 | + `${project.sourceRoot}/*.stories.ts`, // storybook/* rules |
| 43 | + `${project.sourceRoot}/.storybook/main.ts`, // storybook/no-uninstalled-addons rule |
| 44 | + ]); |
| 45 | + |
| 46 | + return { |
| 47 | + eslintrc: eslintConfig, |
| 48 | + patterns, |
| 49 | + }; |
| 50 | +} |
0 commit comments