Skip to content

Commit a5bb3ce

Browse files
devoto13Keen Yee Liau
authored and
Keen Yee Liau
committed
fix(@schematics/angular): fix app shell schematic failure
Fixes #10093
1 parent 4a5973c commit a5bb3ce

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

packages/schematics/angular/app-shell/index.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
insertImport,
2626
isImported,
2727
} from '../utility/ast-utils';
28-
import { InsertChange } from '../utility/change';
28+
import { Change, InsertChange } from '../utility/change';
2929
import { getWorkspace, getWorkspacePath } from '../utility/config';
3030
import { getAppModulePath } from '../utility/ng-ast-utils';
3131
import { getProjectTargets } from '../utility/project-targets';
@@ -224,8 +224,10 @@ function addRouterModule(options: AppShellOptions): Rule {
224224
const moduleSource = getSourceFile(host, modulePath);
225225
const changes = addImportToModule(moduleSource, modulePath, 'RouterModule', '@angular/router');
226226
const recorder = host.beginUpdate(modulePath);
227-
changes.forEach((change: InsertChange) => {
228-
recorder.insertLeft(change.pos, change.toAdd);
227+
changes.forEach((change: Change) => {
228+
if (change instanceof InsertChange) {
229+
recorder.insertLeft(change.pos, change.toAdd);
230+
}
229231
});
230232
host.commitUpdate(recorder);
231233

packages/schematics/angular/app-shell/index_spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ describe('App Shell Schematic', () => {
7878
expect(content).toMatch(/import { RouterModule } from \'@angular\/router\';/);
7979
});
8080

81+
it('should not fail when AppModule have imported RouterModule already', () => {
82+
const updateRecorder = appTree.beginUpdate('/projects/bar/src/app/app.module.ts');
83+
updateRecorder.insertLeft(0, 'import { RouterModule } from \'@angular/router\';');
84+
appTree.commitUpdate(updateRecorder);
85+
86+
const tree = schematicRunner.runSchematic('appShell', defaultOptions, appTree);
87+
const filePath = '/projects/bar/src/app/app.module.ts';
88+
const content = tree.readContent(filePath);
89+
expect(content).toMatch(/import { RouterModule } from \'@angular\/router\';/);
90+
});
91+
8192
describe('Add router-outlet', () => {
8293
function makeInlineTemplate(tree: UnitTestTree, template?: string): void {
8394
template = template || `

0 commit comments

Comments
 (0)