-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
print-affected.ts
112 lines (104 loc) · 3.26 KB
/
print-affected.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { getCommandAsString, getOutputs } from '../tasks-runner/utils';
import * as yargs from 'yargs';
import type { NxArgs } from '../utils/command-line-utils';
import { ProjectGraph, ProjectGraphProjectNode } from '../config/project-graph';
import { Task } from '../config/task-graph';
import { Environment } from './read-environment';
import { ProcessTasks } from 'nx/src/tasks-runner/create-task-graph';
export async function printAffected(
affectedProjectsWithTargetAndConfig: ProjectGraphProjectNode[],
affectedProjects: ProjectGraphProjectNode[],
projectGraph: ProjectGraph,
{ nxJson }: Environment,
nxArgs: NxArgs,
overrides: yargs.Arguments
) {
const projectNames = affectedProjects
.filter((p) => (nxArgs.type ? p.type === nxArgs.type : true))
.map((p) => p.name);
const tasksJson = await createTasks(
affectedProjectsWithTargetAndConfig.filter((p) =>
nxArgs.type ? p.type === nxArgs.type : true
),
projectGraph,
nxArgs,
overrides
);
const result = {
tasks: tasksJson,
projects: projectNames,
projectGraph: serializeProjectGraph(projectGraph),
};
if (nxArgs.select) {
console.log(selectPrintAffected(result, nxArgs.select));
} else {
console.log(JSON.stringify(selectPrintAffected(result, null), null, 2));
}
}
async function createTasks(
affectedProjectsWithTargetAndConfig: ProjectGraphProjectNode[],
projectGraph: ProjectGraph,
nxArgs: NxArgs,
overrides: yargs.Arguments
) {
const tasks: Task[] = affectedProjectsWithTargetAndConfig.map(
(affectedProject) => {
const p = new ProcessTasks({}, projectGraph);
const resolvedConfiguration = p.resolveConfiguration(
affectedProject,
nxArgs.target,
nxArgs.configuration
);
return p.createTask(
p.getId(affectedProject.name, nxArgs.target, resolvedConfiguration),
affectedProject,
nxArgs.target,
resolvedConfiguration,
overrides
);
}
);
return tasks.map((task, index) => ({
id: task.id,
overrides,
target: task.target,
command: getCommandAsString(task),
outputs: getOutputs(projectGraph.nodes, task),
}));
}
function serializeProjectGraph(projectGraph: ProjectGraph) {
const nodes = Object.values(projectGraph.nodes).map((n) => n.name);
return { nodes, dependencies: projectGraph.dependencies };
}
export function selectPrintAffected(wholeJson: any, wholeSelect: string) {
if (!wholeSelect) return wholeJson;
return _select(wholeJson, wholeSelect);
function _select(json: any, select: string) {
if (select.indexOf('.') > -1) {
const [firstKey, ...restKeys] = select.split('.');
const first = json[firstKey];
throwIfEmpty(wholeSelect, first);
const rest = restKeys.join('.');
if (Array.isArray(first)) {
return first.map((q) => _select(q, rest)).join(', ');
} else {
return _select(first, rest);
}
} else {
const res = json[select];
throwIfEmpty(wholeSelect, res);
if (Array.isArray(res)) {
return res.join(', ');
} else {
return res;
}
}
}
}
function throwIfEmpty(select: string, value: any) {
if (value === undefined) {
throw new Error(
`Cannot select '${select}' in the results of print-affected.`
);
}
}