fix(@schematics/angular): better error message when finding only routing modules (#11994)

Closes #11961
This commit is contained in:
Alan Agius 2018-08-30 22:46:52 +02:00 committed by vikerman
parent 41d20873b1
commit b0897c0306
2 changed files with 29 additions and 8 deletions

View File

@ -28,7 +28,7 @@ export function findModuleFromOptions(host: Tree, options: ModuleOptions): Path
if (!options.module) { if (!options.module) {
const pathToCheck = (options.path || '') const pathToCheck = (options.path || '')
+ (options.flat ? '' : '/' + strings.dasherize(options.name)); + (options.flat ? '' : '/' + strings.dasherize(options.name));
return normalize(findModule(host, pathToCheck)); return normalize(findModule(host, pathToCheck));
} else { } else {
@ -59,12 +59,17 @@ export function findModule(host: Tree, generateDir: string): Path {
const moduleRe = /\.module\.ts$/; const moduleRe = /\.module\.ts$/;
const routingModuleRe = /-routing\.module\.ts/; const routingModuleRe = /-routing\.module\.ts/;
while (dir) { let foundRoutingModule = false;
const matches = dir.subfiles.filter(p => moduleRe.test(p) && !routingModuleRe.test(p));
if (matches.length == 1) { while (dir) {
return join(dir.path, matches[0]); const allMatches = dir.subfiles.filter(p => moduleRe.test(p));
} else if (matches.length > 1) { const filteredMatches = allMatches.filter(p => !routingModuleRe.test(p));
foundRoutingModule = foundRoutingModule || allMatches.length !== filteredMatches.length;
if (filteredMatches.length == 1) {
return join(dir.path, filteredMatches[0]);
} else if (filteredMatches.length > 1) {
throw new Error('More than one module matches. Use skip-import option to skip importing ' throw new Error('More than one module matches. Use skip-import option to skip importing '
+ 'the component into the closest module.'); + 'the component into the closest module.');
} }
@ -72,8 +77,12 @@ export function findModule(host: Tree, generateDir: string): Path {
dir = dir.parent; dir = dir.parent;
} }
throw new Error('Could not find an NgModule. Use the skip-import ' const errorMsg = foundRoutingModule ? 'Could not find a non Routing NgModule.'
+ 'option to skip importing in NgModule.'); + '\nModules with suffix \'-routing.module\' are strictly reserved for routing.'
+ '\nUse the skip-import option to skip importing in NgModule.'
: 'Could not find an NgModule. Use the skip-import option to skip importing in NgModule.';
throw new Error(errorMsg);
} }
/** /**

View File

@ -53,6 +53,18 @@ describe('find-module', () => {
} }
}); });
it('should throw if only routing modules were found', () => {
host = new EmptyTree();
host.create('/foo/src/app/anything-routing.module.ts', 'anything routing module');
try {
findModule(host, 'foo/src/app/anything-routing');
throw new Error('Succeeded, should have failed');
} catch (err) {
expect(err.message).toMatch(/Could not find a non Routing NgModule/);
}
});
it('should throw if two modules found', () => { it('should throw if two modules found', () => {
try { try {
host = new EmptyTree(); host = new EmptyTree();