Skip to content

Commit 0b7d48c

Browse files
committed
fix(@schematics/angular): correctly detect modules using new file extension format
Previously, some internal methods failed to locate modules when using the `-module.ts` filename pattern instead of the legacy `.module.ts`. This update ensures proper detection with the new format.
1 parent d2fa8ac commit 0b7d48c

File tree

6 files changed

+46
-56
lines changed

6 files changed

+46
-56
lines changed

packages/schematics/angular/component/index.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,7 @@ export default function (options: ComponentOptions): Rule {
5353
options.path = buildDefaultPath(project);
5454
}
5555

56-
try {
57-
options.module = findModuleFromOptions(host, options);
58-
} catch {
59-
options.module = findModuleFromOptions(host, {
60-
...options,
61-
moduleExt: '-module.ts',
62-
routingModuleExt: '-routing-module.ts',
63-
});
64-
}
65-
56+
options.module = findModuleFromOptions(host, options);
6657
// Schematic templates require a defined type value
6758
options.type ??= '';
6859

packages/schematics/angular/directive/index.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,7 @@ export default function (options: DirectiveOptions): Rule {
3838
options.path = buildDefaultPath(project);
3939
}
4040

41-
try {
42-
options.module = findModuleFromOptions(host, options);
43-
} catch {
44-
options.module = findModuleFromOptions(host, {
45-
...options,
46-
moduleExt: '-module.ts',
47-
routingModuleExt: '-routing-module.ts',
48-
});
49-
}
41+
options.module = findModuleFromOptions(host, options);
5042
const parsedPath = parseName(options.path, options.name);
5143
options.name = parsedPath.name;
5244
options.path = parsedPath.path;

packages/schematics/angular/module/index.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import { addImportToModule, addRouteDeclarationToModule } from '../utility/ast-u
2727
import { InsertChange } from '../utility/change';
2828
import {
2929
MODULE_EXT,
30+
MODULE_EXT_LEGACY,
3031
ROUTING_MODULE_EXT,
32+
ROUTING_MODULE_EXT_LEGACY,
3133
buildRelativePath,
3234
findModuleFromOptions,
3335
} from '../utility/find-module';
@@ -114,11 +116,11 @@ function addRouteDeclarationToNgModule(
114116

115117
function getRoutingModulePath(host: Tree, modulePath: string): string | undefined {
116118
const routingModulePath =
117-
modulePath.endsWith(ROUTING_MODULE_EXT) || modulePath.endsWith('-routing-module.ts')
119+
modulePath.endsWith(ROUTING_MODULE_EXT_LEGACY) || modulePath.endsWith(ROUTING_MODULE_EXT)
118120
? modulePath
119121
: modulePath
120-
.replace(MODULE_EXT, ROUTING_MODULE_EXT)
121-
.replace('-module.ts', '-routing-module.ts');
122+
.replace(MODULE_EXT_LEGACY, ROUTING_MODULE_EXT_LEGACY)
123+
.replace(MODULE_EXT, ROUTING_MODULE_EXT);
122124

123125
return host.exists(routingModulePath) ? routingModulePath : undefined;
124126
}
@@ -138,15 +140,7 @@ export default function (options: ModuleOptions): Rule {
138140
}
139141

140142
if (options.module) {
141-
try {
142-
options.module = findModuleFromOptions(host, options);
143-
} catch {
144-
options.module = findModuleFromOptions(host, {
145-
...options,
146-
moduleExt: '-module.ts',
147-
routingModuleExt: '-routing-module.ts',
148-
});
149-
}
143+
options.module = findModuleFromOptions(host, options);
150144
}
151145

152146
let routingModulePath;

packages/schematics/angular/pipe/index.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,7 @@ import { Schema as PipeOptions } from './schema';
1818
export default function (options: PipeOptions): Rule {
1919
return async (host: Tree) => {
2020
options.path ??= await createDefaultPath(host, options.project);
21-
try {
22-
options.module = findModuleFromOptions(host, options);
23-
} catch {
24-
options.module = findModuleFromOptions(host, {
25-
...options,
26-
moduleExt: '-module.ts',
27-
routingModuleExt: '-routing-module.ts',
28-
});
29-
}
30-
21+
options.module = findModuleFromOptions(host, options);
3122
const parsedPath = parseName(options.path, options.name);
3223
options.name = parsedPath.name;
3324
options.path = parsedPath.path;

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ export interface ModuleOptions {
2020
standalone?: boolean;
2121
}
2222

23-
export const MODULE_EXT = '.module.ts';
24-
export const ROUTING_MODULE_EXT = '-routing.module.ts';
23+
export const MODULE_EXT = '-module.ts';
24+
export const ROUTING_MODULE_EXT = '-routing-module.ts';
25+
export const MODULE_EXT_LEGACY = '.module.ts';
26+
export const ROUTING_MODULE_EXT_LEGACY = '-routing.module.ts';
2527

2628
/**
2729
* Find the module referred by a set of options passed to the schematics.
@@ -31,13 +33,10 @@ export function findModuleFromOptions(host: Tree, options: ModuleOptions): Path
3133
return undefined;
3234
}
3335

34-
const moduleExt = options.moduleExt || MODULE_EXT;
35-
const routingModuleExt = options.routingModuleExt || ROUTING_MODULE_EXT;
36-
3736
if (!options.module) {
3837
const pathToCheck = (options.path || '') + '/' + options.name;
3938

40-
return normalize(findModule(host, pathToCheck, moduleExt, routingModuleExt));
39+
return normalize(findModule(host, pathToCheck, options.moduleExt, options.routingModuleExt));
4140
} else {
4241
const modulePath = normalize(`/${options.path}/${options.module}`);
4342
const componentPath = normalize(`/${options.path}/${options.name}`);
@@ -53,14 +52,21 @@ export function findModuleFromOptions(host: Tree, options: ModuleOptions): Path
5352
}
5453

5554
const candidatesDirs = [...candidateSet].sort((a, b) => b.length - a.length);
56-
for (const c of candidatesDirs) {
57-
const candidateFiles = ['', `${moduleBaseName}.ts`, `${moduleBaseName}${moduleExt}`].map(
58-
(x) => join(c, x),
55+
const candidateFiles: string[] = ['', `${moduleBaseName}.ts`];
56+
if (options.moduleExt) {
57+
candidateFiles.push(`${moduleBaseName}${options.moduleExt}`);
58+
} else {
59+
candidateFiles.push(
60+
`${moduleBaseName}${MODULE_EXT}`,
61+
`${moduleBaseName}${MODULE_EXT_LEGACY}`,
5962
);
63+
}
6064

65+
for (const c of candidatesDirs) {
6166
for (const sc of candidateFiles) {
62-
if (host.exists(sc) && host.readText(sc).includes('@NgModule')) {
63-
return normalize(sc);
67+
const scPath = join(c, sc);
68+
if (host.exists(scPath) && host.readText(scPath).includes('@NgModule')) {
69+
return normalize(scPath);
6470
}
6571
}
6672
}
@@ -78,15 +84,22 @@ export function findModuleFromOptions(host: Tree, options: ModuleOptions): Path
7884
export function findModule(
7985
host: Tree,
8086
generateDir: string,
81-
moduleExt = MODULE_EXT,
82-
routingModuleExt = ROUTING_MODULE_EXT,
87+
moduleExt?: string,
88+
routingModuleExt?: string,
8389
): Path {
8490
let dir: DirEntry | null = host.getDir('/' + generateDir);
8591
let foundRoutingModule = false;
8692

93+
const moduleExtensions: string[] = moduleExt ? [moduleExt] : [MODULE_EXT, MODULE_EXT_LEGACY];
94+
const routingModuleExtensions: string[] = routingModuleExt
95+
? [routingModuleExt]
96+
: [ROUTING_MODULE_EXT, ROUTING_MODULE_EXT_LEGACY];
97+
8798
while (dir) {
88-
const allMatches = dir.subfiles.filter((p) => p.endsWith(moduleExt));
89-
const filteredMatches = allMatches.filter((p) => !p.endsWith(routingModuleExt));
99+
const allMatches = dir.subfiles.filter((p) => moduleExtensions.some((m) => p.endsWith(m)));
100+
const filteredMatches = allMatches.filter(
101+
(p) => !routingModuleExtensions.some((m) => p.endsWith(m)),
102+
);
90103

91104
foundRoutingModule = foundRoutingModule || allMatches.length !== filteredMatches.length;
92105

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ describe('find-module', () => {
143143
expect(modPath).toEqual('/projects/my-proj/src/admin/foo.module.ts');
144144
});
145145

146+
it('should find a module in a sub dir when using the `-module` suffix', () => {
147+
tree.create('/projects/my-proj/src/admin/foo-module.ts', '@NgModule');
148+
options.name = 'other/test';
149+
options.module = 'admin/foo';
150+
options.path = '/projects/my-proj/src';
151+
const modPath = findModuleFromOptions(tree, options) as string;
152+
expect(modPath).toEqual('/projects/my-proj/src/admin/foo-module.ts');
153+
});
154+
146155
it('should find a module in a sub dir (2)', () => {
147156
tree.create('/projects/my-proj/src/admin/foo.module.ts', '@NgModule');
148157
options.name = 'admin/hello';

0 commit comments

Comments
 (0)