Skip to content

Commit

Permalink
fix(@angular/cli): handle extended schematics when retrieving aliases
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alan-agius4 authored and angular-robot[bot] committed Jan 19, 2023
1 parent f7c78dd commit b5737ef
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions packages/angular/cli/src/commands/generate/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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) =>
Expand Down Expand Up @@ -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<FileSystemCollectionDescription, FileSystemSchematicDescription>,
schematicName: string,
): Set<string> | 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;
}

/**
Expand Down

0 comments on commit b5737ef

Please sign in to comment.