Skip to content

Commit

Permalink
feat(core): add --type for nx show projects (nrwl#19358)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder authored Oct 6, 2023
1 parent 8ccd88c commit 621a2d5
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 12 deletions.
14 changes: 14 additions & 0 deletions docs/generated/cli/show.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ Show affected projects in the workspace:
nx show projects --affected
```

Show affected apps in the workspace:

```shell
nx show projects --affected --type app
```

Show affected projects in the workspace, excluding end-to-end projects:

```shell
Expand Down Expand Up @@ -133,6 +139,14 @@ Type: `string`

Show only projects that match a given pattern.

##### type

Type: `string`

Choices: [app, lib, e2e]

Select only projects of the given type

##### uncommitted

Type: `boolean`
Expand Down
14 changes: 14 additions & 0 deletions docs/generated/packages/nx/documents/show.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ Show affected projects in the workspace:
nx show projects --affected
```

Show affected apps in the workspace:

```shell
nx show projects --affected --type app
```

Show affected projects in the workspace, excluding end-to-end projects:

```shell
Expand Down Expand Up @@ -133,6 +139,14 @@ Type: `string`

Show only projects that match a given pattern.

##### type

Type: `string`

Choices: [app, lib, e2e]

Select only projects of the given type

##### uncommitted

Type: `boolean`
Expand Down
16 changes: 12 additions & 4 deletions e2e/nx-run/src/affected-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,19 @@ describe('Nx Affected and Graph Tests', () => {
`
);

const affectedApps = runCLI(
const affectedProjects = runCLI(
`show projects --affected --files="libs/${mylib}/src/index.ts"`
);
expect(affectedApps).toContain(myapp);
expect(affectedApps).not.toContain(myapp2);
expect(affectedProjects).toContain(myapp);
expect(affectedProjects).not.toContain(myapp2);

let affectedLibs = runCLI(
`show projects --affected --files="libs/${mylib}/src/index.ts" --type lib`
);
// type lib shows no apps
expect(affectedLibs).not.toContain(myapp);
expect(affectedLibs).not.toContain(myapp2);
expect(affectedLibs).toContain(mylib);

const implicitlyAffectedApps = runCLI(
'show projects --affected --files="tsconfig.base.json"'
Expand All @@ -81,7 +89,7 @@ describe('Nx Affected and Graph Tests', () => {
expect(noAffectedApps).not.toContain(myapp);
expect(noAffectedApps).not.toContain(myapp2);

const affectedLibs = runCLI(
affectedLibs = runCLI(
`show projects --affected --files="libs/${mylib}/src/index.ts"`
);
expect(affectedLibs).toContain(mypublishablelib);
Expand Down
5 changes: 5 additions & 0 deletions packages/nx/src/command-line/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ export const examples: Record<string, Example[]> = {
description: 'Show affected projects in the workspace',
},

{
command: 'show projects --affected --type app',
description: 'Show affected apps in the workspace',
},

{
command: 'show projects --affected --exclude=*-e2e',
description:
Expand Down
11 changes: 11 additions & 0 deletions packages/nx/src/command-line/show/command-object.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ProjectGraphProjectNode } from '../../config/project-graph';
import { CommandModule, showHelp } from 'yargs';
import { parseCSV, withAffectedOptions } from '../yargs-utils/shared-options';

Expand All @@ -13,6 +14,7 @@ export type ShowProjectsOptions = NxShowArgs & {
base: string;
head: string;
affected: boolean;
type: ProjectGraphProjectNode['type'];
projects: string[];
withTarget: string[];
};
Expand Down Expand Up @@ -75,6 +77,11 @@ const showProjectsCommand: CommandModule<NxShowArgs, ShowProjectsOptions> = {
description: 'Show only projects that have a specific target',
coerce: parseCSV,
})
.option('type', {
type: 'string',
description: 'Select only projects of the given type',
choices: ['app', 'lib', 'e2e'],
})
.implies('untracked', 'affected')
.implies('uncommitted', 'affected')
.implies('files', 'affected')
Expand All @@ -92,6 +99,10 @@ const showProjectsCommand: CommandModule<NxShowArgs, ShowProjectsOptions> = {
'$0 show projects --affected',
'Show affected projects in the workspace'
)
.example(
'$0 show projects --type app --affected',
'Show affected apps in the workspace'
)
.example(
'$0 show projects --affected --exclude=*-e2e',
'Show affected projects in the workspace, excluding end-to-end projects'
Expand Down
36 changes: 28 additions & 8 deletions packages/nx/src/command-line/show/show.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { filterAffected } from '../../project-graph/affected/affected-project-graph';
import { calculateFileChanges } from '../../project-graph/file-utils';
import {
FileChange,
calculateFileChanges,
} from '../../project-graph/file-utils';
import { filterNodes } from '../../project-graph/operators';
import { readNxJson } from '../../config/nx-json';
import {
NxArgs,
Expand Down Expand Up @@ -30,14 +34,26 @@ export async function showProjectsHandler(
nxJson
);

// Affected touches dependencies so it needs to be processed first.
if (args.affected) {
graph = await getAffectedGraph(nxArgs, nxJson, graph);
const touchedFiles = await getTouchedFiles(nxArgs);
graph = await getAffectedGraph(touchedFiles, nxJson, graph);
}

const filter = filterNodes((node) => {
if (args.type && node.type !== args.type) {
return false;
}
return true;
});
graph = filter(graph);

// Apply projects filter and get resultant graph
if (args.projects) {
graph.nodes = getGraphNodesMatchingPatterns(graph, args.projects);
}

// Grab only the nodes with the specified target
if (args.withTarget) {
graph.nodes = Object.entries(graph.nodes).reduce((acc, [name, node]) => {
if (args.withTarget.some((target) => node.data.targets?.[target])) {
Expand Down Expand Up @@ -131,14 +147,18 @@ function getGraphNodesMatchingPatterns(
return nodes;
}

async function getAffectedGraph(
nxArgs: NxArgs,
function getAffectedGraph(
touchedFiles: FileChange[],
nxJson: NxJsonConfiguration<'*' | string[]>,
graph: ProjectGraph
) {
return filterAffected(
graph,
calculateFileChanges(parseFiles(nxArgs).files, await allFileData(), nxArgs),
nxJson
return filterAffected(graph, touchedFiles, nxJson);
}

async function getTouchedFiles(nxArgs: NxArgs): Promise<FileChange[]> {
return calculateFileChanges(
parseFiles(nxArgs).files,
await allFileData(),
nxArgs
);
}

0 comments on commit 621a2d5

Please sign in to comment.