Repository: puppeteer/puppeteer
File: packages/browsers/src/CLI.ts (TypeScript)

Code before:
            yargs.example(
              '$0 list --path /tmp/my-browser-cache',
              'List browsers installed in the specified cache directory',
            );
          }
        },
        async argv => {
          const args = argv as unknown as ListArgs;
          const cacheDir = args.path ?? this.#cachePath;

          try {
            const cache = new Cache(cacheDir);
            const browsers = cache.getInstalledBrowsers();

            if (browsers.length === 0) {
              console.log('No browsers installed.');
              return;
            }

            for (const browser of browsers) {
              console.log(
                `${browser.browser}@${browser.buildId} (${browser.platform}) ${browser.executablePath}`,
              );
            }
          } catch (error) {
            console.error(`Failed to list browsers: ${error}`);
          }
        },
      )
      .demandCommand(1)
      .help();
  }

  #parseBrowser(version: string): Browser {
    return version.split('@').shift() as Browser;
  }

  #parseBuildId(version: string): string {
    const parts = version.split('@');
    return parts.length === 2
      ? parts[1]!
      : this.#pinnedBrowsers
        ? 'pinned'
        : 'latest';
  }

  async #install(args: InstallArgs) {
    args.platform ??= detectBrowserPlatform();
    if (!args.browser) {
      throw new Error(`No browser arg proveded`);
    }

Review comment (bug):
I think we probably do not want to catch an error here so that the whole command exits with an error status code.

Code after:
            yargs.example(
              '$0 list --path /tmp/my-browser-cache',
              'List browsers installed in the specified cache directory',
            );
          }
        },
        async argv => {
          const args = argv as unknown as ListArgs;
          const cacheDir = args.path ?? this.#cachePath;
          const cache = new Cache(cacheDir);
          const browsers = cache.getInstalledBrowsers();

          for (const browser of browsers) {
            console.log(
              `${browser.browser}@${browser.buildId} (${browser.platform}) ${browser.executablePath}`,
            );
          }
        },
      )
      .demandCommand(1)
      .help();
  }

  #parseBrowser(version: string): Browser {
    return version.split('@').shift() as Browser;
  }

  #parseBuildId(version: string): string {
    const parts = version.split('@');
    return parts.length === 2
      ? parts[1]!
      : this.#pinnedBrowsers
        ? 'pinned'
        : 'latest';
  }

  async #install(args: InstallArgs) {
    args.platform ??= detectBrowserPlatform();
    if (!args.browser) {
      throw new Error(`No browser arg proveded`);
    }
    if (!args.platform) {
      throw new Error(`Could not resolve the current platform`);
    }
    if (args.browser.buildId === 'pinned') {
      const options = this.#pinnedBrowsers?.[args.browser.name];
      if (!options || !options.buildId) {
        throw new Error(`No pinned version found for ${args.browser.name}`);
      }
      args.browser.buildId = options.buildId;
    }