diff --git a/.changeset/upgrade-command.md b/.changeset/upgrade-command.md new file mode 100644 index 000000000..b2b9c563a --- /dev/null +++ b/.changeset/upgrade-command.md @@ -0,0 +1,5 @@ +--- +"@browserbasehq/browse-cli": patch +--- + +Add `upgrade` command to update browse-cli to the latest version diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index fea595862..396090121 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -14,7 +14,7 @@ import { promises as fs } from "fs"; import * as path from "path"; import * as os from "os"; import * as net from "net"; -import { spawn } from "child_process"; +import { spawn, execFileSync, spawnSync } from "child_process"; import * as readline from "readline"; import type { Protocol } from "devtools-protocol"; import { version as VERSION } from "../package.json"; @@ -2756,6 +2756,51 @@ networkCmd } }); +// ==================== UPGRADE ==================== + +program + .command("upgrade") + .description("Upgrade browse CLI to the latest version") + .action(async () => { + const pkg = "@browserbasehq/browse-cli@latest"; + + // Detect package manager + let pm: "npm" | "pnpm" | "bun" | "yarn" = "npm"; + const userAgent = process.env.npm_config_user_agent ?? ""; + + if (userAgent.startsWith("pnpm")) { + pm = "pnpm"; + } else if (userAgent.startsWith("bun")) { + pm = "bun"; + } else if (userAgent.startsWith("yarn")) { + pm = "yarn"; + } else { + // Fall back to checking which package managers are available + for (const candidate of ["pnpm", "bun", "yarn", "npm"] as const) { + try { + execFileSync("which", [candidate], { stdio: "ignore" }); + pm = candidate; + break; + } catch { + // not found, try next + } + } + } + + const commands: Record = { + npm: ["npm", "install", "-g", pkg], + pnpm: ["pnpm", "add", "-g", pkg], + bun: ["bun", "add", "-g", pkg], + yarn: ["yarn", "global", "add", pkg], + }; + + const [cmd, ...args] = commands[pm]; + console.log(`Running: ${cmd} ${args.join(" ")}`); + + const result = spawnSync(cmd, args, { stdio: "inherit" }); + process.exit(result.status ?? 1); + }); + // ==================== RUN ==================== program.parse();