|
5 | 5 | * Use of this source code is governed by an MIT-style license that can be
|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 |
| -import { Path, join, normalize, relative, strings } from '@angular-devkit/core'; |
| 8 | +import { |
| 9 | + NormalizedRoot, |
| 10 | + Path, |
| 11 | + dirname, |
| 12 | + join, |
| 13 | + normalize, |
| 14 | + relative, |
| 15 | + strings, |
| 16 | +} from '@angular-devkit/core'; |
9 | 17 | import { DirEntry, Tree } from '@angular-devkit/schematics';
|
10 | 18 |
|
11 | 19 |
|
@@ -39,22 +47,40 @@ export function findModuleFromOptions(host: Tree, options: ModuleOptions): Path
|
39 | 47 |
|
40 | 48 | return normalize(findModule(host, pathToCheck, moduleExt, routingModuleExt));
|
41 | 49 | } else {
|
42 |
| - const modulePath = normalize( |
43 |
| - '/' + (options.path) + '/' + options.module); |
| 50 | + const modulePath = normalize(`/${options.path}/${options.module}`); |
| 51 | + const componentPath = normalize(`/${options.path}/${options.name}`); |
44 | 52 | const moduleBaseName = normalize(modulePath).split('/').pop();
|
45 | 53 |
|
46 |
| - const candidates = [ |
47 |
| - modulePath, |
48 |
| - `${modulePath}.ts`, |
49 |
| - `${modulePath}${moduleExt}`, |
50 |
| - `${modulePath}/${moduleBaseName}${moduleExt}`, |
51 |
| - ]; |
52 |
| - for (const c of candidates) { |
53 |
| - if (host.exists(c)) { |
54 |
| - return normalize(c); |
| 54 | + const candidateSet = new Set<Path>([ |
| 55 | + normalize(options.path || '/'), |
| 56 | + ]); |
| 57 | + |
| 58 | + for (let dir = modulePath; dir != NormalizedRoot; dir = dirname(dir)) { |
| 59 | + candidateSet.add(dir); |
| 60 | + } |
| 61 | + for (let dir = componentPath; dir != NormalizedRoot; dir = dirname(dir)) { |
| 62 | + candidateSet.add(dir); |
| 63 | + } |
| 64 | + |
| 65 | + const candidatesDirs = [...candidateSet].sort((a, b) => b.length - a.length); |
| 66 | + for (const c of candidatesDirs) { |
| 67 | + const candidateFiles = [ |
| 68 | + '', |
| 69 | + `${moduleBaseName}.ts`, |
| 70 | + `${moduleBaseName}${moduleExt}`, |
| 71 | + ].map(x => join(c, x)); |
| 72 | + |
| 73 | + for (const sc of candidateFiles) { |
| 74 | + if (host.exists(sc)) { |
| 75 | + return normalize(sc); |
| 76 | + } |
55 | 77 | }
|
56 | 78 | }
|
57 |
| - throw new Error(`Specified module '${options.module}' does not exist.`); |
| 79 | + |
| 80 | + throw new Error( |
| 81 | + `Specified module '${options.module}' does not exist.\n` |
| 82 | + + `Looked in the following directories:\n ${candidatesDirs.join('\n ')}`, |
| 83 | + ); |
58 | 84 | }
|
59 | 85 | }
|
60 | 86 |
|
|
0 commit comments