Skip to content

Commit 3410cfc

Browse files
committed
feat(core): add support for tags selection in affected and run-many commands
1 parent 3653e59 commit 3410cfc

16 files changed

+238
-36
lines changed

docs/generated/cli/affected-apps.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ Show help
8181

8282
Produces a plain output for affected:apps and affected:libs
8383

84+
### tags
85+
86+
Type: `string`
87+
88+
Tags to run (comma delimited)
89+
8490
### uncommitted
8591

8692
Type: `boolean`

docs/generated/cli/affected-graph.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ Type: `number`
131131

132132
Bind the project graph server to a specific port.
133133

134+
### tags
135+
136+
Type: `string`
137+
138+
Tags to run (comma delimited)
139+
134140
### uncommitted
135141

136142
Type: `boolean`

docs/generated/cli/affected-libs.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ Show help
8181

8282
Produces a plain output for affected:apps and affected:libs
8383

84+
### tags
85+
86+
Type: `string`
87+
88+
Tags to run (comma delimited)
89+
8490
### uncommitted
8591

8692
Type: `boolean`

docs/generated/cli/affected.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ Default: `false`
149149

150150
Rerun the tasks even when the results are available in the cache
151151

152+
### tags
153+
154+
Type: `string`
155+
156+
Tags to run (comma delimited)
157+
152158
### target
153159

154160
Type: `string`

docs/generated/cli/format-check.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ Type: `array`
6767

6868
Projects to format (comma delimited)
6969

70+
### tags
71+
72+
Type: `string`
73+
74+
Tags to run (comma delimited)
75+
7076
### uncommitted
7177

7278
Type: `boolean`

docs/generated/cli/format-write.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ Type: `array`
6767

6868
Projects to format (comma delimited)
6969

70+
### tags
71+
72+
Type: `string`
73+
74+
Tags to run (comma delimited)
75+
7076
### uncommitted
7177

7278
Type: `boolean`

docs/generated/cli/print-affected.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ Type: `string`
9999

100100
Select the subset of the returned json document (e.g., --select=projects)
101101

102+
### tags
103+
104+
Type: `string`
105+
106+
Tags to run (comma delimited)
107+
102108
### target
103109

104110
Type: `string`

docs/generated/cli/run-many.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ Default: `false`
121121

122122
Rerun the tasks even when the results are available in the cache
123123

124+
### tags
125+
126+
Type: `string`
127+
128+
Tags to run (comma delimited)
129+
124130
### target
125131

126132
Type: `string`

docs/generated/packages/nx.json

Lines changed: 8 additions & 8 deletions
Large diffs are not rendered by default.

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import { performance } from 'perf_hooks';
1313
import { createProjectGraphAsync } from '../project-graph/project-graph';
1414
import { ProjectGraph, ProjectGraphProjectNode } from '../config/project-graph';
15-
import { projectHasTarget } from '../utils/project-graph-utils';
15+
import { projectHasTarget, filterByTags } from '../utils/project-graph-utils';
1616
import { filterAffected } from '../project-graph/affected/affected-project-graph';
1717
import { TargetDependencyConfig } from '../config/workspace-json-project-json';
1818
import { readNxJson } from '../config/configuration';
@@ -148,6 +148,9 @@ function projectsToRun(
148148
nxArgs
149149
)
150150
);
151+
if (nxArgs.tags.length > 0) {
152+
affectedGraph = affectedProjectsWithTags(affectedGraph, nxArgs);
153+
}
151154

