Skip to content

Commit 36d712e

Browse files
committed
fix(@angular/cli): register schematic aliases when providing collection name in ng generate.
Previously, schematic aliases were not registered when a collection name was provided to `ng generate`. Example: `ng generate c` where `c` is an alias for `component` would work, but `ng generate @schematics/angular:c` would fail. This commits fixes the schematic registration to handle the latter case. Closes #24518
1 parent 89b9d94 commit 36d712e

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

packages/angular/cli/src/commands/generate/cli.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ export class GenerateCommandModule
165165
*/
166166
private async *getSchematics(): AsyncGenerator<{
167167
schematicName: string;
168+
schematicAliases?: Set<string>;
168169
collectionName: string;
169170
}> {
170171
const seenNames = new Set<string>();
@@ -176,7 +177,10 @@ export class GenerateCommandModule
176177
// If a schematic with this same name is already registered skip.
177178
if (!seenNames.has(schematicName)) {
178179
seenNames.add(schematicName);
179-
yield { schematicName, collectionName };
180+
const { aliases } = collection.description.schematics[schematicName];
181+
const schematicAliases = aliases && new Set(aliases);
182+
183+
yield { schematicName, schematicAliases, collectionName };
180184
}
181185
}
182186
}
@@ -196,8 +200,11 @@ export class GenerateCommandModule
196200
this.context.args.positional[1],
197201
);
198202

199-
for await (const { schematicName, collectionName } of this.getSchematics()) {
200-
if (schematicName === schematicNameFromArgs) {
203+
for await (const { schematicName, collectionName, schematicAliases } of this.getSchematics()) {
204+
if (
205+
schematicNameFromArgs &&
206+
(schematicName === schematicNameFromArgs || schematicAliases?.has(schematicNameFromArgs))
207+
) {
201208
return [[schematicName, collectionName]];
202209
}
203210

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ng } from '../../utils/process';
2+
3+
export default async function () {
4+
// Verify that there are no duplicate options
5+
const schematicNameVariation = [
6+
'component',
7+
'c',
8+
'@schematics/angular:component',
9+
'@schematics/angular:c',
10+
];
11+
12+
for (const schematic of schematicNameVariation) {
13+
await ng('generate', schematic, 'comp-name', '--display-block', '--dry-run');
14+
}
15+
}

0 commit comments

Comments
 (0)