Skip to content

Commit

Permalink
feat(core): add support for tags selection in affected and run-many c…
Browse files Browse the repository at this point in the history
…ommands
  • Loading branch information
fguitton committed Jun 8, 2022
1 parent ab00752 commit 3b0d623
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 28 deletions.
6 changes: 6 additions & 0 deletions docs/generated/cli/affected-apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ Default: false

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

### tags

Type: string

Tags to run (comma delimited)

### uncommitted

Type: boolean
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/cli/affected-graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ Default: false

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

### tags

Type: string

Tags to run (comma delimited)

### uncommitted

Type: boolean
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/cli/affected-libs.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ Default: false

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

### tags

Type: string

Tags to run (comma delimited)

### uncommitted

Type: boolean
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/cli/affected.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ Default: false

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

### tags

Type: string

Tags to run (comma delimited)

### target

Type: string
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/cli/format-check.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ Default: false

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

### tags

Type: string

Tags to run (comma delimited)

### uncommitted

Type: boolean
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/cli/format-write.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ Default: false

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

### tags

Type: string

Tags to run (comma delimited)

### uncommitted

Type: boolean
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/cli/print-affected.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ Default: false

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

### tags

Type: string

Tags to run (comma delimited)

### type

Type: string
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/cli/run-many.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ Default: false

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

### tags

Type: string

Tags to run (comma delimited)

### target

Type: string
Expand Down
16 changes: 8 additions & 8 deletions docs/generated/packages/cli.json

Large diffs are not rendered by default.

