Skip to content

Commit a33e75e

Browse files
authored
feat(core): add --affected flag to nx graph (nrwl#17340)
1 parent cd0b76d commit a33e75e

File tree

6 files changed

+113
-6
lines changed

6 files changed

+113
-6
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ CHANGELOG.md
3131

3232
# Local dev files
3333
.env
34+
.bashrc
3435

3536
*.node

docs/generated/cli/graph.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,36 @@ Watch for changes to project graph and update in-browser:
6767

6868
## Options
6969

70+
### affected
71+
72+
Type: `boolean`
73+
74+
Highlight affected projects
75+
76+
### base
77+
78+
Type: `string`
79+
80+
Base of the current branch (usually main)
81+
7082
### exclude
7183

7284
Type: `string`
7385

74-
List of projects delimited by commas to exclude from the project graph.
86+
Exclude certain projects from being processed
7587

7688
### file
7789

7890
Type: `string`
7991

8092
Output file (e.g. --file=output.json or --file=dep-graph.html)
8193

94+
### files
95+
96+
Type: `string`
97+
98+
Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces
99+
82100
### focus
83101

84102
Type: `string`
@@ -91,6 +109,12 @@ Type: `boolean`
91109

92110
Group projects by folder in the project graph
93111

112+
### head
113+
114+
Type: `string`
115+
116+
Latest commit of the current branch (usually HEAD)
117+
94118
### help
95119

96120
Type: `boolean`
@@ -123,6 +147,18 @@ Type: `string`
123147

124148
The target to show tasks for in the task graph
125149

150+
### uncommitted
151+
152+
Type: `boolean`
153+
154+
Uncommitted changes
155+
156+
### untracked
157+
158+
Type: `boolean`
159+
160+
Untracked changes
161+
126162
### version
127163

128164
Type: `boolean`

docs/generated/packages/nx/documents/dep-graph.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,36 @@ Watch for changes to project graph and update in-browser:
6767

6868
## Options
6969

70+
### affected
71+
72+
Type: `boolean`
73+
74+
Highlight affected projects
75+
76+
### base
77+
78+
Type: `string`
79+
80+
Base of the current branch (usually main)
81+
7082
### exclude
7183

7284
Type: `string`
7385

74-
List of projects delimited by commas to exclude from the project graph.
86+
Exclude certain projects from being processed
7587

7688
### file
7789

7890
Type: `string`
7991

8092
Output file (e.g. --file=output.json or --file=dep-graph.html)
8193

94+
### files
95+
96+
Type: `string`
97+
98+
Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces
99+
82100
### focus
83101

84102
Type: `string`
@@ -91,6 +109,12 @@ Type: `boolean`
91109

92110
Group projects by folder in the project graph
93111

112+
### head
113+
114+
Type: `string`
115+
116+
Latest commit of the current branch (usually HEAD)
117+
94118
### help
95119

96120
Type: `boolean`
@@ -123,6 +147,18 @@ Type: `string`
123147

124148
The target to show tasks for in the task graph
125149

150+
### uncommitted
151+
152+
Type: `boolean`
153+
154+
Uncommitted changes
155+
156+
### untracked
157+
158+
Type: `boolean`
159+
160+
Untracked changes
161+
126162
### version
127163

128164
Type: `boolean`

packages/nx/src/command-line/affected/affected.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export async function affected(
5454
await connectToNxCloudIfExplicitlyAsked(nxArgs);
5555

5656
const projectGraph = await createProjectGraphAsync({ exitOnError: true });
57-
const projects = await projectsToRun(nxArgs, projectGraph);
57+
const projects = await getAffectedGraphNodes(nxArgs, projectGraph);
5858

5959
try {
6060
switch (command) {
@@ -122,7 +122,7 @@ export async function affected(
122122
}
123123
}
124124

125-
async function projectsToRun(
125+
export async function getAffectedGraphNodes(
126126
nxArgs: NxArgs,
127127
projectGraph: ProjectGraph
128128
): Promise<ProjectGraphProjectNode[]> {
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
import { CommandModule } from 'yargs';
22
import { linkToNxDevAndExamples } from '../yargs-utils/documentation';
3-
import { withDepGraphOptions } from '../yargs-utils/shared-options';
3+
import {
4+
withAffectedOptions,
5+
withDepGraphOptions,
6+
} from '../yargs-utils/shared-options';
47

58
export const yargsDepGraphCommand: CommandModule = {
69
command: 'graph',
710
describe: 'Graph dependencies within workspace',
811
aliases: ['dep-graph'],
912
builder: (yargs) =>
10-
linkToNxDevAndExamples(withDepGraphOptions(yargs), 'dep-graph'),
13+
linkToNxDevAndExamples(
14+
withAffectedOptions(withDepGraphOptions(yargs)),
15+
'dep-graph'
16+
)
17+
.option('affected', {
18+
type: 'boolean',
19+
description: 'Highlight affected projects',
20+
})
21+
.implies('untracked', 'affected')
22+
.implies('uncommitted', 'affected')
23+
.implies('files', 'affected')
24+
.implies('base', 'affected')
25+
.implies('head', 'affected'),
1126
handler: async (args) =>
1227
await (await import('./graph')).generateGraph(args as any, []),
1328
};

packages/nx/src/command-line/graph/graph.ts

+19
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import { TaskGraph } from '../../config/task-graph';
2525
import { daemonClient } from '../../daemon/client/client';
2626
import { Server } from 'net';
2727
import { readProjectFileMapCache } from '../../project-graph/nx-deps-cache';
28+
import { getAffectedGraphNodes } from '../affected/affected';
29+
import { splitArgsIntoNxArgsAndOverrides } from '../../utils/command-line-utils';
2830

2931
export interface ProjectGraphClientResponse {
3032
hash: string;
@@ -187,6 +189,7 @@ export async function generateGraph(
187189
targets?: string[];
188190
focus?: string;
189191
exclude?: string[];
192+
affected?: boolean;
190193
},
191194
affectedProjects: string[]
192195
): Promise<void> {
@@ -228,6 +231,20 @@ export async function generateGraph(
228231
}
229232
}
230233

234+
if (args.affected) {
235+
affectedProjects = (
236+
await getAffectedGraphNodes(
237+
splitArgsIntoNxArgsAndOverrides(
238+
args,
239+
'affected',
240+
{ printWarnings: true },
241+
readNxJson()
242+
).nxArgs,
243+
graph
244+
)
245+
).map((n) => n.name);
246+
}
247+
231248
if (args.exclude) {
232249
const invalidExcludes: string[] = [];
233250

@@ -379,6 +396,8 @@ export async function generateGraph(
379396
.map((projectName) => encodeURIComponent(projectName))
380397
.join(' ')
381398
);
399+
} else if (args.affected) {
400+
url.pathname += '/affected';
382401
}
383402
if (args.groupByFolder) {
384403
url.searchParams.append('groupByFolder', 'true');

0 commit comments

Comments
 (0)