fix(@schematics/angular): fix app shell schematic failure

Fixes #10093
This commit is contained in:
Yaroslav Admin 2018-07-19 20:23:25 +02:00 committed by Keen Yee Liau
parent 4a5973c37a
commit a5bb3ced10
2 changed files with 16 additions and 3 deletions

View File

@ -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';
@ -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);

View File

@ -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 || `