refactor(@angular/cli): handle undefined ng add collection name

Fixes an issue where JSON help extraction fails if the `ng add` collection name is undefined.
This commit is contained in:
Alan Agius 2025-02-13 13:49:56 +00:00
parent 57a08c92f2
commit f5689e9525

View File

@ -100,7 +100,11 @@ export default class AddCommandModule
// `ng add @angular/localize -- --package-options`. // `ng add @angular/localize -- --package-options`.
.strict(false); .strict(false);
const collectionName = await this.getCollectionName(); const collectionName = this.getCollectionName();
if (!collectionName) {
return localYargs;
}
const workflow = this.getOrCreateWorkflowForBuilder(collectionName); const workflow = this.getOrCreateWorkflowForBuilder(collectionName);
try { try {
@ -401,14 +405,19 @@ export default class AddCommandModule
return false; return false;
} }
private async getCollectionName(): Promise<string> { private getCollectionName(): string | undefined {
let [, collectionName] = this.context.args.positional; const [, collectionName] = this.context.args.positional;
if (!collectionName) {
return undefined;
}
// The CLI argument may specify also a version, like `ng add @my/lib@13.0.0`, // The CLI argument may specify also a version, like `ng add @my/lib@13.0.0`,
// but here we need only the name of the package, like `@my/lib` // but here we need only the name of the package, like `@my/lib`.
try { try {
const packageIdentifier = npa(collectionName); const packageName = npa(collectionName).name;
collectionName = packageIdentifier.name ?? collectionName; if (packageName) {
return packageName;
}
} catch (e) { } catch (e) {
assertIsError(e); assertIsError(e);
this.context.logger.error(e.message); this.context.logger.error(e.message);