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 May 1, 2022
1 parent cb8bf52 commit adbc649
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 20 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 @@ -109,6 +109,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 @@ -161,6 +161,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 @@ -109,6 +109,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 @@ -129,6 +129,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 @@ -97,6 +97,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 @@ -97,6 +97,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 @@ -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)

### uncommitted

Type: boolean
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 @@ -99,6 +99,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
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';

Expand Down Expand Up @@ -133,6 +133,9 @@ function projectsToRun(
nxArgs
)
);
if (!nxArgs.all && nxArgs.tags) {
affectedGraph = filterByTag(affectedGraph, nxArgs);
}
if (!nxArgs.all && nxArgs.withDeps) {
affectedGraph = withDeps(
projectGraph,
Expand All @@ -150,6 +153,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 @@ -359,6 +359,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 @@ -416,10 +421,11 @@ function withAffectedOptions(yargs: yargs.Argv): yargs.Argv {
describe: 'Print additional error stack trace on failure',
})
.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 @@ -428,15 +434,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 @@ -471,7 +483,9 @@ function withRunManyOptions(yargs: yargs.Argv): yargs.Argv {
describe: 'Print additional error stack trace on failure',
})
.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 @@ -46,9 +46,13 @@ function projectsToRun(
);
}
checkForInvalidProjects(nxArgs, allProjects);
let selectedProjects = nxArgs.projects.map((name) =>
allProjects.find((project) => project.name === name)
);
let 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 @@ -58,7 +62,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 @@ -84,7 +84,7 @@ const runOne: string[] = [
'hide-cached-output',
];

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

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

export interface RawNxArgs extends NxArgs {
Expand Down Expand Up @@ -129,6 +130,7 @@ export interface NxArgs {
'hide-cached-output'?: boolean;
hideCachedOutput?: boolean;
scan?: boolean;
tags?: string[];
}

const ignoreArgs = ['$0', '_'];
Expand Down Expand Up @@ -163,9 +165,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 @@ -206,7 +206,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 @@ -246,7 +246,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 @@ -255,6 +255,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 adbc649

Please sign in to comment.