@@ -79,7 +79,9 @@ export class GenerateCommandModule
79
79
// When 'describe' is set to false, it results in a hidden command.
80
80
describe : hidden === true ? false : typeof description === 'string' ? description : '' ,
81
81
deprecated : xDeprecated === true || typeof xDeprecated === 'string' ? xDeprecated : false ,
82
- aliases : Array . isArray ( aliases ) ? ( aliases as string [ ] ) : undefined ,
82
+ aliases : Array . isArray ( aliases )
83
+ ? await this . generateCommandAliasesStrings ( collectionName , aliases as string [ ] )
84
+ : undefined ,
83
85
builder : ( localYargs ) => this . addSchemaOptionsToCommand ( localYargs , options ) . strict ( ) ,
84
86
handler : ( options ) =>
85
87
this . handler ( { ...options , schematic : `${ collectionName } :${ schematicName } ` } ) ,
@@ -120,6 +122,41 @@ export class GenerateCommandModule
120
122
return collectionName ? [ collectionName ] : [ ...( await this . getSchematicCollections ( ) ) ] ;
121
123
}
122
124
125
+ private async shouldAddCollectionNameAsPartOfCommand ( ) : Promise < boolean > {
126
+ const [ collectionNameFromArgs ] = this . parseSchematicInfo (
127
+ // positional = [generate, component] or [generate]
128
+ this . context . args . positional [ 1 ] ,
129
+ ) ;
130
+
131
+ const schematicCollectionsFromConfig = await this . getSchematicCollections ( ) ;
132
+ const collectionNames = await this . getCollectionNames ( ) ;
133
+
134
+ // Only add the collection name as part of the command when it's not a known
135
+ // schematics collection or when it has been provided via the CLI.
136
+ // Ex:`ng generate @schematics/angular:c`
137
+ return (
138
+ ! ! collectionNameFromArgs ||
139
+ ! collectionNames . some ( ( c ) => schematicCollectionsFromConfig . has ( c ) )
140
+ ) ;
141
+ }
142
+
143
+ /**
144
+ * Generate an aliases string array to be passed to the command builder.
145
+ *
146
+ * @example `[component]` or `[@schematics/angular:component]`.
147
+ */
148
+ private async generateCommandAliasesStrings (
149
+ collectionName : string ,
150
+ schematicAliases : string [ ] ,
151
+ ) : Promise < string [ ] > {
152
+ // Only add the collection name as part of the command when it's not a known
153
+ // schematics collection or when it has been provided via the CLI.
154
+ // Ex:`ng generate @schematics/angular:c`
155
+ return ( await this . shouldAddCollectionNameAsPartOfCommand ( ) )
156
+ ? schematicAliases . map ( ( alias ) => `${ collectionName } :${ alias } ` )
157
+ : schematicAliases ;
158
+ }
159
+
123
160
/**
124
161
* Generate a command string to be passed to the command builder.
125
162
*
@@ -130,23 +167,14 @@ export class GenerateCommandModule
130
167
schematicName : string ,
131
168
options : Option [ ] ,
132
169
) : Promise < string > {
133
- const [ collectionNameFromArgs ] = this . parseSchematicInfo (
134
- // positional = [generate, component] or [generate]
135
- this . context . args . positional [ 1 ] ,
136
- ) ;
137
-
138
170
const dasherizedSchematicName = strings . dasherize ( schematicName ) ;
139
- const schematicCollectionsFromConfig = await this . getSchematicCollections ( ) ;
140
- const collectionNames = await this . getCollectionNames ( ) ;
141
171
142
172
// Only add the collection name as part of the command when it's not a known
143
173
// schematics collection or when it has been provided via the CLI.
144
174
// Ex:`ng generate @schematics/angular:component`
145
- const commandName =
146
- ! ! collectionNameFromArgs ||
147
- ! collectionNames . some ( ( c ) => schematicCollectionsFromConfig . has ( c ) )
148
- ? collectionName + ':' + dasherizedSchematicName
149
- : dasherizedSchematicName ;
175
+ const commandName = ( await this . shouldAddCollectionNameAsPartOfCommand ( ) )
176
+ ? collectionName + ':' + dasherizedSchematicName
177
+ : dasherizedSchematicName ;
150
178
151
179
const positionalArgs = options
152
180
. filter ( ( o ) => o . positional !== undefined )
@@ -165,6 +193,7 @@ export class GenerateCommandModule
165
193
*/
166
194
private async * getSchematics ( ) : AsyncGenerator < {
167
195
schematicName : string ;
196
+ schematicAliases ?: Set < string > ;
168
197
collectionName : string ;
169
198
} > {
170
199
const seenNames = new Set < string > ( ) ;
@@ -176,7 +205,10 @@ export class GenerateCommandModule
176
205
// If a schematic with this same name is already registered skip.
177
206
if ( ! seenNames . has ( schematicName ) ) {
178
207
seenNames . add ( schematicName ) ;
179
- yield { schematicName, collectionName } ;
208
+ const { aliases } = collection . description . schematics [ schematicName ] ;
209
+ const schematicAliases = aliases && new Set ( aliases ) ;
210
+
211
+ yield { schematicName, schematicAliases, collectionName } ;
180
212
}
181
213
}
182
214
}
@@ -196,8 +228,11 @@ export class GenerateCommandModule
196
228
this . context . args . positional [ 1 ] ,
197
229
) ;
198
230
199
- for await ( const { schematicName, collectionName } of this . getSchematics ( ) ) {
200
- if ( schematicName === schematicNameFromArgs ) {
231
+ for await ( const { schematicName, collectionName, schematicAliases } of this . getSchematics ( ) ) {
232
+ if (
233
+ schematicNameFromArgs &&
234
+ ( schematicName === schematicNameFromArgs || schematicAliases ?. has ( schematicNameFromArgs ) )
235
+ ) {
201
236
return [ [ schematicName , collectionName ] ] ;
202
237
}
203
238
0 commit comments