feat(core): add --affected flag to nx graph (#17340)

This commit is contained in:
Craigory Coppola 2023-06-01 11:24:18 -04:00 committed by GitHub
parent cd0b76d950
commit a33e75e4d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 113 additions and 6 deletions

1
.gitignore vendored
View File

@ -31,5 +31,6 @@ CHANGELOG.md
# Local dev files # Local dev files
.env .env
.bashrc
*.node *.node

View File

@ -67,11 +67,23 @@ Watch for changes to project graph and update in-browser:
## Options ## Options
### affected
Type: `boolean`
Highlight affected projects
### base
Type: `string`
Base of the current branch (usually main)
### exclude ### exclude
Type: `string` Type: `string`
List of projects delimited by commas to exclude from the project graph. Exclude certain projects from being processed
### file ### file
@ -79,6 +91,12 @@ Type: `string`
Output file (e.g. --file=output.json or --file=dep-graph.html) Output file (e.g. --file=output.json or --file=dep-graph.html)
### files
Type: `string`
Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces
### focus ### focus
Type: `string` Type: `string`
@ -91,6 +109,12 @@ Type: `boolean`
Group projects by folder in the project graph Group projects by folder in the project graph
### head
Type: `string`
Latest commit of the current branch (usually HEAD)
### help ### help
Type: `boolean` Type: `boolean`
@ -123,6 +147,18 @@ Type: `string`
The target to show tasks for in the task graph The target to show tasks for in the task graph
### uncommitted
Type: `boolean`
Uncommitted changes
### untracked
Type: `boolean`
Untracked changes
### version ### version
Type: `boolean` Type: `boolean`

View File

@ -67,11 +67,23 @@ Watch for changes to project graph and update in-browser:
## Options ## Options
### affected
Type: `boolean`
Highlight affected projects
### base
Type: `string`
Base of the current branch (usually main)
### exclude ### exclude
Type: `string` Type: `string`
List of projects delimited by commas to exclude from the project graph. Exclude certain projects from being processed
### file ### file
@ -79,6 +91,12 @@ Type: `string`
Output file (e.g. --file=output.json or --file=dep-graph.html) Output file (e.g. --file=output.json or --file=dep-graph.html)
### files
Type: `string`
Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces
### focus ### focus
Type: `string` Type: `string`
@ -91,6 +109,12 @@ Type: `boolean`
Group projects by folder in the project graph Group projects by folder in the project graph
### head
Type: `string`
Latest commit of the current branch (usually HEAD)
### help ### help
Type: `boolean` Type: `boolean`
@ -123,6 +147,18 @@ Type: `string`
The target to show tasks for in the task graph The target to show tasks for in the task graph
### uncommitted
Type: `boolean`
Uncommitted changes
### untracked
Type: `boolean`
Untracked changes
### version ### version
Type: `boolean` Type: `boolean`

View File

@ -54,7 +54,7 @@ export async function affected(
await connectToNxCloudIfExplicitlyAsked(nxArgs); await connectToNxCloudIfExplicitlyAsked(nxArgs);
const projectGraph = await createProjectGraphAsync({ exitOnError: true }); const projectGraph = await createProjectGraphAsync({ exitOnError: true });
const projects = await projectsToRun(nxArgs, projectGraph); const projects = await getAffectedGraphNodes(nxArgs, projectGraph);
try { try {
switch (command) { switch (command) {
@ -122,7 +122,7 @@ export async function affected(
} }
} }
async function projectsToRun( export async function getAffectedGraphNodes(
nxArgs: NxArgs, nxArgs: NxArgs,
projectGraph: ProjectGraph projectGraph: ProjectGraph
): Promise<ProjectGraphProjectNode[]> { ): Promise<ProjectGraphProjectNode[]> {

View File

@ -1,13 +1,28 @@
import { CommandModule } from 'yargs'; import { CommandModule } from 'yargs';
import { linkToNxDevAndExamples } from '../yargs-utils/documentation'; import { linkToNxDevAndExamples } from '../yargs-utils/documentation';
import { withDepGraphOptions } from '../yargs-utils/shared-options'; import {
withAffectedOptions,
withDepGraphOptions,
} from '../yargs-utils/shared-options';
export const yargsDepGraphCommand: CommandModule = { export const yargsDepGraphCommand: CommandModule = {
command: 'graph', command: 'graph',
describe: 'Graph dependencies within workspace', describe: 'Graph dependencies within workspace',
aliases: ['dep-graph'], aliases: ['dep-graph'],
builder: (yargs) => builder: (yargs) =>
linkToNxDevAndExamples(withDepGraphOptions(yargs), 'dep-graph'), linkToNxDevAndExamples(
withAffectedOptions(withDepGraphOptions(yargs)),
'dep-graph'
)
.option('affected', {
type: 'boolean',
description: 'Highlight affected projects',
})
.implies('untracked', 'affected')
.implies('uncommitted', 'affected')
.implies('files', 'affected')
.implies('base', 'affected')
.implies('head', 'affected'),
handler: async (args) => handler: async (args) =>
await (await import('./graph')).generateGraph(args as any, []), await (await import('./graph')).generateGraph(args as any, []),
}; };

View File

@ -25,6 +25,8 @@ import { TaskGraph } from '../../config/task-graph';
import { daemonClient } from '../../daemon/client/client'; import { daemonClient } from '../../daemon/client/client';
import { Server } from 'net'; import { Server } from 'net';
import { readProjectFileMapCache } from '../../project-graph/nx-deps-cache'; import { readProjectFileMapCache } from '../../project-graph/nx-deps-cache';
import { getAffectedGraphNodes } from '../affected/affected';
import { splitArgsIntoNxArgsAndOverrides } from '../../utils/command-line-utils';
export interface ProjectGraphClientResponse { export interface ProjectGraphClientResponse {
hash: string; hash: string;
@ -187,6 +189,7 @@ export async function generateGraph(
targets?: string[]; targets?: string[];
focus?: string; focus?: string;
exclude?: string[]; exclude?: string[];
affected?: boolean;
}, },
affectedProjects: string[] affectedProjects: string[]
): Promise<void> { ): Promise<void> {
@ -228,6 +231,20 @@ export async function generateGraph(
} }
} }
if (args.affected) {
affectedProjects = (
await getAffectedGraphNodes(
splitArgsIntoNxArgsAndOverrides(
args,
'affected',
{ printWarnings: true },
readNxJson()
).nxArgs,
graph
)
).map((n) => n.name);
}
if (args.exclude) { if (args.exclude) {
const invalidExcludes: string[] = []; const invalidExcludes: string[] = [];
@ -379,6 +396,8 @@ export async function generateGraph(
.map((projectName) => encodeURIComponent(projectName)) .map((projectName) => encodeURIComponent(projectName))
.join(' ') .join(' ')
); );
} else if (args.affected) {
url.pathname += '/affected';
} }
if (args.groupByFolder) { if (args.groupByFolder) {
url.searchParams.append('groupByFolder', 'true'); url.searchParams.append('groupByFolder', 'true');