From f5689e9525c081a16e94b61d48663fbd85b578bb Mon Sep 17 00:00:00 2001
From: Alan Agius <alan.agius4@gmail.com>
Date: Thu, 13 Feb 2025 13:49:56 +0000
Subject: [PATCH] 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.
---
 packages/angular/cli/src/commands/add/cli.ts | 21 ++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/packages/angular/cli/src/commands/add/cli.ts b/packages/angular/cli/src/commands/add/cli.ts
index c280ba1af0..5470562a97 100644
--- a/packages/angular/cli/src/commands/add/cli.ts
+++ b/packages/angular/cli/src/commands/add/cli.ts
@@ -100,7 +100,11 @@ export default class AddCommandModule
       // `ng add @angular/localize -- --package-options`.
       .strict(false);
 
-    const collectionName = await this.getCollectionName();
+    const collectionName = this.getCollectionName();
+    if (!collectionName) {
+      return localYargs;
+    }
+
     const workflow = this.getOrCreateWorkflowForBuilder(collectionName);
 
     try {
@@ -401,14 +405,19 @@ export default class AddCommandModule
     return false;
   }
 
-  private async getCollectionName(): Promise<string> {
-    let [, collectionName] = this.context.args.positional;
+  private getCollectionName(): string | undefined {
+    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`,
-    // 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 {
-      const packageIdentifier = npa(collectionName);
-      collectionName = packageIdentifier.name ?? collectionName;
+      const packageName = npa(collectionName).name;
+      if (packageName) {
+        return packageName;
+      }
     } catch (e) {
       assertIsError(e);
       this.context.logger.error(e.message);