fix(core): use default configuration when the provided one is not available

This commit is contained in:
Victor Savkin 2020-02-28 18:58:46 -05:00 committed by Victor Savkin
parent b70fe0535d
commit 115a1abd75
10 changed files with 86 additions and 77 deletions

View File

@ -13,11 +13,13 @@ forEachCli(() => {
describe('Run Many', () => { describe('Run Many', () => {
it('should build specific and all projects', () => { it('should build specific and all projects', () => {
newProject(); newProject();
const appA = uniq('appa-rand');
const libA = uniq('liba-rand'); const libA = uniq('liba-rand');
const libB = uniq('libb-rand'); const libB = uniq('libb-rand');
const libC = uniq('libc-rand'); const libC = uniq('libc-rand');
const libD = uniq('libd-rand'); const libD = uniq('libd-rand');
l(runCLI(`generate @nrwl/angular:app ${appA}`));
l(runCLI(`generate @nrwl/angular:lib ${libA} --publishable --defaults`)); l(runCLI(`generate @nrwl/angular:lib ${libA} --publishable --defaults`));
l(runCLI(`generate @nrwl/angular:lib ${libB} --publishable --defaults`)); l(runCLI(`generate @nrwl/angular:lib ${libB} --publishable --defaults`));
l(runCLI(`generate @nrwl/angular:lib ${libC} --publishable --defaults`)); l(runCLI(`generate @nrwl/angular:lib ${libC} --publishable --defaults`));
@ -75,6 +77,20 @@ forEachCli(() => {
expect(buildWithDeps).toContain('Running target "build" succeeded'); expect(buildWithDeps).toContain('Running target "build" succeeded');
l('=======> testing run many --with-deps complete'); l('=======> testing run many --with-deps complete');
l('=======> testing run many --configuration');
const buildConfig = l(
runCLI(
`run-many --target=build --projects="${appA},${libA}" --configuration=production`
)
);
expect(buildConfig).toContain(`Running target build for projects:`);
expect(buildConfig).toContain(`build ${appA} --configuration production`);
expect(buildConfig).toContain(`build ${libA}`);
expect(buildConfig).toContain('Running target "build" succeeded');
l('=======> testing run many --configuration');
}, 1000000); }, 1000000);
}); });
}); });

View File

