diff --git a/packages/schematics/angular/app-shell/index.ts b/packages/schematics/angular/app-shell/index.ts index 2cdf54a46cd0..256359bf96ed 100644 --- a/packages/schematics/angular/app-shell/index.ts +++ b/packages/schematics/angular/app-shell/index.ts @@ -25,7 +25,7 @@ import { insertImport, isImported, } from '../utility/ast-utils'; -import { InsertChange } from '../utility/change'; +import { Change, InsertChange } from '../utility/change'; import { getWorkspace, getWorkspacePath } from '../utility/config'; import { getAppModulePath } from '../utility/ng-ast-utils'; import { getProjectTargets } from '../utility/project-targets'; @@ -219,8 +219,10 @@ function addRouterModule(options: AppShellOptions): Rule { const moduleSource = getSourceFile(host, modulePath); const changes = addImportToModule(moduleSource, modulePath, 'RouterModule', '@angular/router'); const recorder = host.beginUpdate(modulePath); - changes.forEach((change: InsertChange) => { - recorder.insertLeft(change.pos, change.toAdd); + changes.forEach((change: Change) => { + if (change instanceof InsertChange) { + recorder.insertLeft(change.pos, change.toAdd); + } }); host.commitUpdate(recorder); diff --git a/packages/schematics/angular/app-shell/index_spec.ts b/packages/schematics/angular/app-shell/index_spec.ts index d7ceb96785be..bf6508680685 100644 --- a/packages/schematics/angular/app-shell/index_spec.ts +++ b/packages/schematics/angular/app-shell/index_spec.ts @@ -77,6 +77,17 @@ describe('App Shell Schematic', () => { expect(content).toMatch(/import { RouterModule } from \'@angular\/router\';/); }); + it('should not fail when AppModule have imported RouterModule already', () => { + const updateRecorder = appTree.beginUpdate('/projects/bar/src/app/app.module.ts'); + updateRecorder.insertLeft(0, 'import { RouterModule } from \'@angular/router\';'); + appTree.commitUpdate(updateRecorder); + + const tree = schematicRunner.runSchematic('appShell', defaultOptions, appTree); + const filePath = '/projects/bar/src/app/app.module.ts'; + const content = tree.readContent(filePath); + expect(content).toMatch(/import { RouterModule } from \'@angular\/router\';/); + }); + describe('Add router-outlet', () => { function makeInlineTemplate(tree: UnitTestTree, template?: string): void { template = template || `