-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
workspace-projects.ts
104 lines (91 loc) · 3.06 KB
/
workspace-projects.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
import * as minimatch from 'minimatch';
import { TouchedProjectLocator } from '../affected-project-graph-models';
import { NxJsonConfiguration } from '../../../config/nx-json';
import {
createProjectRootMappings,
findProjectForPath,
} from '../../utils/find-project-for-path';
export const getTouchedProjects: TouchedProjectLocator = (
touchedFiles,
projectGraphNodes
): string[] => {
const projectRootMap = createProjectRootMappings(projectGraphNodes);
return touchedFiles.reduce((affected, f) => {
const matchingProject = findProjectForPath(f.file, projectRootMap);
if (matchingProject) {
affected.push(matchingProject);
}
return affected;
}, []);
};
export const getImplicitlyTouchedProjects: TouchedProjectLocator = (
fileChanges,
projectGraphNodes,
nxJson
): string[] => {
const implicits = {};
const globalFiles = [...extractGlobalFilesFromInputs(nxJson), 'nx.json'];
globalFiles.forEach((file) => {
implicits[file] = '*' as any;
});
Object.values(projectGraphNodes || {}).forEach((node) => {
[
...extractFilesFromNamedInputs(node.data.namedInputs),
...extractFilesFromTargetInputs(node.data.targets),
].forEach((input) => {
implicits[input] ??= [];
if (Array.isArray(implicits[input])) {
implicits[input].push(node.name);
}
});
});
const touched = new Set<string>();
for (const [pattern, projects] of Object.entries(implicits)) {
const implicitDependencyWasChanged = fileChanges.some((f) =>
minimatch(f.file, pattern, { dot: true })
);
if (!implicitDependencyWasChanged) {
continue;
}
// File change affects all projects, just return all projects.
if (projects === '*') {
return Object.keys(projectGraphNodes);
} else if (Array.isArray(projects)) {
projects.forEach((project) => touched.add(project));
}
}
return Array.from(touched);
};
export function extractGlobalFilesFromInputs(nxJson: NxJsonConfiguration) {
const globalFiles = [];
globalFiles.push(...extractFilesFromNamedInputs(nxJson.namedInputs));
globalFiles.push(...extractFilesFromTargetInputs(nxJson.targetDefaults));
return globalFiles;
}
function extractFilesFromNamedInputs(namedInputs: any) {
const files = [];
for (const inputs of Object.values(namedInputs || {})) {
files.push(...extractFilesFromInputs(inputs));
}
return files;
}
function extractFilesFromTargetInputs(targets: any) {
const globalFiles = [];
for (const target of Object.values(targets || {})) {
if ((target as any).inputs) {
globalFiles.push(...extractFilesFromInputs((target as any).inputs));
}
}
return globalFiles;
}
function extractFilesFromInputs(inputs: any) {
const globalFiles = [];
for (const input of inputs) {
if (typeof input === 'string' && input.startsWith('{workspaceRoot}/')) {
globalFiles.push(input.substring('{workspaceRoot}/'.length));
} else if (input.fileset && input.fileset.startsWith('{workspaceRoot}/')) {
globalFiles.push(input.fileset.substring('{workspaceRoot}/'.length));
}
}
return globalFiles;
}