-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
run-many.ts
85 lines (75 loc) · 2.68 KB
/
run-many.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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 { output } from '../utils/output';
import { connectToNxCloudUsingScan } from './connect-to-nx-cloud';
import { performance } from 'perf_hooks';
import { ProjectGraph, ProjectGraphProjectNode } from '../config/project-graph';
import { createProjectGraphAsync } from '../project-graph/project-graph';
import { readEnvironment } from './read-environment';
export async function runMany(parsedArgs: yargs.Arguments & RawNxArgs) {
performance.mark('command-execution-begins');
const { nxArgs, overrides } = splitArgsIntoNxArgsAndOverrides(
parsedArgs,
'run-many'
);
await connectToNxCloudUsingScan(nxArgs.scan);
const projectGraph = await createProjectGraphAsync();
const projects = projectsToRun(nxArgs, projectGraph);
const env = readEnvironment();
await runCommand(projects, projectGraph, env, nxArgs, overrides, null);
}
function projectsToRun(
nxArgs: NxArgs,
projectGraph: ProjectGraph
): ProjectGraphProjectNode[] {
const allProjects = Object.values(projectGraph.nodes);
const excludedProjects = new Set(nxArgs.exclude ?? []);
if (nxArgs.all) {
return runnableForTarget(allProjects, nxArgs.target).filter(
(proj) => !excludedProjects.has(proj.name)
);
}
checkForInvalidProjects(nxArgs, allProjects);
let selectedProjects = nxArgs.projects.map((name) =>
allProjects.find((project) => project.name === name)
);
return runnableForTarget(selectedProjects, nxArgs.target, true).filter(
(proj) => !excludedProjects.has(proj.name)
);
}
function checkForInvalidProjects(
nxArgs: NxArgs,
allProjects: ProjectGraphProjectNode[]
) {
const invalid = nxArgs.projects.filter(
(name) => !allProjects.find((p) => p.name === name)
);
if (invalid.length !== 0) {
throw new Error(`Invalid projects: ${invalid.join(', ')}`);
}
}
function runnableForTarget(
projects: ProjectGraphProjectNode[],
target: string,
strict = false
): ProjectGraphProjectNode[] {
const notRunnable = [] as ProjectGraphProjectNode[];
const runnable = [] as ProjectGraphProjectNode[];
for (let project of projects) {
if (projectHasTarget(project, target)) {
runnable.push(project);
} else {
notRunnable.push(project);
}
}
if (strict && notRunnable.length) {
output.warn({
title: `the following do not have configuration for "${target}"`,
bodyLines: notRunnable.map((p) => `- ${p.name}`),
});
}
return runnable;
}