Skip to content

Commit ffda6f0

Browse files
committed
fix(@schematics/angular): fix app shel schematic failure
Fixes #10093
1 parent 4106a89 commit ffda6f0

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
@@ -29,7 +29,7 @@ import {
2929
insertImport,
3030
isImported,
3131
} from '../utility/ast-utils';
32-
import { InsertChange } from '../utility/change';
32+
import { Change, InsertChange } from '../utility/change';
3333
import { getWorkspace, getWorkspacePath } from '../utility/config';
3434
import { getAppModulePath } from '../utility/ng-ast-utils';
3535
import { Schema as AppShellOptions } from './schema';
@@ -221,8 +221,10 @@ function addRouterModule(options: AppShellOptions): Rule {
221221
const moduleSource = getSourceFile(host, modulePath);
222222
const changes = addImportToModule(moduleSource, modulePath, 'RouterModule', '@angular/router');
223223
const recorder = host.beginUpdate(modulePath);
224-
changes.forEach((change: InsertChange) => {
225-
recorder.insertLeft(change.pos, change.toAdd);
224+
changes.forEach((change: Change) => {
225+
if (change instanceof InsertChange) {
226+
recorder.insertLeft(change.pos, change.toAdd);
227+
}
226228
});
227229
host.commitUpdate(recorder);
228230

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

Lines changed: 11 additions & 0 deletions
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)