Skip to content

Commit

Permalink
feat(plugin-eslint): add eslintConfigFromNxProject helper that doesn'…
Browse files Browse the repository at this point in the history
…t include nx project deps

Signed-off-by: Vojtech Masek <vojtech@flowup.cz>
  • Loading branch information
vmasek committed May 22, 2024
1 parent efbb72a commit 0706bda
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/plugin-eslint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export {
eslintConfigFromNxProjectAndDeps,
eslintConfigFromNxProjects,
eslintConfigFromAllNxProjects,
eslintConfigFromNxProject,
} from './lib/nx';
37 changes: 34 additions & 3 deletions packages/plugin-eslint/src/lib/nx.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import {
eslintConfigFromAllNxProjects,
eslintConfigFromNxProjectAndDeps,
} from './nx';
import { eslintConfigFromNxProject } from './nx/find-project-without-deps';

const ALL_PROJECTS = ['cli', 'core', 'nx-plugin', 'utils'] as const;
type Project = (typeof ALL_PROJECTS)[number];

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

const ALL_PROJECTS = ['cli', 'core', 'nx-plugin', 'utils'] as const;
type Project = (typeof ALL_PROJECTS)[number];

it.each<[Project, Project[]]>([
['cli', ['cli', 'core', 'utils']],
['core', ['core', 'utils']],
Expand All @@ -125,4 +126,34 @@ describe('Nx helpers', () => {
},
);
});

describe('create config from target Nx project without its dependencies', () => {
/*
* Project graph:
*
* cli
* │
* │
* ▼
* core
* │ nx-plugin
* │ │
* ▼ │
* utils ◄──────┘
*/

it.each<[Project]>([['cli'], ['core'], ['utils']])(
'project %j - expected configurations for projects %j',
async project => {
const targets = await eslintConfigFromNxProject(project);

expect(targets).toEqual([
{
eslintrc: `./packages/${project}/.eslintrc.json`,
patterns: expect.arrayContaining([`packages/${project}/**/*.ts`]),
},
]);
},
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { findAllDependencies } from './traverse-graph';
*
* Use when you wish to include a targeted subset of your Nx monorepo in your Code PushUp project.
* If you prefer to include all Nx projects, refer to {@link eslintConfigFromAllNxProjects} instead.
* if you'd like to skip dependencies of the provided target project use {@link eslintConfigFromNxProject} instead.
*
* @example
* import eslintPlugin, {
Expand Down
39 changes: 39 additions & 0 deletions packages/plugin-eslint/src/lib/nx/find-project-without-deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { ESLintTarget } from '../config';
import { nxProjectsToConfig } from './projects-to-config';

/**
* Accepts a target Nx projects, converts lint configurations to Code PushUp ESLint plugin parameters.
*
* Use when you wish to include a targeted subset of your Nx monorepo in your Code PushUp project.
* If you prefer to include all Nx projects, refer to {@link eslintConfigFromAllNxProjects} instead,
* if you'd like to auto include all dependencies of the provided target project use {@link eslintConfigFromNxProjectAndDeps} instead.
*
* @example
* import eslintPlugin, {
* eslintConfigFromNxProject,
* } from '@code-pushup/eslint-plugin';
*
* const projectName = 'backoffice'; // <-- name from project.json
*
* export default {
* plugins: [
* await eslintPlugin(
* await eslintConfigFromNxProject(projectName)
* )
* ]
* }
*
* @param projectName Nx project serving as main entry point
* @returns ESLint config and patterns, intended to be passed to {@link eslintPlugin}
*/
export async function eslintConfigFromNxProject(
projectName: string,
): Promise<ESLintTarget[]> {
const { createProjectGraphAsync } = await import('@nx/devkit');
const projectGraph = await createProjectGraphAsync({ exitOnError: false });

return nxProjectsToConfig(
projectGraph,
project => !!project.name && project.name === projectName,
);
}
1 change: 1 addition & 0 deletions packages/plugin-eslint/src/lib/nx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export {
eslintConfigFromNxProjects,
eslintConfigFromAllNxProjects,
} from './find-all-projects';
export { eslintConfigFromNxProject } from './find-project-without-deps';
export { eslintConfigFromNxProjectAndDeps } from './find-project-with-deps';

0 comments on commit 0706bda

Please sign in to comment.