Skip to content

Commit 6289c25

Browse files
Broccohansl
authored andcommitted
fix(@schematics/angular): Fix generation within a library project
fixes #10344
1 parent 9cad002 commit 6289c25

File tree

10 files changed

+29
-9
lines changed

10 files changed

+29
-9
lines changed

packages/schematics/angular/class/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export default function (options: ClassOptions): Rule {
3232
const project = workspace.projects[options.project];
3333

3434
if (options.path === undefined) {
35-
options.path = `/${project.root}/src/app`;
35+
const projectDirName = project.projectType === 'application' ? 'app' : 'lib';
36+
options.path = `/${project.root}/src/${projectDirName}`;
3637
}
3738

3839
options.type = !!options.type ? `.${options.type}` : '';

packages/schematics/angular/component/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ export default function(options: ComponentOptions): Rule {
113113
const project = workspace.projects[options.project];
114114

115115
if (options.path === undefined) {
116-
options.path = `/${project.root}/src/app`;
116+
const projectDirName = project.projectType === 'application' ? 'app' : 'lib';
117+
options.path = `/${project.root}/src/${projectDirName}`;
117118
}
118119

119120
options.module = findModuleFromOptions(host, options);

packages/schematics/angular/directive/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ export default function (options: DirectiveOptions): Rule {
110110
const project = workspace.projects[options.project];
111111

112112
if (options.path === undefined) {
113-
options.path = `/${project.root}/src/app`;
113+
const projectDirName = project.projectType === 'application' ? 'app' : 'lib';
114+
options.path = `/${project.root}/src/${projectDirName}`;
114115
}
115116

116117
options.module = findModuleFromOptions(host, options);

packages/schematics/angular/enum/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export default function (options: EnumOptions): Rule {
3232
const project = workspace.projects[options.project];
3333

3434
if (options.path === undefined) {
35-
options.path = `/${project.root}/src/app`;
35+
const projectDirName = project.projectType === 'application' ? 'app' : 'lib';
36+
options.path = `/${project.root}/src/${projectDirName}`;
3637
}
3738

3839
const parsedPath = parseName(options.path, options.name);

packages/schematics/angular/guard/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ export default function (options: GuardOptions): Rule {
7474
const project = workspace.projects[options.project];
7575

7676
if (options.path === undefined) {
77-
options.path = `/${project.root}/src/app`;
77+
const projectDirName = project.projectType === 'application' ? 'app' : 'lib';
78+
options.path = `/${project.root}/src/${projectDirName}`;
7879
}
7980

8081
if (options.module) {

packages/schematics/angular/interface/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export default function (options: InterfaceOptions): Rule {
3232
const project = workspace.projects[options.project];
3333

3434
if (options.path === undefined) {
35-
options.path = `/${project.root}/src/app`;
35+
const projectDirName = project.projectType === 'application' ? 'app' : 'lib';
36+
options.path = `/${project.root}/src/${projectDirName}`;
3637
}
3738

3839
const parsedPath = parseName(options.path, options.name);

packages/schematics/angular/library/index_spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
99
import * as path from 'path';
1010
import { getFileContent } from '../../angular/utility/test';
11+
import { Schema as ComponentOptions } from '../component/schema';
1112
import { latestVersions } from '../utility/latest-versions';
1213
import { Schema as WorkspaceOptions } from '../workspace/schema';
1314
import { Schema as GenerateLibrarySchema } from './schema';
@@ -203,4 +204,14 @@ describe('Library Schematic', () => {
203204
expect(tsConfigJson.compilerOptions.paths).toBeUndefined();
204205
});
205206
});
207+
208+
it('should generate inside of a library', () => {
209+
let tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
210+
const componentOptions: ComponentOptions = {
211+
name: 'comp',
212+
project: 'foo',
213+
};
214+
tree = schematicRunner.runSchematic('component', componentOptions, tree);
215+
expect(tree.exists('/projects/foo/src/lib/comp/comp.component.ts')).toBe(true);
216+
});
206217
});

packages/schematics/angular/module/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ export default function (options: ModuleOptions): Rule {
7979
const project = workspace.projects[options.project];
8080

8181
if (options.path === undefined) {
82-
options.path = `/${project.root}/src/app`;
82+
const projectDirName = project.projectType === 'application' ? 'app' : 'lib';
83+
options.path = `/${project.root}/src/${projectDirName}`;
8384
}
8485
if (options.module) {
8586
options.module = findModuleFromOptions(host, options);

packages/schematics/angular/pipe/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ export default function (options: PipeOptions): Rule {
9494
const project = workspace.projects[options.project];
9595

9696
if (options.path === undefined) {
97-
options.path = `/${project.root}/src/app`;
97+
const projectDirName = project.projectType === 'application' ? 'app' : 'lib';
98+
options.path = `/${project.root}/src/${projectDirName}`;
9899
}
99100

100101
const parsedPath = parseName(options.path, options.name);

packages/schematics/angular/service/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export default function (options: ServiceOptions): Rule {
3131
const project = workspace.projects[options.project];
3232

3333
if (options.path === undefined) {
34-
options.path = `/${project.root}/src/app`;
34+
const projectDirName = project.projectType === 'application' ? 'app' : 'lib';
35+
options.path = `/${project.root}/src/${projectDirName}`;
3536
}
3637

3738
const parsedPath = parseName(options.path, options.name);

0 commit comments

Comments
 (0)