mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-16 02:24:10 +08:00
fix(@schematics/angular): better error message when finding only routing modules (#11994)
Closes #11961
This commit is contained in:
parent
41d20873b1
commit
b0897c0306
@ -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/;
|
||||
|
||||
while (dir) {
|
||||
const matches = dir.subfiles.filter(p => moduleRe.test(p) && !routingModuleRe.test(p));
|
||||
let foundRoutingModule = false;
|
||||
|
||||
if (matches.length == 1) {
|
||||
return join(dir.path, matches[0]);
|
||||
} else if (matches.length > 1) {
|
||||
while (dir) {
|
||||
const allMatches = dir.subfiles.filter(p => moduleRe.test(p));
|
||||
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 '
|
||||
+ '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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user