152155
if (nxArgs.exclude) {
153156
const excludedProjects = new Set(nxArgs.exclude);
@@ -159,6 +162,27 @@ function projectsToRun(
159162
return Object.values(affectedGraph.nodes) as ProjectGraphProjectNode[];
160163
}
161164

165+
function affectedProjectsWithTags(
166+
affectedGraph: ProjectGraph,
167+
nxArgs: NxArgs
168+
): ProjectGraph {
169+
const matchedProjectNames = filterByTags(affectedGraph, nxArgs.tags);
170+
const filteredProjects = matchedProjectNames.map(
171+
(projectName) => affectedGraph.nodes[projectName]
172+
);
173+
const res = {
174+
nodes: filteredProjects.reduce(
175+
(nodes, project) => ({
176+
...nodes,
177+
[project.name]: project,
178+
}),
179+
{}
180+
),
181+
dependencies: affectedGraph.dependencies,
182+
} as ProjectGraph;
183+
return res;
184+
}
185+
162186
function allProjectsWithTarget(
163187
projects: ProjectGraphProjectNode[],
164188
nxArgs: NxArgs

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,11 @@ function withAffectedOptions(yargs: yargs.Argv): yargs.Argv {
512512
type: 'boolean',
513513
default: undefined,
514514
})
515+
.option('tags', {
516+
describe: 'Tags to run (comma delimited)',
517+
type: 'string',
518+
default: undefined,
519+
})
515520
.option('all', {
516521
describe: 'All projects',
517522
type: 'boolean',
@@ -538,10 +543,11 @@ function withAffectedOptions(yargs: yargs.Argv): yargs.Argv {
538543
.group(['files', 'uncommitted', 'untracked'], 'or using:')
539544
.implies('head', 'base')
540545
.conflicts({
541-
files: ['uncommitted', 'untracked', 'base', 'head', 'all'],
542-
untracked: ['uncommitted', 'files', 'base', 'head', 'all'],
543-
uncommitted: ['files', 'untracked', 'base', 'head', 'all'],
544-
all: ['files', 'untracked', 'uncommitted', 'base', 'head'],
546+
files: ['uncommitted', 'untracked', 'base', 'head', 'all', 'tags'],
547+
untracked: ['uncommitted', 'files', 'base', 'head', 'all', 'tags'],
548+
uncommitted: ['files', 'untracked', 'base', 'head', 'all', 'tags'],
549+
all: ['files', 'untracked', 'uncommitted', 'base', 'head', 'tags'],
550+
tags: ['files', 'untracked', 'uncommitted', 'base', 'head', 'all'],
545551
});
546552
}
547553

@@ -556,11 +562,21 @@ function withRunManyOptions(yargs: yargs.Argv): yargs.Argv {
556562
describe:
557563
'Projects to run. (comma delimited project names and/or patterns)',
558564
type: 'string',
565+
default: undefined,
566+
})
567+
.option('tags', {
568+
describe: 'Tags to run (comma delimited)',
569+
type: 'string',
570+
default: undefined,
559571
})
560572
.option('all', {
561573
describe: '[deprecated] Run the target on all projects in the workspace',
562574
type: 'boolean',
563575
default: true,
576+
})
577+
.conflicts({
578+
projects: ['tags'],
579+
tags: ['projects'],
564580
});
565581
}
566582

packages/nx/src/command-line/run-many.spec.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe('run-many', () => {
1313
type: 'lib',
1414
data: {
1515
root: 'proj1',
16+
tags: ['api', 'theme1'],
1617
targets: {
1718
build: {},
1819
test: {},
@@ -24,6 +25,7 @@ describe('run-many', () => {
2425
type: 'lib',
2526
data: {
2627
root: 'proj2',
28+
tags: ['ui', 'theme2'],
2729
targets: {
2830
test: {},
2931
},
@@ -40,6 +42,7 @@ describe('run-many', () => {
4042
all: true,
4143
target: 'test',
4244
projects: [],
45+
tags: [],
4346
},
4447
projectGraph
4548
).map(({ name }) => name);
@@ -52,6 +55,7 @@ describe('run-many', () => {
5255
{
5356
target: 'test',
5457
projects: ['proj1'],
58+
tags: [],
5559
},
5660
projectGraph
5761
).map(({ name }) => name);
@@ -64,19 +68,73 @@ describe('run-many', () => {
6468
{
6569
target: 'test',
6670
projects: ['proj*'],
71+
tags: [],
6772
},
6873
projectGraph
6974
).map(({ name }) => name);
7075
expect(projects).toContain('proj1');
7176
expect(projects).toContain('proj2');
7277
});
7378

79+
it('should filter projects by tag', () => {
80+
const projects = projectsToRun(
81+
{
82+
target: 'test',
83+
projects: [],
84+
tags: ['api'],
85+
},
86+
projectGraph
87+
).map(({ name }) => name);
88+
expect(projects).toContain('proj1');
89+
expect(projects).not.toContain('proj2');
90+
});
91+
92+
it('should filter projects by tag pattern', () => {
93+
const projects = projectsToRun(
94+
{
95+
target: 'test',
96+
projects: [],
97+
tags: ['theme*'],
98+
},
99+
projectGraph
100+
).map(({ name }) => name);
101+
expect(projects).toContain('proj1');
102+
expect(projects).toContain('proj2');
103+
});
104+
105+
it('should filter projects by tag only', () => {
106+
const projects = projectsToRun(
107+
{
108+
target: 'test',
109+
projects: ['proj1'],
110+
tags: ['ui'],
111+
},
112+
projectGraph
113+
).map(({ name }) => name);
114+
expect(projects).not.toContain('proj1');
115+
expect(projects).toContain('proj2');
116+
});
117+
74118
it('should throw for invalid patterns', () => {
75119
expect(() => {
76120
projectsToRun(
77121
{
78122
target: 'test',
79123
projects: ['nomatch*'],
124+
tags: [],
125+
},
126+
projectGraph
127+
).map(({ name }) => name);
128+
}).toThrowError('nomatch*');
129+
});
130+
131+
it('should throw for invalid tag patterns', () => {
132+
expect(() => {
133+
projectsToRun(
134+
{
135+
target: 'test',
136+
projects: [],
137+
tags: ['nomatch*'],
80138
},
81139
projectGraph
82140
).map(({ name }) => name);
@@ -89,6 +147,7 @@ describe('run-many', () => {
89147
all: true,
90148
target: 'test',
91149
projects: [],
150+
tags: [],
92151
exclude: ['proj1'],
93152
},
94153
projectGraph
@@ -103,6 +162,7 @@ describe('run-many', () => {
103162
all: true,
104163
target: 'test',
105164
projects: [],
165+
tags: [],
106166
exclude: ['proj*'],
107167
},
108168
projectGraph
@@ -133,6 +193,7 @@ describe('run-many', () => {
133193
{
134194
target: 'test',
135195
projects: ['proj1*'],
196+
tags: [],
136197
exclude: ['proj12*'],
137198
},
138199
projectGraph

0 commit comments

Comments
 (0)