Skip to content

Commit

Permalink
fix(@schematics/angular): fix app shell schematic failure
Browse files Browse the repository at this point in the history
Fixes #10093
  • Loading branch information
devoto13 authored and Keen Yee Liau committed Sep 10, 2018
1 parent 4a5973c commit a5bb3ce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/schematics/angular/app-shell/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -224,8 +224,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);

Expand Down
11 changes: 11 additions & 0 deletions packages/schematics/angular/app-shell/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,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 || `
Expand Down

0 comments on commit a5bb3ce

Please sign in to comment.