Skip to content

Commit c814959

Browse files
committed
fixup! feat(@schematics/angular): use library builder and add entry-point schematic
1 parent 98c90c5 commit c814959

3 files changed

Lines changed: 69 additions & 12 deletions

File tree

‎packages/angular/cli/src/command-builder/utilities/schematic-engine-host.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ const legacyModules: Record<string, unknown> = {
117117
},
118118
'@schematics/angular/utility/project': {
119119
buildDefaultPath(project: { sourceRoot?: string; root: string; projectType: string }): string {
120-
const root = project.sourceRoot ? `/${project.sourceRoot}/` : `/${project.root}/src/`;
120+
const isLibraryAtRoot =
121+
project.projectType === 'library' && project.sourceRoot === project.root;
122+
const root =
123+
project.sourceRoot && !isLibraryAtRoot
124+
? `/${project.sourceRoot}/`
125+
: `/${project.root}/src/`;
121126

122127
return `${root}${project.projectType === 'application' ? 'app' : 'lib'}`;
123128
},

‎packages/schematics/angular/utility/workspace.ts‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,11 @@ export async function writeWorkspace(
116116
* @param project The project which will have its default path generated.
117117
*/
118118
export function buildDefaultPath(project: workspaces.ProjectDefinition): string {
119-
let root: string;
120-
if (project.extensions['projectType'] === ProjectType.Library) {
121-
root =
122-
project.sourceRoot && project.sourceRoot !== project.root
123-
? `/${project.sourceRoot}/`
124-
: `/${project.root}/src/`;
125-
} else {
126-
root = `/${project.sourceRoot || project.root}/`; // fallback for non-library projects
127-
}
128-
119+
const isLibraryAtRoot =
120+
project.extensions['projectType'] === ProjectType.Library &&
121+
project.sourceRoot === project.root;
122+
const root =
123+
project.sourceRoot && !isLibraryAtRoot ? `/${project.sourceRoot}/` : `/${project.root}/src/`;
129124
const projectDirName =
130125
project.extensions['projectType'] === ProjectType.Application ? 'app' : 'lib';
131126

‎packages/schematics/angular/utility/workspace_spec.ts‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import { workspaces } from '@angular-devkit/core';
910
import { EmptyTree, Rule, SchematicContext, Tree, callRule } from '@angular-devkit/schematics';
10-
import { getWorkspace as readWorkspace, updateWorkspace, writeWorkspace } from './workspace';
11+
import {
12+
buildDefaultPath,
13+
getWorkspace as readWorkspace,
14+
updateWorkspace,
15+
writeWorkspace,
16+
} from './workspace';
17+
import { ProjectType } from './workspace-models';
1118

1219
const TEST_WORKSPACE_CONTENT = JSON.stringify({
1320
version: 1,
@@ -126,3 +133,53 @@ describe('updateWorkspace', () => {
126133
expect(tree.read('/followup.txt')?.toString()).toContain('12345');
127134
});
128135
});
136+
137+
describe('buildDefaultPath', () => {
138+
function createProject(
139+
projectType: ProjectType | undefined,
140+
root: string,
141+
sourceRoot?: string,
142+
): workspaces.ProjectDefinition {
143+
return {
144+
root,
145+
sourceRoot,
146+
extensions: projectType ? { projectType } : {},
147+
targets: new workspaces.TargetDefinitionCollection(),
148+
};
149+
}
150+
151+
it('returns /<sourceRoot>/app for an application with sourceRoot', () => {
152+
const project = createProject(ProjectType.Application, 'projects/app', 'projects/app/src');
153+
expect(buildDefaultPath(project)).toBe('/projects/app/src/app');
154+
});
155+
156+
it('returns /<sourceRoot>/app for an application with sourceRoot equal to root', () => {
157+
const project = createProject(ProjectType.Application, 'projects/app', 'projects/app');
158+
expect(buildDefaultPath(project)).toBe('/projects/app/app');
159+
});
160+
161+
it('returns /<root>/src/app for an application without sourceRoot', () => {
162+
const project = createProject(ProjectType.Application, 'projects/app');
163+
expect(buildDefaultPath(project)).toBe('/projects/app/src/app');
164+
});
165+
166+
it('returns /<root>/src/lib for a library with sourceRoot equal to root', () => {
167+
const project = createProject(ProjectType.Library, 'projects/my-lib', 'projects/my-lib');
168+
expect(buildDefaultPath(project)).toBe('/projects/my-lib/src/lib');
169+
});
170+
171+
it('returns /<sourceRoot>/lib for a library with a distinct sourceRoot', () => {
172+
const project = createProject(ProjectType.Library, 'projects/my-lib', 'projects/my-lib/src');
173+
expect(buildDefaultPath(project)).toBe('/projects/my-lib/src/lib');
174+
});
175+
176+
it('returns /<sourceRoot>/lib for a library with a custom sourceRoot', () => {
177+
const project = createProject(ProjectType.Library, 'projects/my-lib', 'projects/my-lib/custom');
178+
expect(buildDefaultPath(project)).toBe('/projects/my-lib/custom/lib');
179+
});
180+
181+
it('returns /<root>/src/lib for a library without sourceRoot', () => {
182+
const project = createProject(ProjectType.Library, 'projects/my-lib');
183+
expect(buildDefaultPath(project)).toBe('/projects/my-lib/src/lib');
184+
});
185+
});

0 commit comments

Comments
 (0)