Skip to content

Commit

Permalink
fix(@schematics/angular): better error message when finding only rout…
Browse files Browse the repository at this point in the history
…ing modules (#11994)

Closes #11961
  • Loading branch information
alan-agius4 authored and vikerman committed Aug 30, 2018
1 parent d3d13d3 commit 1ed3ab7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/schematics/angular/utility/find-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -59,21 +59,30 @@ 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.');
}

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);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/schematics/angular/utility/find-module_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 1ed3ab7

Please sign in to comment.