-
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
d8a7eb2
commit e9560f5
Showing
8 changed files
with
193 additions
and
47 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
44 changes: 1 addition & 43 deletions
44
packages/plugin-eslint/src/lib/nx/projects-to-config.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
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,51 @@ | ||
import type { | ||
ProjectGraph, | ||
ProjectGraphDependency, | ||
ProjectGraphProjectNode, | ||
} from '@nx/devkit'; | ||
|
||
/** | ||
* Converts nodes and dependencies into a ProjectGraph object for testing purposes. | ||
* | ||
* @param nodes - Array of ProjectGraphProjectNode representing project nodes. | ||
* @param dependencies - Optional dependencies for each project in a record format. | ||
* @returns A ProjectGraph object. | ||
*/ | ||
export function toProjectGraph( | ||
nodes: ProjectGraphProjectNode[], | ||
dependencies?: Record<string, string[]>, | ||
): ProjectGraph { | ||
return { | ||
nodes: Object.fromEntries( | ||
nodes.map(node => [ | ||
node.name, | ||
{ | ||
...node, | ||
data: { | ||
targets: { | ||
lint: { | ||
options: { | ||
lintFilePatterns: `${node.data.root}/**/*.ts`, | ||
}, | ||
}, | ||
}, | ||
sourceRoot: `${node.data.root}/src`, | ||
...node.data, | ||
}, | ||
}, | ||
]), | ||
), | ||
dependencies: Object.fromEntries( | ||
nodes.map(node => [ | ||
node.name, | ||
dependencies?.[node.name]?.map( | ||
(target): ProjectGraphDependency => ({ | ||
source: node.name, | ||
target, | ||
type: 'static', | ||
}), | ||
) ?? [], | ||
]), | ||
), | ||
}; | ||
} |