Skip to content

Commit 9807291

Browse files
committed
refactor(@angular/cli): remove getProjectsByPath config util
We already have a similar method `getProjectByCwd` that does the same thing.
1 parent 09f8659 commit 9807291

File tree

2 files changed

+11
-59
lines changed

2 files changed

+11
-59
lines changed

packages/angular/cli/src/command-builder/schematics-command-module.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from '@angular-devkit/schematics/tools';
1616
import type { CheckboxQuestion, Question } from 'inquirer';
1717
import { Argv } from 'yargs';
18-
import { getProjectByCwd, getProjectsByPath, getSchematicDefaults } from '../utilities/config';
18+
import { getProjectByCwd, getSchematicDefaults } from '../utilities/config';
1919
import { isTTY } from '../utilities/tty';
2020
import {
2121
CommandModule,
@@ -364,32 +364,23 @@ export abstract class SchematicsCommandModule
364364
return undefined;
365365
}
366366

367-
const projectNames = getProjectsByPath(workspace, process.cwd(), workspace.basePath);
367+
const projectName = getProjectByCwd(workspace);
368+
if (projectName) {
369+
return projectName;
370+
}
368371

369-
if (projectNames.length === 1) {
370-
return projectNames[0];
371-
} else {
372-
if (projectNames.length > 1) {
372+
const defaultProjectName = workspace.extensions['defaultProject'];
373+
if (typeof defaultProjectName === 'string' && defaultProjectName) {
374+
if (!this.defaultProjectDeprecationWarningShown) {
373375
logger.warn(tags.oneLine`
374-
Two or more projects are using identical roots.
375-
Unable to determine project using current working directory.
376-
Using default workspace project instead.
377-
`);
378-
}
379-
380-
const defaultProjectName = workspace.extensions['defaultProject'];
381-
if (typeof defaultProjectName === 'string' && defaultProjectName) {
382-
if (!this.defaultProjectDeprecationWarningShown) {
383-
logger.warn(tags.oneLine`
384376
DEPRECATED: The 'defaultProject' workspace option has been deprecated.
385377
The project to use will be determined from the current working directory.
386378
`);
387379

388-
this.defaultProjectDeprecationWarningShown = true;
389-
}
390-
391-
return defaultProjectName;
380+
this.defaultProjectDeprecationWarningShown = true;
392381
}
382+
383+
return defaultProjectName;
393384
}
394385

395386
return undefined;

packages/angular/cli/src/utilities/config.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -393,42 +393,3 @@ export async function isWarningEnabled(warning: string): Promise<boolean> {
393393
// All warnings are enabled by default
394394
return result ?? true;
395395
}
396-
397-
export function getProjectsByPath(
398-
workspace: workspaces.WorkspaceDefinition,
399-
cwd: string,
400-
root: string,
401-
): string[] {
402-
if (workspace.projects.size === 1) {
403-
return Array.from(workspace.projects.keys());
404-
}
405-
406-
const isInside = (base: string, potential: string): boolean => {
407-
const absoluteBase = path.resolve(root, base);
408-
const absolutePotential = path.resolve(root, potential);
409-
const relativePotential = path.relative(absoluteBase, absolutePotential);
410-
if (!relativePotential.startsWith('..') && !path.isAbsolute(relativePotential)) {
411-
return true;
412-
}
413-
414-
return false;
415-
};
416-
417-
const projects = Array.from(workspace.projects.entries())
418-
.map(([name, project]) => [path.resolve(root, project.root), name] as [string, string])
419-
.filter((tuple) => isInside(tuple[0], cwd))
420-
// Sort tuples by depth, with the deeper ones first. Since the first member is a path and
421-
// we filtered all invalid paths, the longest will be the deepest (and in case of equality
422-
// the sort is stable and the first declared project will win).
423-
.sort((a, b) => b[0].length - a[0].length);
424-
425-
if (projects.length === 1) {
426-
return [projects[0][1]];
427-
} else if (projects.length > 1) {
428-
const firstPath = projects[0][0];
429-
430-
return projects.filter((v) => v[0] === firstPath).map((v) => v[1]);
431-
}
432-
433-
return [];
434-
}

0 commit comments

Comments
 (0)