@ -13,7 +13,7 @@ import {
forEachCli(() => { forEachCli(() => {
describe('Task Runner V2', () => { describe('Task Runner V2', () => {
describe('run-one with deps', () => { describe('run-one with deps', () => {
it('should be able to run tasks in parallel', () => { it('should be able to run the task for the specified project and its dependencies', () => {
newProject(); newProject();
updateFile('nx.json', c => { updateFile('nx.json', c => {

View File

@ -103,13 +103,14 @@ export function validateTargetAndConfiguration(
} }
const targets = architect.targets; const targets = architect.targets;
const availableTargets = [...targets.keys()];
const target = targets.get(opts.target); const target = targets.get(opts.target);
if (!target) { if (!target) {
throw new Error( throw new Error(
`Could not find target "${opts.target}" in the ${ `Could not find target "${opts.target}" in the ${
opts.project opts.project
} project. Valid targets are: ${terminal.bold( } project. Valid targets are: ${terminal.bold(
Object.keys(targets).join(', ') availableTargets.join(', ')
)}` )}`
); );
} }

View File

@ -14,7 +14,7 @@ import {
} from '../core/project-graph'; } from '../core/project-graph';
import { calculateFileChanges, readEnvironment } from '../core/file-utils'; import { calculateFileChanges, readEnvironment } from '../core/file-utils';
import { printAffected } from './print-affected'; import { printAffected } from './print-affected';
import { projectHasTargetAndConfiguration } from '../utils/project-has-target-and-configuration'; import { projectHasTarget } from '../utils/project-graph-utils';
import { DefaultReporter } from '../tasks-runner/default-reporter'; import { DefaultReporter } from '../tasks-runner/default-reporter';
export function affected(command: string, parsedArgs: yargs.Arguments): void { export function affected(command: string, parsedArgs: yargs.Arguments): void {
@ -87,12 +87,12 @@ export function affected(command: string, parsedArgs: yargs.Arguments): void {
case 'print-affected': case 'print-affected':
if (nxArgs.target) { if (nxArgs.target) {
const projectWithTargetAndConfig = allProjectsWithTargetAndConfiguration( const projectsWithTarget = allProjectsWithTarget(
affectedProjects, affectedProjects,
nxArgs nxArgs
); );
printAffected( printAffected(
projectWithTargetAndConfig, projectsWithTarget,
affectedProjects, affectedProjects,
projectGraph, projectGraph,
nxArgs, nxArgs,
@ -104,13 +104,13 @@ export function affected(command: string, parsedArgs: yargs.Arguments): void {
break; break;
case 'affected': { case 'affected': {
const projectWithTargetAndConfig = allProjectsWithTargetAndConfiguration( const projectsWithTarget = allProjectsWithTarget(
affectedProjects, affectedProjects,
nxArgs nxArgs
); );
printArgsWarning(nxArgs); printArgsWarning(nxArgs);
runCommand( runCommand(
projectWithTargetAndConfig, projectsWithTarget,
projectGraph, projectGraph,
env, env,
nxArgs, nxArgs,
@ -126,13 +126,8 @@ export function affected(command: string, parsedArgs: yargs.Arguments): void {
} }
} }
function allProjectsWithTargetAndConfiguration( function allProjectsWithTarget(projects: ProjectGraphNode[], nxArgs: NxArgs) {
projects: ProjectGraphNode[], return projects.filter(p => projectHasTarget(p, nxArgs.target));
nxArgs: NxArgs
) {
return projects.filter(p =>
projectHasTargetAndConfiguration(p, nxArgs.target, nxArgs.configuration)
);
} }
function printError(e: any, verbose?: boolean) { function printError(e: any, verbose?: boolean) {

View File

@ -1,7 +1,6 @@
import * as yargs from 'yargs'; import * as yargs from 'yargs';
import { runCommand } from '../tasks-runner/run-command'; import { runCommand } from '../tasks-runner/run-command';
import { splitArgsIntoNxArgsAndOverrides, NxArgs } from './utils'; import { NxArgs, splitArgsIntoNxArgsAndOverrides } from './utils';
import { output } from '../utils/output';
import { import {
createProjectGraph, createProjectGraph,
ProjectGraph, ProjectGraph,
@ -9,8 +8,9 @@ import {
withDeps withDeps
} from '../core/project-graph'; } from '../core/project-graph';
import { readEnvironment } from '../core/file-utils'; import { readEnvironment } from '../core/file-utils';
import { projectHasTargetAndConfiguration } from '../utils/project-has-target-and-configuration';
import { DefaultReporter } from '../tasks-runner/default-reporter'; import { DefaultReporter } from '../tasks-runner/default-reporter';
import { projectHasTarget } from '../utils/project-graph-utils';
import { output } from '@nrwl/workspace';
export function runMany(parsedArgs: yargs.Arguments): void { export function runMany(parsedArgs: yargs.Arguments): void {
const { nxArgs, overrides } = splitArgsIntoNxArgsAndOverrides( const { nxArgs, overrides } = splitArgsIntoNxArgsAndOverrides(
@ -37,11 +37,7 @@ export function runMany(parsedArgs: yargs.Arguments): void {
function projectsToRun(nxArgs: NxArgs, projectGraph: ProjectGraph) { function projectsToRun(nxArgs: NxArgs, projectGraph: ProjectGraph) {
const allProjects = Object.values(projectGraph.nodes); const allProjects = Object.values(projectGraph.nodes);
if (nxArgs.all) { if (nxArgs.all) {
return runnableForTargetAndConfiguration( return runnableForTarget(allProjects, nxArgs.target);
allProjects,
nxArgs.target,
nxArgs.configuration
);
} else { } else {
checkForInvalidProjects(nxArgs, allProjects); checkForInvalidProjects(nxArgs, allProjects);
let selectedProjects = allProjects.filter( let selectedProjects = allProjects.filter(
@ -52,12 +48,7 @@ function projectsToRun(nxArgs: NxArgs, projectGraph: ProjectGraph) {
withDeps(projectGraph, selectedProjects).nodes withDeps(projectGraph, selectedProjects).nodes
); );
} }
return runnableForTargetAndConfiguration( return runnableForTarget(selectedProjects, nxArgs.target, true);
selectedProjects,
nxArgs.target,
nxArgs.configuration,
true
);
} }
} }
@ -73,17 +64,16 @@ function checkForInvalidProjects(
} }
} }
function runnableForTargetAndConfiguration( function runnableForTarget(
projects: ProjectGraphNode[], projects: ProjectGraphNode[],
target: string, target: string,
configuration?: string,
strict = false strict = false
): ProjectGraphNode[] { ): ProjectGraphNode[] {
const notRunnable = []; const notRunnable = [];
const runnable = []; const runnable = [];
for (let project of projects) { for (let project of projects) {
if (projectHasTargetAndConfiguration(project, target, configuration)) { if (projectHasTarget(project, target)) {
runnable.push(project); runnable.push(project);
} else { } else {
notRunnable.push(project); notRunnable.push(project);

View File

@ -1,14 +1,9 @@
import { runCommand } from '../tasks-runner/run-command'; import { runCommand } from '../tasks-runner/run-command';
import { import { createProjectGraph, ProjectGraph } from '../core/project-graph';
createProjectGraph,
onlyWorkspaceProjects,
ProjectGraph,
withDeps
} from '../core/project-graph';
import { readEnvironment } from '../core/file-utils'; import { readEnvironment } from '../core/file-utils';
import { EmptyReporter } from '../tasks-runner/empty-reporter'; import { EmptyReporter } from '../tasks-runner/empty-reporter';
import { splitArgsIntoNxArgsAndOverrides } from './utils'; import { splitArgsIntoNxArgsAndOverrides } from './utils';
import { DefaultReporter } from '../tasks-runner/default-reporter'; import { projectHasTarget } from '../utils/project-graph-utils';
export function runOne(opts: { export function runOne(opts: {
project: string; project: string;
@ -30,11 +25,12 @@ export function runOne(opts: {
const { projects, projectsMap } = getProjects( const { projects, projectsMap } = getProjects(
projectGraph, projectGraph,
nxArgs.withDeps, nxArgs.withDeps,
opts.project opts.project,
opts.target
); );
const env = readEnvironment(opts.target, projectsMap); const env = readEnvironment(opts.target, projectsMap);
const reporter = nxArgs.withDeps const reporter = nxArgs.withDeps
? new DefaultReporter() ? new (require(`../tasks-runner/default-reporter`)).DefaultReporter()
: new EmptyReporter(); : new EmptyReporter();
runCommand(projects, projectGraph, env, nxArgs, overrides, reporter); runCommand(projects, projectGraph, env, nxArgs, overrides, reporter);
@ -43,20 +39,24 @@ export function runOne(opts: {
function getProjects( function getProjects(
projectGraph: ProjectGraph, projectGraph: ProjectGraph,
includeDeps: boolean, includeDeps: boolean,
project: string project: string,
) { target: string
): any {
let projects = [projectGraph.nodes[project]]; let projects = [projectGraph.nodes[project]];
let projectsMap = { let projectsMap = {
[project]: projectGraph.nodes[project] [project]: projectGraph.nodes[project]
}; };
if (includeDeps) { if (includeDeps) {
const projectWithDeps = onlyWorkspaceProjects( const s = require(`../core/project-graph`);
withDeps(projectGraph, projects) const deps = s.onlyWorkspaceProjects(s.withDeps(projectGraph, projects))
).nodes; .nodes;
const projectsWithTarget = Object.values(deps).filter((p: any) =>
projectHasTarget(p, target)
);
return { return {
projects: Object.values(projectWithDeps), projects: projectsWithTarget,
projectsMap: projectWithDeps projectsMap: deps
}; };
} else { } else {
return { projects, projectsMap }; return { projects, projectsMap };

View File

@ -16,7 +16,11 @@ export enum DependencyType {
export interface ProjectGraphNode<T extends {} = {}> { export interface ProjectGraphNode<T extends {} = {}> {
type: string; type: string;
name: string; name: string;
data: T & { files: FileData[]; [k: string]: any }; data: T & {
architect?: { [k: string]: any };
files: FileData[];
[k: string]: any;
};
} }
export type ProjectGraphNodeRecords = Record<string, ProjectGraphNode>; export type ProjectGraphNodeRecords = Record<string, ProjectGraphNode>;

View File

@ -8,6 +8,7 @@ import { Environment, NxJson } from '../core/shared-interfaces';
import { NxArgs } from '@nrwl/workspace/src/command-line/utils'; import { NxArgs } from '@nrwl/workspace/src/command-line/utils';
import { isRelativePath } from '../utils/fileutils'; import { isRelativePath } from '../utils/fileutils';
import { Hasher } from './hasher'; import { Hasher } from './hasher';
import { projectHasTargetAndConfiguration } from '../utils/project-graph-utils';
type RunArgs = yargs.Arguments & ReporterArgs; type RunArgs = yargs.Arguments & ReporterArgs;
@ -26,14 +27,14 @@ export async function runCommand<T extends RunArgs>(
...overrides ...overrides
}); });
const tasks: Task[] = projectsToRun.map(project => const tasks: Task[] = projectsToRun.map(project => {
createTask({ return createTask({
project, project,
target: nxArgs.target, target: nxArgs.target,
configuration: nxArgs.configuration, configuration: nxArgs.configuration,
overrides: overrides overrides: overrides
}) });
); });
if (tasksRunner !== require('./default-tasks-runner').defaultTasksRunner) { if (tasksRunner !== require('./default-tasks-runner').defaultTasksRunner) {
const hasher = new Hasher(projectGraph, nxJson, tasksOptions); const hasher = new Hasher(projectGraph, nxJson, tasksOptions);
@ -96,10 +97,17 @@ export function createTask({
configuration, configuration,
overrides overrides
}: TaskParams): Task { }: TaskParams): Task {
const config = projectHasTargetAndConfiguration(
project,
target,
configuration
)
? configuration
: undefined;
const qualifiedTarget = { const qualifiedTarget = {
project: project.name, project: project.name,
target, target,
configuration configuration: config
}; };
return { return {
id: getId(qualifiedTarget), id: getId(qualifiedTarget),

View File

@ -0,0 +1,19 @@
import { ProjectGraphNode } from '../core/project-graph/project-graph-models';
export function projectHasTarget(project: ProjectGraphNode, target: string) {
return (
project.data && project.data.architect && project.data.architect[target]
);
}
export function projectHasTargetAndConfiguration(
project: ProjectGraphNode,
target: string,
configuration: string
) {
return (
projectHasTarget(project, target) &&
project.data.architect[target].configurations &&
project.data.architect[target].configurations[configuration]
);
}

View File

@ -1,24 +0,0 @@
import { ProjectGraphNode } from '@nrwl/workspace/src/core/project-graph';
export function projectHasTargetAndConfiguration(
project: ProjectGraphNode,
target: string,
configuration?: string
) {
if (
!project.data ||
!project.data.architect ||
!project.data.architect[target]
) {
return false;
}
if (!configuration) {
return !!project.data.architect[target];
} else {
return (
project.data.architect[target].configurations &&
project.data.architect[target].configurations[configuration]
);
}
}