From de15ec5763afe231439c3f1ace35cbacefad2ca7 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 19 Jan 2023 11:15:20 +0000 Subject: [PATCH] fix(@angular/cli): handle extended schematics when retrieving aliases Previously base collections where not being taken into account and the recent changes caused an exception ``` An unhandled exception occurred: Cannot destructure property 'aliases' of 'collection.description.schematics[schematicName]' as it is undefined. ``` See: https://angular-team.slack.com/archives/CHEEH2LCA/p1674122139247359 (cherry picked from commit b5737efae8448084af319fd077863ff6922d651e) --- .../angular/cli/src/commands/generate/cli.ts | 40 ++++++++++++++++--- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/packages/angular/cli/src/commands/generate/cli.ts b/packages/angular/cli/src/commands/generate/cli.ts index eaa0dad05429..2124f2333a25 100644 --- a/packages/angular/cli/src/commands/generate/cli.ts +++ b/packages/angular/cli/src/commands/generate/cli.ts @@ -7,6 +7,11 @@ */ import { strings } from '@angular-devkit/core'; +import { Collection } from '@angular-devkit/schematics'; +import { + FileSystemCollectionDescription, + FileSystemSchematicDescription, +} from '@angular-devkit/schematics/tools'; import { Argv } from 'yargs'; import { CommandModuleError, @@ -69,7 +74,6 @@ export class GenerateCommandModule const { 'x-deprecated': xDeprecated, description = schematicDescription, - aliases = schematicAliases, hidden = schematicHidden, } = schemaJson; const options = await this.getSchematicOptions(collection, schematicName, workflow); @@ -79,8 +83,8 @@ export class GenerateCommandModule // When 'describe' is set to false, it results in a hidden command. describe: hidden === true ? false : typeof description === 'string' ? description : '', deprecated: xDeprecated === true || typeof xDeprecated === 'string' ? xDeprecated : false, - aliases: Array.isArray(aliases) - ? await this.generateCommandAliasesStrings(collectionName, aliases as string[]) + aliases: Array.isArray(schematicAliases) + ? await this.generateCommandAliasesStrings(collectionName, schematicAliases) : undefined, builder: (localYargs) => this.addSchemaOptionsToCommand(localYargs, options).strict(), handler: (options) => @@ -205,13 +209,37 @@ export class GenerateCommandModule // If a schematic with this same name is already registered skip. if (!seenNames.has(schematicName)) { seenNames.add(schematicName); - const { aliases } = collection.description.schematics[schematicName]; - const schematicAliases = aliases && new Set(aliases); - yield { schematicName, schematicAliases, collectionName }; + yield { + schematicName, + collectionName, + schematicAliases: this.listSchematicAliases(collection, schematicName), + }; + } + } + } + } + + private listSchematicAliases( + collection: Collection, + schematicName: string, + ): Set | undefined { + const description = collection.description.schematics[schematicName]; + if (description) { + return description.aliases && new Set(description.aliases); + } + + // Extended collections + if (collection.baseDescriptions) { + for (const base of collection.baseDescriptions) { + const description = base.schematics[schematicName]; + if (description) { + return description.aliases && new Set(description.aliases); } } } + + return undefined; } /**