Skip to content

Commit 0706bda

Browse files
committed
feat(plugin-eslint): add eslintConfigFromNxProject helper that doesn't include nx project deps
Signed-off-by: Vojtech Masek <vojtech@flowup.cz>
1 parent efbb72a commit 0706bda

File tree

5 files changed

+76
-3
lines changed

5 files changed

+76
-3
lines changed

packages/plugin-eslint/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export {
88
eslintConfigFromNxProjectAndDeps,
99
eslintConfigFromNxProjects,
1010
eslintConfigFromAllNxProjects,
11+
eslintConfigFromNxProject,
1112
} from './lib/nx';

packages/plugin-eslint/src/lib/nx.integration.test.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import {
77
eslintConfigFromAllNxProjects,
88
eslintConfigFromNxProjectAndDeps,
99
} from './nx';
10+
import { eslintConfigFromNxProject } from './nx/find-project-without-deps';
11+
12+
const ALL_PROJECTS = ['cli', 'core', 'nx-plugin', 'utils'] as const;
13+
type Project = (typeof ALL_PROJECTS)[number];
1014

1115
describe('Nx helpers', () => {
1216
let cwdSpy: MockInstance<[], string>;
@@ -101,9 +105,6 @@ describe('Nx helpers', () => {
101105
* utils ◄──────┘
102106
*/
103107

104-
const ALL_PROJECTS = ['cli', 'core', 'nx-plugin', 'utils'] as const;
105-
type Project = (typeof ALL_PROJECTS)[number];
106-
107108
it.each<[Project, Project[]]>([
108109
['cli', ['cli', 'core', 'utils']],
109110
['core', ['core', 'utils']],
@@ -125,4 +126,34 @@ describe('Nx helpers', () => {
125126
},
126127
);
127128
});
129+
130+
describe('create config from target Nx project without its dependencies', () => {
131+
/*
132+
* Project graph:
133+
*
134+
* cli
135+
* │
136+
* │
137+
* ▼
138+
* core
139+
* │ nx-plugin
140+
* │ │
141+
* ▼ │
142+
* utils ◄──────┘
143+
*/
144+
145+
it.each<[Project]>([['cli'], ['core'], ['utils']])(
146+
'project %j - expected configurations for projects %j',
147+
async project => {
148+
const targets = await eslintConfigFromNxProject(project);
149+
150+
expect(targets).toEqual([
151+
{
152+
eslintrc: `./packages/${project}/.eslintrc.json`,
153+
patterns: expect.arrayContaining([`packages/${project}/**/*.ts`]),
154+
},
155+
]);
156+
},
157+
);
158+
});
128159
});

packages/plugin-eslint/src/lib/nx/find-project-with-deps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { findAllDependencies } from './traverse-graph';
77
*
88
* Use when you wish to include a targeted subset of your Nx monorepo in your Code PushUp project.
99
* If you prefer to include all Nx projects, refer to {@link eslintConfigFromAllNxProjects} instead.
10+
* if you'd like to skip dependencies of the provided target project use {@link eslintConfigFromNxProject} instead.
1011
*
1112
* @example
1213
* import eslintPlugin, {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { ESLintTarget } from '../config';
2+
import { nxProjectsToConfig } from './projects-to-config';
3+
4+
/**
5+
* Accepts a target Nx projects, converts lint configurations to Code PushUp ESLint plugin parameters.
6+
*
7+
* Use when you wish to include a targeted subset of your Nx monorepo in your Code PushUp project.
8+
* If you prefer to include all Nx projects, refer to {@link eslintConfigFromAllNxProjects} instead,
9+
* if you'd like to auto include all dependencies of the provided target project use {@link eslintConfigFromNxProjectAndDeps} instead.
10+
*
11+
* @example
12+
* import eslintPlugin, {
13+
* eslintConfigFromNxProject,
14+
* } from '@code-pushup/eslint-plugin';
15+
*
16+
* const projectName = 'backoffice'; // <-- name from project.json
17+
*
18+
* export default {
19+
* plugins: [
20+
* await eslintPlugin(
21+
* await eslintConfigFromNxProject(projectName)
22+
* )
23+
* ]
24+
* }
25+
*
26+
* @param projectName Nx project serving as main entry point
27+
* @returns ESLint config and patterns, intended to be passed to {@link eslintPlugin}
28+
*/
29+
export async function eslintConfigFromNxProject(
30+
projectName: string,
31+
): Promise<ESLintTarget[]> {
32+
const { createProjectGraphAsync } = await import('@nx/devkit');
33+
const projectGraph = await createProjectGraphAsync({ exitOnError: false });
34+
35+
return nxProjectsToConfig(
36+
projectGraph,
37+
project => !!project.name && project.name === projectName,
38+
);
39+
}

packages/plugin-eslint/src/lib/nx/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export {
22
eslintConfigFromNxProjects,
33
eslintConfigFromAllNxProjects,
44
} from './find-all-projects';
5+
export { eslintConfigFromNxProject } from './find-project-without-deps';
56
export { eslintConfigFromNxProjectAndDeps } from './find-project-with-deps';

0 commit comments

Comments
 (0)