|
1 |
| -import { |
2 |
| - createProjectGraphAsync, |
3 |
| - readProjectsConfigurationFromProjectGraph, |
4 |
| -} from '@nx/devkit'; |
5 |
| -import type { ESLint } from 'eslint'; |
| 1 | +import { createProjectGraphAsync } from '@nx/devkit'; |
6 | 2 | import type { ESLintPluginConfig } from '../config';
|
7 |
| -import { |
8 |
| - findCodePushupEslintrc, |
9 |
| - getEslintConfig, |
10 |
| - getLintFilePatterns, |
11 |
| -} from './utils'; |
| 3 | +import { nxProjectsToConfig } from './projects-to-config'; |
12 | 4 |
|
| 5 | +/** |
| 6 | + * Finds all Nx projects in workspace and converts their lint configurations to Code PushUp ESLint plugin parameters. |
| 7 | + * |
| 8 | + * Use when you wish to automatically include every Nx project in a single Code PushUp project. |
| 9 | + * If you prefer to only include a subset of your Nx monorepo, refer to {@link eslintConfigFromNxProject} instead. |
| 10 | + * |
| 11 | + * @example |
| 12 | + * import eslintPlugin, { |
| 13 | + * eslintConfigFromNxProjects, |
| 14 | + * } from '@code-pushup/eslint-plugin'; |
| 15 | + * |
| 16 | + * export default { |
| 17 | + * plugins: [ |
| 18 | + * await eslintPlugin( |
| 19 | + * await eslintConfigFromNxProjects() |
| 20 | + * ) |
| 21 | + * ] |
| 22 | + * } |
| 23 | + * |
| 24 | + * @returns ESLint config and patterns, intended to be passed to {@link eslintPlugin} |
| 25 | + */ |
13 | 26 | export async function eslintConfigFromNxProjects(): Promise<ESLintPluginConfig> {
|
14 |
| - // find Nx projects with lint target |
15 | 27 | const projectGraph = await createProjectGraphAsync({ exitOnError: false });
|
16 |
| - const projectsConfiguration = |
17 |
| - readProjectsConfigurationFromProjectGraph(projectGraph); |
18 |
| - const projects = Object.values(projectsConfiguration.projects) |
19 |
| - .filter(project => 'lint' in (project.targets ?? {})) |
20 |
| - .sort((a, b) => a.root.localeCompare(b.root)); |
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}/*.spec.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 |
| - }; |
| 28 | + return nxProjectsToConfig(projectGraph); |
50 | 29 | }
|
0 commit comments