-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-eslint): add exclude option for Nx projects
- Loading branch information
1 parent
849ce1f
commit 0a280e6
Showing
5 changed files
with
136 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { | ||
ProjectGraph, | ||
ProjectGraphDependency, | ||
ProjectGraphProjectNode, | ||
} from '@nx/devkit'; | ||
|
||
export function filterProjectGraph( | ||
projectGraph: ProjectGraph, | ||
exclude: string[] = [], | ||
): ProjectGraph { | ||
const filteredNodes: Record<string, ProjectGraphProjectNode> = Object.entries( | ||
projectGraph.nodes, | ||
).reduce( | ||
(acc, [projectName, projectNode]) => | ||
exclude.includes(projectName) | ||
? acc | ||
: { ...acc, [projectName]: projectNode }, | ||
{}, | ||
); | ||
const filteredDependencies: Record<string, ProjectGraphDependency[]> = | ||
Object.entries(projectGraph.dependencies).reduce( | ||
(acc, [key, deps]) => | ||
exclude.includes(key) ? acc : { ...acc, [key]: deps }, | ||
{}, | ||
); | ||
return { | ||
nodes: filteredNodes, | ||
dependencies: filteredDependencies, | ||
version: projectGraph.version, | ||
}; | ||
} |
68 changes: 68 additions & 0 deletions
68
packages/plugin-eslint/src/lib/nx/filter-project-graph.unit.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { toProjectGraph } from '@code-pushup/test-utils'; | ||
import { filterProjectGraph } from './filter-project-graph'; | ||
|
||
describe('filterProjectGraph', () => { | ||
it('should exclude specified projects from nodes', () => { | ||
const projectGraph = toProjectGraph([ | ||
{ name: 'client', type: 'app', data: { root: 'apps/client' } }, | ||
{ name: 'server', type: 'app', data: { root: 'apps/server' } }, | ||
{ name: 'models', type: 'lib', data: { root: 'libs/models' } }, | ||
]); | ||
|
||
const filteredGraph = filterProjectGraph(projectGraph, ['client']); | ||
|
||
expect(Object.keys(filteredGraph.nodes)).not.toContain('client'); | ||
expect(Object.keys(filteredGraph.nodes)).toContain('server'); | ||
expect(Object.keys(filteredGraph.nodes)).toContain('models'); | ||
}); | ||
|
||
it('should exclude dependencies of excluded projects', () => { | ||
const projectGraph = toProjectGraph( | ||
[ | ||
{ name: 'client', type: 'app', data: { root: 'apps/client' } }, | ||
{ name: 'server', type: 'app', data: { root: 'apps/server' } }, | ||
{ name: 'models', type: 'lib', data: { root: 'libs/models' } }, | ||
], | ||
{ | ||
client: ['server'], | ||
server: ['models'], | ||
}, | ||
); | ||
|
||
const filteredGraph = filterProjectGraph(projectGraph, ['client']); | ||
|
||
expect(Object.keys(filteredGraph.dependencies)).not.toContain('client'); | ||
expect(filteredGraph.dependencies['server']).toEqual([ | ||
{ source: 'server', target: 'models', type: 'static' }, | ||
]); | ||
}); | ||
|
||
it('should include all projects if exclude list is empty', () => { | ||
const projectGraph = toProjectGraph([ | ||
{ name: 'client', type: 'app', data: { root: 'apps/client' } }, | ||
{ name: 'server', type: 'app', data: { root: 'apps/server' } }, | ||
{ name: 'models', type: 'lib', data: { root: 'libs/models' } }, | ||
]); | ||
|
||
const filteredGraph = filterProjectGraph(projectGraph, []); | ||
|
||
expect(Object.keys(filteredGraph.nodes)).toContain('client'); | ||
expect(Object.keys(filteredGraph.nodes)).toContain('server'); | ||
expect(Object.keys(filteredGraph.nodes)).toContain('models'); | ||
}); | ||
|
||
it('should ignore non-existent projects in the exclude list', () => { | ||
const projectGraph = toProjectGraph([ | ||
{ name: 'client', type: 'app', data: { root: 'apps/client' } }, | ||
{ name: 'server', type: 'app', data: { root: 'apps/server' } }, | ||
{ name: 'models', type: 'lib', data: { root: 'libs/models' } }, | ||
]); | ||
|
||
const filteredGraph = filterProjectGraph(projectGraph, ['non-existent']); | ||
|
||
expect(Object.keys(filteredGraph.nodes)).toContain('client'); | ||
expect(Object.keys(filteredGraph.nodes)).toContain('server'); | ||
expect(Object.keys(filteredGraph.nodes)).toContain('models'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters