Skip to content
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
52 changes: 39 additions & 13 deletions packages/schematics/angular/utility/find-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand Down Expand Up @@ -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<Path>([
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 ')}`,
);
}
}

Expand Down
9 changes: 9 additions & 0 deletions packages/schematics/angular/utility/find-module_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down