Skip to content

Commit 115a1ab

Browse files
committed
fix(core): use default configuration when the provided one is not available
1 parent b70fe05 commit 115a1ab

File tree

10 files changed

+86
-77
lines changed

10 files changed

+86
-77
lines changed

e2e/run-many.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ forEachCli(() => {
1313
describe('Run Many', () => {
1414
it('should build specific and all projects', () => {
1515
newProject();
16+
const appA = uniq('appa-rand');
1617
const libA = uniq('liba-rand');
1718
const libB = uniq('libb-rand');
1819
const libC = uniq('libc-rand');
1920
const libD = uniq('libd-rand');
2021

22+
l(runCLI(`generate @nrwl/angular:app ${appA}`));
2123
l(runCLI(`generate @nrwl/angular:lib ${libA} --publishable --defaults`));
2224
l(runCLI(`generate @nrwl/angular:lib ${libB} --publishable --defaults`));
2325
l(runCLI(`generate @nrwl/angular:lib ${libC} --publishable --defaults`));
@@ -75,6 +77,20 @@ forEachCli(() => {
7577
expect(buildWithDeps).toContain('Running target "build" succeeded');
7678

7779
l('=======> testing run many --with-deps complete');
80+
81+
l('=======> testing run many --configuration');
82+
83+
const buildConfig = l(
84+
runCLI(
85+
`run-many --target=build --projects="${appA},${libA}" --configuration=production`
86+
)
87+
);
88+
expect(buildConfig).toContain(`Running target build for projects:`);
89+
expect(buildConfig).toContain(`build ${appA} --configuration production`);
90+
expect(buildConfig).toContain(`build ${libA}`);
91+
expect(buildConfig).toContain('Running target "build" succeeded');
92+
93+
l('=======> testing run many --configuration');
7894
}, 1000000);
7995
});
8096
});

e2e/tasks-runner-v2.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
forEachCli(() => {
1414
describe('Task Runner V2', () => {
1515
describe('run-one with deps', () => {
16-
it('should be able to run tasks in parallel', () => {
16+
it('should be able to run the task for the specified project and its dependencies', () => {
1717
newProject();
1818

1919
updateFile('nx.json', c => {

packages/tao/src/commands/run.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,14 @@ export function validateTargetAndConfiguration(
103103
}
104104
const targets = architect.targets;
105105

106+
const availableTargets = [...targets.keys()];
106107
const target = targets.get(opts.target);
107108
if (!target) {
108109
throw new Error(
109110
`Could not find target "${opts.target}" in the ${
110111
opts.project
111112
} project. Valid targets are: ${terminal.bold(
112-
Object.keys(targets).join(', ')
113+
availableTargets.join(', ')
113114
)}`
114115
);
115116
}

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

+7-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from '../core/project-graph';
1515
import { calculateFileChanges, readEnvironment } from '../core/file-utils';
1616
import { printAffected } from './print-affected';
17-
import { projectHasTargetAndConfiguration } from '../utils/project-has-target-and-configuration';
17+
import { projectHasTarget } from '../utils/project-graph-utils';
1818
import { DefaultReporter } from '../tasks-runner/default-reporter';
1919

2020
export function affected(command: string, parsedArgs: yargs.Arguments): void {
@@ -87,12 +87,12 @@ export function affected(command: string, parsedArgs: yargs.Arguments): void {
8787

8888
case 'print-affected':
8989
if (nxArgs.target) {
90-
const projectWithTargetAndConfig = allProjectsWithTargetAndConfiguration(
90+
const projectsWithTarget = allProjectsWithTarget(
9191
affectedProjects,
9292
nxArgs
9393
);
9494
printAffected(
95-
projectWithTargetAndConfig,
95+
projectsWithTarget,
9696
affectedProjects,
9797
projectGraph,
9898
nxArgs,
@@ -104,13 +104,13 @@ export function affected(command: string, parsedArgs: yargs.Arguments): void {
104104
break;
105105

106106
case 'affected': {
107-
const projectWithTargetAndConfig = allProjectsWithTargetAndConfiguration(
107+
const projectsWithTarget = allProjectsWithTarget(
108108
affectedProjects,
109109
nxArgs
110110
);
111111
printArgsWarning(nxArgs);
112112
runCommand(
113-
projectWithTargetAndConfig,
113+
projectsWithTarget,
114114
projectGraph,
115115
env,
116116
nxArgs,
@@ -126,13 +126,8 @@ export function affected(command: string, parsedArgs: yargs.Arguments): void {
126126
}
127127
}
128128

129-
function allProjectsWithTargetAndConfiguration(
130-
projects: ProjectGraphNode[],
131-
nxArgs: NxArgs
132-
) {
133-
return projects.filter(p =>
134-
projectHasTargetAndConfiguration(p, nxArgs.target, nxArgs.configuration)
135-
);
129+
function allProjectsWithTarget(projects: ProjectGraphNode[], nxArgs: NxArgs) {
130+
return projects.filter(p => projectHasTarget(p, nxArgs.target));
136131
}
137132

138133
function printError(e: any, verbose?: boolean) {

packages/workspace/src/command-line/run-many.ts

+7-17
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import * as yargs from 'yargs';
22
import { runCommand } from '../tasks-runner/run-command';
3-
import { splitArgsIntoNxArgsAndOverrides, NxArgs } from './utils';
4-
import { output } from '../utils/output';
3+
import { NxArgs, splitArgsIntoNxArgsAndOverrides } from './utils';
54
import {
65
createProjectGraph,
76
ProjectGraph,
87
ProjectGraphNode,
98
withDeps
109
} from '../core/project-graph';
1110
import { readEnvironment } from '../core/file-utils';
12-
import { projectHasTargetAndConfiguration } from '../utils/project-has-target-and-configuration';
1311
import { DefaultReporter } from '../tasks-runner/default-reporter';
12+
import { projectHasTarget } from '../utils/project-graph-utils';
13+
import { output } from '@nrwl/workspace';
1414

1515
export function runMany(parsedArgs: yargs.Arguments): void {
1616
const { nxArgs, overrides } = splitArgsIntoNxArgsAndOverrides(
@@ -37,11 +37,7 @@ export function runMany(parsedArgs: yargs.Arguments): void {
3737
function projectsToRun(nxArgs: NxArgs, projectGraph: ProjectGraph) {
3838
const allProjects = Object.values(projectGraph.nodes);
3939
if (nxArgs.all) {
40-
return runnableForTargetAndConfiguration(
41-
allProjects,
42-
nxArgs.target,
43-
nxArgs.configuration
44-
);
40+
return runnableForTarget(allProjects, nxArgs.target);
4541
} else {
4642
checkForInvalidProjects(nxArgs, allProjects);
4743
let selectedProjects = allProjects.filter(
@@ -52,12 +48,7 @@ function projectsToRun(nxArgs: NxArgs, projectGraph: ProjectGraph) {
5248
withDeps(projectGraph, selectedProjects).nodes
5349
);
5450
}
55-
return runnableForTargetAndConfiguration(
56-
selectedProjects,
57-
nxArgs.target,
58-
nxArgs.configuration,
59-
true
60-
);
51+
return runnableForTarget(selectedProjects, nxArgs.target, true);
6152
}
6253
}
6354

@@ -73,17 +64,16 @@ function checkForInvalidProjects(
7364
}
7465
}
7566

76-
function runnableForTargetAndConfiguration(
67+
function runnableForTarget(
7768
projects: ProjectGraphNode[],
7869
target: string,
79-
configuration?: string,
8070
strict = false
8171
): ProjectGraphNode[] {
8272
const notRunnable = [];
8373
const runnable = [];
8474

8575
for (let project of projects) {
86-
if (projectHasTargetAndConfiguration(project, target, configuration)) {
76+
if (projectHasTarget(project, target)) {
8777
runnable.push(project);
8878
} else {
8979
notRunnable.push(project);

packages/workspace/src/command-line/run-one.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import { runCommand } from '../tasks-runner/run-command';
2-
import {
3-
createProjectGraph,
4-
onlyWorkspaceProjects,
5-
ProjectGraph,
6-
withDeps
7-
} from '../core/project-graph';
2+
import { createProjectGraph, ProjectGraph } from '../core/project-graph';
83
import { readEnvironment } from '../core/file-utils';
94
import { EmptyReporter } from '../tasks-runner/empty-reporter';
105
import { splitArgsIntoNxArgsAndOverrides } from './utils';
11-
import { DefaultReporter } from '../tasks-runner/default-reporter';
6+
import { projectHasTarget } from '../utils/project-graph-utils';
127

138
export function runOne(opts: {
149
project: string;
@@ -30,11 +25,12 @@ export function runOne(opts: {
3025
const { projects, projectsMap } = getProjects(
3126
projectGraph,
3227
nxArgs.withDeps,
33-
opts.project
28+
opts.project,
29+
opts.target
3430
);
3531
const env = readEnvironment(opts.target, projectsMap);
3632
const reporter = nxArgs.withDeps
37-
? new DefaultReporter()
33+
? new (require(`../tasks-runner/default-reporter`)).DefaultReporter()
3834
: new EmptyReporter();
3935

4036
runCommand(projects, projectGraph, env, nxArgs, overrides, reporter);
@@ -43,20 +39,24 @@ export function runOne(opts: {
4339
function getProjects(
4440
projectGraph: ProjectGraph,
4541
includeDeps: boolean,
46-
project: string
47-
) {
42+
project: string,
43+
target: string
44+
): any {
4845
let projects = [projectGraph.nodes[project]];
4946
let projectsMap = {
5047
[project]: projectGraph.nodes[project]
5148
};
5249

5350
if (includeDeps) {
54-
const projectWithDeps = onlyWorkspaceProjects(
55-
withDeps(projectGraph, projects)
56-
).nodes;
51+
const s = require(`../core/project-graph`);
52+
const deps = s.onlyWorkspaceProjects(s.withDeps(projectGraph, projects))
53+
.nodes;
54+
const projectsWithTarget = Object.values(deps).filter((p: any) =>
55+
projectHasTarget(p, target)
56+
);
5757
return {
58-
projects: Object.values(projectWithDeps),
59-
projectsMap: projectWithDeps
58+
projects: projectsWithTarget,
59+
projectsMap: deps
6060
};
6161
} else {
6262
return { projects, projectsMap };

packages/workspace/src/core/project-graph/project-graph-models.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ export enum DependencyType {
1616
export interface ProjectGraphNode<T extends {} = {}> {
1717
type: string;
1818
name: string;
19-
data: T & { files: FileData[]; [k: string]: any };
19+
data: T & {
20+
architect?: { [k: string]: any };
21+
files: FileData[];
22+
[k: string]: any;
23+
};
2024
}
2125

2226
export type ProjectGraphNodeRecords = Record<string, ProjectGraphNode>;

packages/workspace/src/tasks-runner/run-command.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Environment, NxJson } from '../core/shared-interfaces';
88
import { NxArgs } from '@nrwl/workspace/src/command-line/utils';
99
import { isRelativePath } from '../utils/fileutils';
1010
import { Hasher } from './hasher';
11+
import { projectHasTargetAndConfiguration } from '../utils/project-graph-utils';
1112

1213
type RunArgs = yargs.Arguments & ReporterArgs;
1314

@@ -26,14 +27,14 @@ export async function runCommand<T extends RunArgs>(
2627
...overrides
2728
});
2829

29-
const tasks: Task[] = projectsToRun.map(project =>
30-
createTask({
30+
const tasks: Task[] = projectsToRun.map(project => {
31+
return createTask({
3132
project,
3233
target: nxArgs.target,
3334
configuration: nxArgs.configuration,
3435
overrides: overrides
35-
})
36-
);
36+
});
37+
});
3738

3839
if (tasksRunner !== require('./default-tasks-runner').defaultTasksRunner) {
3940
const hasher = new Hasher(projectGraph, nxJson, tasksOptions);
@@ -96,10 +97,17 @@ export function createTask({
9697
configuration,
9798
overrides
9899
}: TaskParams): Task {
100+
const config = projectHasTargetAndConfiguration(
101+
project,
102+
target,
103+
configuration
104+
)
105+
? configuration
106+
: undefined;
99107
const qualifiedTarget = {
100108
project: project.name,
101109
target,
102-
configuration
110+
configuration: config
103111
};
104112
return {
105113
id: getId(qualifiedTarget),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ProjectGraphNode } from '../core/project-graph/project-graph-models';
2+
3+
export function projectHasTarget(project: ProjectGraphNode, target: string) {
4+
return (
5+
project.data && project.data.architect && project.data.architect[target]
6+
);
7+
}
8+
9+
export function projectHasTargetAndConfiguration(
10+
project: ProjectGraphNode,
11+
target: string,
12+
configuration: string
13+
) {
14+
return (
15+
projectHasTarget(project, target) &&
16+
project.data.architect[target].configurations &&
17+
project.data.architect[target].configurations[configuration]
18+
);
19+
}

packages/workspace/src/utils/project-has-target-and-configuration.ts

-24
This file was deleted.

0 commit comments

Comments
 (0)