Skip to content

Commit 5484e03

Browse files
committed
fix(@schematics/angular): fix application sourceDir option
There is a hard-coded sourceDir expectation in the code, this removes it in favor of using the appropriate value from options. Fixes #7656
1 parent d647249 commit 5484e03

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

packages/schematics/angular/application/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import { InsertChange } from '../utility/change';
2828
import { Schema as ApplicationOptions } from './schema';
2929

3030

31-
function addBootstrapToNgModule(directory: string): Rule {
31+
function addBootstrapToNgModule(directory: string, sourceDir: string): Rule {
3232
return (host: Tree) => {
33-
const modulePath = `${directory}/src/app/app.module.ts`;
33+
const modulePath = `${directory}/${sourceDir}/app/app.module.ts`;
3434
const content = host.read(modulePath);
3535
if (!content) {
3636
throw new SchematicsError(`File ${modulePath} does not exist.`);
@@ -88,6 +88,7 @@ export default function (options: ApplicationOptions): Rule {
8888
spec: false,
8989
styleext: options.style,
9090
};
91+
const sourceDir = options.sourceDir || 'src';
9192

9293
return chain([
9394
mergeWith(
@@ -106,17 +107,17 @@ export default function (options: ApplicationOptions): Rule {
106107
flat: true,
107108
routing: options.routing,
108109
routingScope: 'Root',
109-
sourceDir: options.directory + '/' + options.sourceDir,
110+
sourceDir: options.directory + '/' + sourceDir,
110111
spec: false,
111112
}),
112113
schematic('component', {
113114
name: 'app',
114115
selector: appRootSelector,
115-
sourceDir: options.directory + '/' + options.sourceDir,
116+
sourceDir: options.directory + '/' + sourceDir,
116117
flat: true,
117118
...componentOptions,
118119
}),
119-
addBootstrapToNgModule(options.directory),
120+
addBootstrapToNgModule(options.directory, sourceDir),
120121
mergeWith(
121122
apply(url('./other-files'), [
122123
componentOptions.inlineTemplate ? filter(path => !path.endsWith('.html')) : noop(),
@@ -127,7 +128,7 @@ export default function (options: ApplicationOptions): Rule {
127128
selector: appRootSelector,
128129
...componentOptions,
129130
}),
130-
move(options.directory + '/' + options.sourceDir + '/app'),
131+
move(options.directory + '/' + sourceDir + '/app'),
131132
]), MergeStrategy.Overwrite),
132133
])(host, context);
133134
};

packages/schematics/angular/application/index_spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
import { Tree } from '@angular-devkit/schematics';
89
import { SchematicTestRunner } from '@angular-devkit/schematics/test';
910
import { getFileContent } from '../utility/test';
1011
import { Schema as AppSchema } from './schema';
@@ -64,6 +65,20 @@ describe('Application Schematic', () => {
6465
expect(files.indexOf('/foo/src/app/app.component.ts')).toBeGreaterThanOrEqual(0);
6566
});
6667

68+
it('should handle a different sourceDir', () => {
69+
const options = { ...defaultOptions, sourceDir: 'some/custom/path' };
70+
71+
let tree: Tree | null = null;
72+
expect(() => tree = schematicRunner.runSchematic('application', options))
73+
.not.toThrow();
74+
75+
if (tree) {
76+
// tslint:disable-next-line:non-null-operator
77+
const files = tree !.files;
78+
expect(files.indexOf('/foo/some/custom/path/app/app.module.ts')).toBeGreaterThanOrEqual(0);
79+
}
80+
});
81+
6782
it('should handle the routing flag', () => {
6883
const options = { ...defaultOptions, routing: true };
6984

0 commit comments

Comments
 (0)