Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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