Skip to content

Commit dfac045

Browse files
committed
fix(@angular/cli): use workspace.getProjectByPath
1 parent 553a7bd commit dfac045

File tree

1 file changed

+4
-46
lines changed

1 file changed

+4
-46
lines changed

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

+4-46
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ import {
55
JsonAstObject,
66
JsonParseMode,
77
JsonValue,
8-
Path,
98
experimental,
10-
isAbsolute,
119
normalize,
1210
parseJsonAst,
13-
resolve,
14-
relative,
1511
virtualFs,
1612
} from '@angular-devkit/core';
1713
import { NodeJsSyncHost } from '@angular-devkit/core/node';
@@ -139,49 +135,11 @@ export function validateWorkspace(json: JsonValue) {
139135
return true;
140136
}
141137

142-
export function getProjectByCwd(workspace?: experimental.workspace.Workspace): string | null {
143-
if (!workspace) {
144-
workspace = getWorkspace('local');
145-
if (!workspace) {
146-
return null;
147-
}
148-
}
149-
150-
const projectNames = workspace.listProjectNames();
151-
if (projectNames.length === 1) {
152-
return projectNames[0];
153-
}
154-
155-
const cwd = normalize(process.cwd());
156-
const isInside = (base: Path, potential: Path): boolean => {
157-
const absoluteBase = resolve(workspace.root, base);
158-
const absolutePotential = resolve(workspace.root, potential);
159-
const relativePotential = relative(absoluteBase, absolutePotential);
160-
if (!relativePotential.startsWith('..') && !isAbsolute(relativePotential)) {
161-
return true;
162-
}
163-
164-
return false;
165-
};
166-
167-
const projects = workspace.listProjectNames()
168-
.map(name => [workspace.getProject(name).root, name] as [Path, string])
169-
.sort((a, b) => isInside(a[0], b[0]) ? 1 : 0);
170-
171-
for (const project of projects) {
172-
if (isInside(project[0], cwd)) {
173-
return project[1];
174-
}
175-
}
176-
177-
return null;
178-
}
179-
180138
export function getPackageManager(): string {
181139
let workspace = getWorkspace();
182140

183141
if (workspace) {
184-
const project = getProjectByCwd(workspace);
142+
const project = workspace.getProjectByPath(normalize(process.cwd()));
185143
if (project && workspace.getProjectCli(project)) {
186144
const value = workspace.getProjectCli(project)['packageManager'];
187145
if (typeof value == 'string') {
@@ -210,7 +168,7 @@ export function getDefaultSchematicCollection(): string {
210168
let workspace = getWorkspace('local');
211169

212170
if (workspace) {
213-
const project = getProjectByCwd(workspace);
171+
const project = workspace.getProjectByPath(normalize(process.cwd()));
214172
if (project && workspace.getProjectCli(project)) {
215173
const value = workspace.getProjectCli(project)['defaultCollection'];
216174
if (typeof value == 'string') {
@@ -267,7 +225,7 @@ export function getSchematicDefaults(collection: string, schematic: string, proj
267225
}
268226
}
269227

270-
project = project || getProjectByCwd(workspace);
228+
project = project || workspace.getProjectByPath(normalize(process.cwd()));
271229
if (project && workspace.getProjectSchematics(project)) {
272230
const schematicObject = workspace.getProjectSchematics(project)[fullName];
273231
if (schematicObject) {
@@ -287,7 +245,7 @@ export function isWarningEnabled(warning: string): boolean {
287245
let workspace = getWorkspace('local');
288246

289247
if (workspace) {
290-
const project = getProjectByCwd(workspace);
248+
const project = workspace.getProjectByPath(normalize(process.cwd()));
291249
if (project && workspace.getProjectCli(project)) {
292250
const warnings = workspace.getProjectCli(project)['warnings'];
293251
if (typeof warnings == 'object' && !Array.isArray(warnings)) {

0 commit comments

Comments
 (0)