Skip to content

Commit b39189c

Browse files
committed
fix(@schematics/angular): fix app shell schematic failure
Fixes #10093
1 parent 02bfde5 commit b39189c

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
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';
@@ -219,8 +219,10 @@ function addRouterModule(options: AppShellOptions): Rule {
219219
const moduleSource = getSourceFile(host, modulePath);
220220
const changes = addImportToModule(moduleSource, modulePath, 'RouterModule', '@angular/router');
221221
const recorder = host.beginUpdate(modulePath);
222-
changes.forEach((change: InsertChange) => {
223-
recorder.insertLeft(change.pos, change.toAdd);
222+
changes.forEach((change: Change) => {
223+
if (change instanceof InsertChange) {
224+
recorder.insertLeft(change.pos, change.toAdd);
225+
}
224226
});
225227
host.commitUpdate(recorder);
226228

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ describe('App Shell Schematic', () => {
7777
expect(content).toMatch(/import { RouterModule } from \'@angular\/router\';/);
7878
});
7979

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

0 commit comments

Comments
 (0)