(workspace: AngularWorkspace, location: string)
| 261 | } |
| 262 | |
| 263 | function 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 | |
| 305 | export function getProjectByCwd(workspace: AngularWorkspace): string | null { |
| 306 | if (workspace.projects.size === 1) { |
no test coverage detected