mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-15 10:11:50 +08:00
parent
4a5973c37a
commit
a5bb3ced10
@ -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);
|
||||
|
||||
|
@ -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 || `
|
||||
|
Loading…
x
Reference in New Issue
Block a user