-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
find-matching-projects.ts
265 lines (240 loc) · 7.8 KB
/
find-matching-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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import minimatch = require('minimatch');
import type { ProjectGraphProjectNode } from '../config/project-graph';
const validPatternTypes = [
'name', // Pattern is based on the project's name
'tag', // Pattern is based on the project's tags
'directory', // Pattern is based on the project's root directory
'unlabeled', // Pattern was passed without specifying a type
] as const;
type ProjectPatternType = typeof validPatternTypes[number];
interface ProjectPattern {
// If true, the pattern is an exclude pattern
exclude: boolean;
// The type of pattern to match against
type: ProjectPatternType;
// The pattern to match against
value: string;
}
const globCharacters = ['*', '|', '{', '}', '(', ')'];
/**
* Find matching project names given a list of potential project names or globs.
*
* @param patterns A list of project names or globs to match against.
* @param projects A map of {@link ProjectGraphProjectNode} by project name.
* @returns
*/
export function findMatchingProjects(
patterns: string[] = [],
projects: Record<string, ProjectGraphProjectNode>
): string[] {
if (!patterns.length || patterns.filter((p) => p.length).length === 0) {
return []; // Short circuit if called with no patterns
}
const projectNames = Object.keys(projects);
const selectedProjects: Set<string> = new Set();
const excludedProjects: Set<string> = new Set();
for (const stringPattern of patterns) {
if (!stringPattern.length) {
continue;
}
const pattern = parseStringPattern(stringPattern, projects);
// Handle wildcard with short-circuit, as its a common case with potentially
// large project sets and we can avoid the more expensive glob matching.
if (pattern.value === '*') {
for (const projectName of projectNames) {
if (pattern.exclude) {
excludedProjects.add(projectName);
} else {
selectedProjects.add(projectName);
}
}
continue;
}
switch (pattern.type) {
case 'tag': {
addMatchingProjectsByTag(
projectNames,
projects,
pattern,
excludedProjects,
selectedProjects
);
continue;
}
case 'name': {
addMatchingProjectsByName(
projectNames,
projects,
pattern,
excludedProjects,
selectedProjects
);
continue;
}
case 'directory': {
addMatchingProjectsByDirectory(
projectNames,
projects,
pattern,
excludedProjects,
selectedProjects
);
continue;
}
// Same thing as `type:unlabeled`. If no specific type is set,
// we can waterfall through the different types until we find a match
default: {
// The size of the selected and excluded projects set, before we
// start updating it with this pattern. If the size changes, we
// know we found a match and can skip the other types.
const originalSize = selectedProjects.size + excludedProjects.size;
addMatchingProjectsByName(
projectNames,
projects,
pattern,
excludedProjects,
selectedProjects
);
if (selectedProjects.size + excludedProjects.size > originalSize) {
// There was some match by name, don't check other types
continue;
}
addMatchingProjectsByDirectory(
projectNames,
projects,
pattern,
excludedProjects,
selectedProjects
);
if (selectedProjects.size + excludedProjects.size > originalSize) {
// There was some match by directory, don't check other types
// Note - this doesn't do anything currently, but preps for future
// types
continue;
}
}
}
}
for (const project of excludedProjects) {
selectedProjects.delete(project);
}
return Array.from(selectedProjects);
}
function addMatchingProjectsByDirectory(
projectNames: string[],
projects: Record<string, ProjectGraphProjectNode>,
pattern: ProjectPattern,
excludedProjects: Set<string>,
selectedProjects: Set<string>
) {
for (const projectName of projectNames) {
const root = projects[projectName].data.root;
if (getMatchingStringsWithCache(pattern.value, [root]).length > 0) {
(pattern.exclude ? excludedProjects : selectedProjects).add(projectName);
}
}
}
function addMatchingProjectsByName(
projectNames: string[],
projects: Record<string, ProjectGraphProjectNode>,
pattern: ProjectPattern,
excludedProjects: Set<string>,
selectedProjects: Set<string>
) {
if (projects[pattern.value]) {
(pattern.exclude ? excludedProjects : selectedProjects).add(pattern.value);
return;
}
if (!globCharacters.some((c) => pattern.value.includes(c))) {
return;
}
const matchedProjectNames = getMatchingStringsWithCache(
pattern.value,
projectNames
);
for (const projectName of matchedProjectNames) {
if (pattern.exclude) {
excludedProjects.add(projectName);
} else {
selectedProjects.add(projectName);
}
}
}
function addMatchingProjectsByTag(
projectNames: string[],
projects: Record<string, ProjectGraphProjectNode>,
pattern: ProjectPattern,
excludedProjects: Set<string>,
selectedProjects: Set<string>
) {
for (const projectName of projectNames) {
const tags = projects[projectName].data.tags || [];
if (tags.includes(pattern.value)) {
(pattern.exclude ? excludedProjects : selectedProjects).add(projectName);
continue;
}
if (!globCharacters.some((c) => pattern.value.includes(c))) {
continue;
}
if (getMatchingStringsWithCache(pattern.value, tags).length) {
(pattern.exclude ? excludedProjects : selectedProjects).add(projectName);
}
}
}
function parseStringPattern(
pattern: string,
projects: Record<string, ProjectGraphProjectNode>
): ProjectPattern {
const isExclude = pattern.startsWith('!');
// Support for things like: `!{type}:value`
if (isExclude) {
pattern = pattern.substring(1);
}
const indexOfFirstPotentialSeparator = pattern.indexOf(':');
// There is a project that matches directly
if (projects[pattern]) {
return { type: 'name', value: pattern, exclude: isExclude };
// The pattern does not contain a label
} else if (indexOfFirstPotentialSeparator === -1) {
return { type: 'unlabeled', value: pattern, exclude: isExclude };
// The pattern may contain a label
} else {
const potentialType = pattern.substring(0, indexOfFirstPotentialSeparator);
return {
type: isValidPatternType(potentialType) ? potentialType : 'unlabeled',
value: pattern.substring(indexOfFirstPotentialSeparator + 1),
exclude: isExclude,
};
}
}
function isValidPatternType(type: string): type is ProjectPatternType {
return validPatternTypes.includes(type as ProjectPatternType);
}
export const getMatchingStringsWithCache = (() => {
// Map< Pattern, Map< Item, Result >>
const minimatchCache = new Map<string, Map<string, boolean>>();
const regexCache = new Map<string, RegExp>();
return (pattern: string, items: string[]) => {
if (!minimatchCache.has(pattern)) {
minimatchCache.set(pattern, new Map());
}
const patternCache = minimatchCache.get(pattern)!;
if (!regexCache.has(pattern)) {
const regex = minimatch.makeRe(pattern, { dot: true });
if (regex) {
regexCache.set(pattern, regex);
} else {
throw new Error('Invalid glob pattern ' + pattern);
}
}
const matcher = regexCache.get(pattern);
return items.filter((item) => {
let entry = patternCache.get(item);
if (entry === undefined || entry === null) {
entry = item === pattern ? true : matcher.test(item);
patternCache.set(item, entry);
}
return entry;
});
};
})();