From 10023dd984d8f21aedf0ac395101a1a044761b6c Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 24 Aug 2018 17:16:05 +0200 Subject: [PATCH] fix(@schematics/angular): better error message when finding only routing modules Closes #11961 --- .../schematics/angular/utility/find-module.ts | 23 +++++++++++++------ .../angular/utility/find-module_spec.ts | 12 ++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/packages/schematics/angular/utility/find-module.ts b/packages/schematics/angular/utility/find-module.ts index cf55bbd2a88f..6086523b6a12 100644 --- a/packages/schematics/angular/utility/find-module.ts +++ b/packages/schematics/angular/utility/find-module.ts @@ -28,7 +28,7 @@ export function findModuleFromOptions(host: Tree, options: ModuleOptions): Path if (!options.module) { const pathToCheck = (options.path || '') - + (options.flat ? '' : '/' + strings.dasherize(options.name)); + + (options.flat ? '' : '/' + strings.dasherize(options.name)); return normalize(findModule(host, pathToCheck)); } else { @@ -59,12 +59,17 @@ export function findModule(host: Tree, generateDir: string): Path { const moduleRe = /\.module\.ts$/; const routingModuleRe = /-routing\.module\.ts/; + let foundRoutingModule = false; + while (dir) { - const matches = dir.subfiles.filter(p => moduleRe.test(p) && !routingModuleRe.test(p)); + const allMatches = dir.subfiles.filter(p => moduleRe.test(p)); + const filteredMatches = allMatches.filter(p => !routingModuleRe.test(p)); + + foundRoutingModule = foundRoutingModule || allMatches.length !== filteredMatches.length; - if (matches.length == 1) { - return join(dir.path, matches[0]); - } else if (matches.length > 1) { + 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 ' + 'the component into the closest module.'); } @@ -72,8 +77,12 @@ export function findModule(host: Tree, generateDir: string): Path { dir = dir.parent; } - throw new Error('Could not find an NgModule. Use the skip-import ' - + 'option to skip importing in NgModule.'); + const errorMsg = foundRoutingModule ? 'Could not find a non Routing 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); } /** diff --git a/packages/schematics/angular/utility/find-module_spec.ts b/packages/schematics/angular/utility/find-module_spec.ts index 8b92f4ebc54a..4caa1d1b1618 100644 --- a/packages/schematics/angular/utility/find-module_spec.ts +++ b/packages/schematics/angular/utility/find-module_spec.ts @@ -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', () => { try { host = new EmptyTree();