Skip to content

Commit fabcac1

Browse files
hanslvikerman
authored andcommitted
feat(@schematics/angular): look for module in more places (angular#12389)
Recursively go through parents of the component and parents of the module name. Fixes angular#7662. Also shows tried directories on failure so the user has more informations.
1 parent 4d46c4a commit fabcac1

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

packages/schematics/angular/utility/find-module.ts

+39-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
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';
917
import { DirEntry, Tree } from '@angular-devkit/schematics';
1018

1119

@@ -39,22 +47,40 @@ export function findModuleFromOptions(host: Tree, options: ModuleOptions): Path
3947

4048
return normalize(findModule(host, pathToCheck, moduleExt, routingModuleExt));
4149
} 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}`);
4452
const moduleBaseName = normalize(modulePath).split('/').pop();
4553

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+
}
5577
}
5678
}
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+
);
5884
}
5985
}
6086

packages/schematics/angular/utility/find-module_spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ describe('find-module', () => {
120120
expect(modPath).toEqual('/projects/my-proj/src/admin/foo.module.ts' as Path);
121121
});
122122

123+
it('should find a module in a sub dir (2)', () => {
124+
tree.create('/projects/my-proj/src/admin/foo.module.ts', '');
125+
options.name = 'admin/hello';
126+
options.module = 'foo';
127+
options.path = '/projects/my-proj/src';
128+
const modPath = findModuleFromOptions(tree, options);
129+
expect(modPath).toEqual('/projects/my-proj/src/admin/foo.module.ts' as Path);
130+
});
131+
123132
it('should find a module using custom ext', () => {
124133
tree.create('/projects/my-proj/src/app_module.ts', '');
125134
options.module = 'app';

0 commit comments

Comments
 (0)