MCPcopy Index your code
hub / github.com/angular/angular-cli / findProjectByPath

Function findProjectByPath

packages/angular/cli/src/utilities/config.ts:263–303  ·  view source on GitHub ↗
(workspace: AngularWorkspace, location: string)

Source from the content-addressed store, hash-verified

261}
262
263function findProjectByPath(workspace: AngularWorkspace, location: string): string | null {
264 const isInside = (base: string, potential: string): boolean => {
265 const absoluteBase = path.resolve(workspace.basePath, base);
266 const absolutePotential = path.resolve(workspace.basePath, potential);
267 const relativePotential = path.relative(absoluteBase, absolutePotential);
268 if (!relativePotential.startsWith('..') && !path.isAbsolute(relativePotential)) {
269 return true;
270 }
271
272 return false;
273 };
274
275 const projects = Array.from(workspace.projects)
276 .map(([name, project]) => [project.root, name] as [string, string])
277 .filter((tuple) => isInside(tuple[0], location))
278 // Sort tuples by depth, with the deeper ones first. Since the first member is a path and
279 // we filtered all invalid paths, the longest will be the deepest (and in case of equality
280 // the sort is stable and the first declared project will win).
281 .sort((a, b) => b[0].length - a[0].length);
282
283 if (projects.length === 0) {
284 return null;
285 } else if (projects.length > 1) {
286 const found = new Set<string>();
287 const sameRoots = projects.filter((v) => {
288 if (!found.has(v[0])) {
289 found.add(v[0]);
290
291 return false;
292 }
293
294 return true;
295 });
296 if (sameRoots.length > 0) {
297 // Ambiguous location - cannot determine a project
298 return null;
299 }
300 }
301
302 return projects[0][1];
303}
304
305export function getProjectByCwd(workspace: AngularWorkspace): string | null {
306 if (workspace.projects.size === 1) {

Callers 1

getProjectByCwdFunction · 0.85

Calls 4

isInsideFunction · 0.85
fromMethod · 0.80
hasMethod · 0.65
addMethod · 0.45

Tested by

no test coverage detected