diff --git a/packages/schematics/angular/utility/find-module.ts b/packages/schematics/angular/utility/find-module.ts index de57684421cd..f1485c2aefb2 100644 --- a/packages/schematics/angular/utility/find-module.ts +++ b/packages/schematics/angular/utility/find-module.ts @@ -5,7 +5,15 @@ * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ -import { Path, join, normalize, relative, strings } from '@angular-devkit/core'; +import { + NormalizedRoot, + Path, + dirname, + join, + normalize, + relative, + strings, +} from '@angular-devkit/core'; import { DirEntry, Tree } from '@angular-devkit/schematics'; @@ -39,22 +47,40 @@ export function findModuleFromOptions(host: Tree, options: ModuleOptions): Path return normalize(findModule(host, pathToCheck, moduleExt, routingModuleExt)); } else { - const modulePath = normalize( - '/' + (options.path) + '/' + options.module); + const modulePath = normalize(`/${options.path}/${options.module}`); + const componentPath = normalize(`/${options.path}/${options.name}`); const moduleBaseName = normalize(modulePath).split('/').pop(); - const candidates = [ - modulePath, - `${modulePath}.ts`, - `${modulePath}${moduleExt}`, - `${modulePath}/${moduleBaseName}${moduleExt}`, - ]; - for (const c of candidates) { - if (host.exists(c)) { - return normalize(c); + const candidateSet = new Set([ + normalize(options.path || '/'), + ]); + + for (let dir = modulePath; dir != NormalizedRoot; dir = dirname(dir)) { + candidateSet.add(dir); + } + for (let dir = componentPath; dir != NormalizedRoot; dir = dirname(dir)) { + candidateSet.add(dir); + } + + const candidatesDirs = [...candidateSet].sort((a, b) => b.length - a.length); + for (const c of candidatesDirs) { + const candidateFiles = [ + '', + `${moduleBaseName}.ts`, + `${moduleBaseName}${moduleExt}`, + ].map(x => join(c, x)); + + for (const sc of candidateFiles) { + if (host.exists(sc)) { + return normalize(sc); + } } } - throw new Error(`Specified module '${options.module}' does not exist.`); + + throw new Error( + `Specified module '${options.module}' does not exist.\n` + + `Looked in the following directories:\n ${candidatesDirs.join('\n ')}`, + ); } } diff --git a/packages/schematics/angular/utility/find-module_spec.ts b/packages/schematics/angular/utility/find-module_spec.ts index 0d7ad5f1f424..babbdcbdb6a3 100644 --- a/packages/schematics/angular/utility/find-module_spec.ts +++ b/packages/schematics/angular/utility/find-module_spec.ts @@ -120,6 +120,15 @@ describe('find-module', () => { expect(modPath).toEqual('/projects/my-proj/src/admin/foo.module.ts' as Path); }); + it('should find a module in a sub dir (2)', () => { + tree.create('/projects/my-proj/src/admin/foo.module.ts', ''); + options.name = 'admin/hello'; + options.module = 'foo'; + options.path = '/projects/my-proj/src'; + const modPath = findModuleFromOptions(tree, options); + expect(modPath).toEqual('/projects/my-proj/src/admin/foo.module.ts' as Path); + }); + it('should find a module using custom ext', () => { tree.create('/projects/my-proj/src/app_module.ts', ''); options.module = 'app';