Skip to content

Commit

Permalink
feat(core): ability to pass extra target deps when invoking commands
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Jun 2, 2022
1 parent 0407d8a commit 7ce487e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
10 changes: 8 additions & 2 deletions packages/nx/src/command-line/affected.ts
Expand Up @@ -17,10 +17,15 @@ import { ProjectGraph, ProjectGraphProjectNode } from '../config/project-graph';
import { projectHasTarget } 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';

export async function affected(
command: 'apps' | 'libs' | 'graph' | 'print-affected' | 'affected',
parsedArgs: yargs.Arguments & RawNxArgs
parsedArgs: yargs.Arguments & RawNxArgs,
extraTargetDependencies: Record<
string,
(TargetDependencyConfig | string)[]
> = {}
): Promise<void> {
performance.mark('command-execution-begins');
const env = readEnvironment();
Expand Down Expand Up @@ -116,7 +121,8 @@ export async function affected(
env,
nxArgs,
overrides,
null
null,
extraTargetDependencies
);
break;
}
Expand Down
19 changes: 17 additions & 2 deletions packages/nx/src/command-line/run-many.ts
Expand Up @@ -9,8 +9,15 @@ import { performance } from 'perf_hooks';
import { ProjectGraph, ProjectGraphProjectNode } from '../config/project-graph';
import { createProjectGraphAsync } from '../project-graph/project-graph';
import { readEnvironment } from './read-environment';
import { TargetDependencyConfig } from '../config/workspace-json-project-json';

export async function runMany(parsedArgs: yargs.Arguments & RawNxArgs) {
export async function runMany(
parsedArgs: yargs.Arguments & RawNxArgs,
extraTargetDependencies: Record<
string,
(TargetDependencyConfig | string)[]
> = {}
) {
performance.mark('command-execution-begins');
const env = readEnvironment();
const { nxArgs, overrides } = splitArgsIntoNxArgsAndOverrides(
Expand All @@ -25,7 +32,15 @@ export async function runMany(parsedArgs: yargs.Arguments & RawNxArgs) {
const projectGraph = await createProjectGraphAsync();
const projects = projectsToRun(nxArgs, projectGraph);

await runCommand(projects, projectGraph, env, nxArgs, overrides, null);
await runCommand(
projects,
projectGraph,
env,
nxArgs,
overrides,
null,
extraTargetDependencies
);
}

function projectsToRun(
Expand Down
14 changes: 11 additions & 3 deletions packages/nx/src/command-line/run-one.ts
Expand Up @@ -9,11 +9,18 @@ import { workspaceRoot } from '../utils/workspace-root';
import { splitTarget } from '../utils/split-target';
import { output } from '../utils/output';
import { readEnvironment } from './read-environment';
import { ProjectsConfigurations } from '../config/workspace-json-project-json';
import {
ProjectsConfigurations,
TargetDependencyConfig,
} from '../config/workspace-json-project-json';

export async function runOne(
cwd: string,
args: { [k: string]: any }
args: { [k: string]: any },
extraTargetDependencies: Record<
string,
(TargetDependencyConfig | string)[]
> = {}
): Promise<void> {
performance.mark('command-execution-begins');
performance.measure('code-loading', 'init-local', 'command-execution-begins');
Expand Down Expand Up @@ -51,7 +58,8 @@ export async function runOne(
env,
nxArgs,
overrides,
opts.project
opts.project,
extraTargetDependencies
);
}

Expand Down
32 changes: 30 additions & 2 deletions packages/nx/src/tasks-runner/run-command.ts
Expand Up @@ -18,6 +18,7 @@ import { NxJsonConfiguration } from '../config/nx-json';
import { Task } from '../config/task-graph';
import { createTaskGraph } from './create-task-graph';
import { findCycle, makeAcyclic } from './task-graph-utils';
import { TargetDependencyConfig } from 'nx/src/config/workspace-json-project-json';

async function getTerminalOutputLifeCycle(
initiatingProject: string,
Expand Down Expand Up @@ -79,11 +80,15 @@ export async function runCommand(
{ nxJson }: { nxJson: NxJsonConfiguration },
nxArgs: NxArgs,
overrides: any,
initiatingProject: string | null
initiatingProject: string | null,
extraTargetDependencies: Record<string, (TargetDependencyConfig | string)[]>
) {
const { tasksRunner, runnerOptions } = getRunner(nxArgs, nxJson);

const defaultDependencyConfigs = nxJson.targetDependencies;
const defaultDependencyConfigs = mergeTargetDependencies(
nxJson.targetDependencies,
extraTargetDependencies
);
const projectNames = projectsToRun.map((t) => t.name);
const taskGraph = createTaskGraph(
projectGraph,
Expand Down Expand Up @@ -174,6 +179,29 @@ export async function runCommand(
process.exit(anyFailures ? 1 : 0);
}

function mergeTargetDependencies(
a: Record<string, (TargetDependencyConfig | string)[]> | null,
b: Record<string, (TargetDependencyConfig | string)[]> | null
): Record<string, (TargetDependencyConfig | string)[]> {
const res = {};
if (a) {
Object.keys(a).forEach((k) => {
res[k] = a[k];
});
}
if (b) {
Object.keys(b).forEach((k) => {
if (res[k]) {
res[k] = [...res[k], b[k]];
} else {
res[k] = b[k];
}
});

return res;
}
}

async function anyFailuresInPromise(
promise: Promise<{ [id: string]: TaskStatus }>
) {
Expand Down

1 comment on commit 7ce487e

@vercel
Copy link

@vercel vercel bot commented on 7ce487e Jun 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.