38 changes: 37 additions & 1 deletion packages/nx/src/command-line/affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { performance } from 'perf_hooks';
import { createProjectGraphAsync } from '../project-graph/project-graph';
import { withDeps } from '../project-graph/operators';
import { ProjectGraph, ProjectGraphProjectNode } from '../config/project-graph';
import { projectHasTarget } from '../utils/project-graph-utils';
import { projectHasTarget, projectHasTag } from '../utils/project-graph-utils';
import { filterAffected } from '../project-graph/affected/affected-project-graph';
import { readEnvironment } from './read-environment';
import { TargetDependencyConfig } from 'nx/src/config/workspace-json-project-json';
Expand Down Expand Up @@ -147,6 +147,9 @@ function projectsToRun(
nxArgs
)
);
if (!nxArgs.all && nxArgs.tags) {
affectedGraph = filterByTag(affectedGraph, nxArgs);
}
if (!nxArgs.all && nxArgs.withDeps) {
affectedGraph = withDeps(
projectGraph,
Expand All @@ -164,6 +167,39 @@ function projectsToRun(
return Object.values(affectedGraph.nodes) as ProjectGraphProjectNode[];
}

function filterByTag(
affectedGraph: ProjectGraph,
nxArgs: NxArgs
): ProjectGraph {
const filteredProjects = allProjectsWithTag(
Object.values(affectedGraph.nodes),
nxArgs
);
const res = {
nodes: filteredProjects.reduce(
(nodes, project) => ({
...nodes,
[project.name]: project,
}),
{}
),
dependencies: affectedGraph.dependencies,
} as ProjectGraph;
return res;
}

function allProjectsWithTag(
projects: ProjectGraphProjectNode[],
nxArgs: NxArgs
) {
return projects.filter((p) =>
nxArgs.tags.reduce(
(matched, tag) => matched || projectHasTag(p, tag),
false
)
);
}

function allProjectsWithTarget(
projects: ProjectGraphProjectNode[],
nxArgs: NxArgs
Expand Down
30 changes: 22 additions & 8 deletions packages/nx/src/command-line/nx-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,11 @@ function withAffectedOptions(yargs: yargs.Argv): yargs.Argv {
type: 'boolean',
default: undefined,
})
.option('tags', {
describe: 'Tags to run (comma delimited)',
type: 'string',
default: undefined,
})
.option('all', {
describe: 'All projects',
type: 'boolean',
Expand Down Expand Up @@ -448,10 +453,11 @@ function withAffectedOptions(yargs: yargs.Argv): yargs.Argv {
default: false,
})
.conflicts({
files: ['uncommitted', 'untracked', 'base', 'head', 'all'],
untracked: ['uncommitted', 'files', 'base', 'head', 'all'],
uncommitted: ['files', 'untracked', 'base', 'head', 'all'],
all: ['files', 'untracked', 'uncommitted', 'base', 'head'],
files: ['uncommitted', 'untracked', 'base', 'head', 'all', 'tags'],
untracked: ['uncommitted', 'files', 'base', 'head', 'all', 'tags'],
uncommitted: ['files', 'untracked', 'base', 'head', 'all', 'tags'],
all: ['files', 'untracked', 'uncommitted', 'base', 'head', 'tags'],
tags: ['files', 'untracked', 'uncommitted', 'base', 'head', 'all'],
});
}

Expand All @@ -465,15 +471,21 @@ function withRunManyOptions(yargs: yargs.Argv): yargs.Argv {
.option('projects', {
describe: 'Projects to run (comma delimited)',
type: 'string',
default: undefined,
})
.option('tags', {
describe: 'Tags to run (comma delimited)',
type: 'string',
default: undefined,
})
.option('all', {
describe: 'Run the target on all projects in the workspace',
type: 'boolean',
default: undefined,
})
.check(({ all, projects }) => {
if ((all && projects) || (!all && !projects))
throw new Error('You must provide either --all or --projects');
.check(({ all, projects, tags }) => {
if (!all && !projects && !tags)
throw new Error('You must provide either --all, --projects or --tags');
return true;
})
.options('runner', {
Expand Down Expand Up @@ -518,7 +530,9 @@ function withRunManyOptions(yargs: yargs.Argv): yargs.Argv {
default: false,
})
.conflicts({
all: 'projects',
all: ['projects', 'tags'],
projects: ['all', 'tags'],
tags: ['all', 'projects'],
});
}

Expand Down
14 changes: 9 additions & 5 deletions packages/nx/src/command-line/run-many.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as yargs from 'yargs';
import { runCommand } from '../tasks-runner/run-command';
import type { NxArgs, RawNxArgs } from '../utils/command-line-utils';
import { splitArgsIntoNxArgsAndOverrides } from '../utils/command-line-utils';
import { projectHasTarget } from '../utils/project-graph-utils';
import { projectHasTarget, projectHasTag } from '../utils/project-graph-utils';
import { output } from '../utils/output';
import { connectToNxCloudUsingScan } from './connect-to-nx-cloud';
import { performance } from 'perf_hooks';
Expand Down Expand Up @@ -57,9 +57,13 @@ function projectsToRun(
return res;
}
checkForInvalidProjects(nxArgs, allProjects);
const selectedProjects = nxArgs.projects.map((name) =>
allProjects.find((project) => project.name === name)
);
const selectedProjects = nxArgs.tags
? nxArgs.tags.map((tag) =>
allProjects.find((project) => project.data.tags.includes(tag))
)
: (nxArgs.projects ?? []).map((name) =>
allProjects.find((project) => project.name === name)
);
return runnableForTarget(selectedProjects, nxArgs.target, true).filter(
(proj) => !excludedProjects.has(proj.name)
);
Expand All @@ -69,7 +73,7 @@ function checkForInvalidProjects(
nxArgs: NxArgs,
allProjects: ProjectGraphProjectNode[]
) {
const invalid = nxArgs.projects.filter(
const invalid = (nxArgs.projects ?? []).filter(
(name) => !allProjects.find((p) => p.name === name)
);
if (invalid.length !== 0) {
Expand Down
18 changes: 12 additions & 6 deletions packages/nx/src/utils/command-line-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const runOne: string[] = [
'nxIgnoreCycles',
];

const runMany: string[] = [...runOne, 'projects', 'all'];
const runMany: string[] = [...runOne, 'projects', 'tags', 'all'];

const runAffected: string[] = [
...runOne,
Expand All @@ -98,6 +98,7 @@ const runAffected: string[] = [
'files',
'plain',
'select',
'tags',
'type',
];

Expand Down Expand Up @@ -130,6 +131,7 @@ export interface NxArgs {
scan?: boolean;
nxBail?: boolean;
nxIgnoreCycles?: boolean;
tags?: string[];
type?: string;
}

Expand Down Expand Up @@ -166,9 +168,7 @@ export function splitArgsIntoNxArgsAndOverrides(
});

if (mode === 'run-many') {
if (!nxArgs.projects) {
nxArgs.projects = [];
} else {
if (nxArgs.projects) {
nxArgs.projects = (args.projects as string)
.split(',')
.map((p: string) => p.trim());
Expand Down Expand Up @@ -209,7 +209,7 @@ export function splitArgsIntoNxArgsAndOverrides(
!nxArgs.untracked &&
!nxArgs.base &&
!nxArgs.head &&
!nxArgs.all &&
(!nxArgs.all || !nxArgs.tags) &&
args._.length >= 3
) {
nxArgs.base = args._[1] as string;
Expand Down Expand Up @@ -248,7 +248,7 @@ export function splitArgsIntoNxArgsAndOverrides(
!nxArgs.files &&
!nxArgs.uncommitted &&
!nxArgs.untracked &&
!nxArgs.all
(!nxArgs.all || !nxArgs.tags)
) {
output.note({
title: `Affected criteria defaulted to --base=${output.bold(
Expand All @@ -257,6 +257,12 @@ export function splitArgsIntoNxArgsAndOverrides(
});
}
}

if (nxArgs.tags) {
nxArgs.tags = (args.tags as string)
.split(',')
.map((p: string) => p.trim());
}
}

if (!nxArgs.skipNxCache) {
Expand Down
8 changes: 8 additions & 0 deletions packages/nx/src/utils/project-graph-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ export function projectHasTargetAndConfiguration(
);
}

export function projectHasTag(project: ProjectGraphProjectNode, tag: string) {
return !!(
project.data &&
project.data.tags &&
project.data.tags.includes(tag)
);
}

export function mergeNpmScriptsWithTargets(
projectRoot: string,
targets
Expand Down

0 comments on commit 3b0d623

Please sign in to comment.