CLI Overview
Relevant source files
The following files were used as context for generating this wiki page:The react-grab Command Line Interface (CLI), distributed as the grab package, is a tool designed to automate the installation and configuration of React Grab within a project. Its primary purpose is to detect the project's framework and structure, then apply the necessary code modifications to integrate the core react-grab library and any optional agent providers. This simplifies the setup process, making it the recommended method for getting started.
The main command provided by the CLI is init, which handles project analysis, dependency management, and code transformation. It supports various frameworks like Next.js and Vite, and can configure integrations with multiple coding agents such as Claude Code and Cursor.
Sources: packages/cli/src/commands/init.ts, README.md, packages/website/app/blog/agent/page.tsx:223-230
Installation and Usage
The CLI is intended to be run directly using npx, which ensures the latest version is always used. The primary command to initialize React Grab in a project is:
npx grab@latest init
This command should be executed from the root directory of the target project. The CLI will then guide the user through the installation process.
Sources: README.md, packages/website/app/blog/agent/page.tsx:227-230, packages/website/components/install-tabs.tsx:64-69
The init Command
The init command is the sole command exposed by the CLI and serves as the main entry point for all its functionality. It orchestrates project detection, user interaction, package installation, and file modification.
Sources: packages/cli/src/cli.ts:8-10, packages/cli/src/commands/init.ts:133
Command Options
The init command accepts several options to customize its behavior, allowing for both interactive and non-interactive execution.
| Option | Alias | Description | Default Value |
|---|---|---|---|
--yes | -y | Skips all confirmation prompts for non-interactive use. | false |
--force | -f | Forces an overwrite of an existing React Grab configuration. | false |
--agent <agent> | -a | Specifies an agent integration to install (e.g., cursor, claude-code). | undefined |
--skip-install | Skips the package installation step. | false | |
--cwd <cwd> | -c | Sets the working directory for the command. | process.cwd() |
Sources: packages/cli/src/commands/init.ts:134-146
Execution Flow
The init command follows a structured process to safely and correctly configure a project. The process adapts based on whether React Grab is already installed.
This diagram illustrates the high-level logic of the init command.
Core Features
Project Detection
Before making any changes, the CLI runs a detectProject function to analyze the current project. This detection phase identifies:
- Framework: Determines if the project uses a supported framework (
next,vite,webpack) or an unsupported one (remix,astro, etc.). - Package Manager: Detects which package manager is in use (
npm,pnpm,yarn,bun). - Existing Installation: Checks if
react-grabor any agent integrations are already present. - Monorepo: Identifies if the command is run in a monorepo, and if so, can prompt the user to select a specific workspace project.
| Detection Target | Supported Values |
|---|---|
| Framework | next, vite, webpack, unknown |
| Unsupported Frameworks | remix, astro, sveltekit, gatsby |
| Package Manager | npm, yarn, pnpm, bun |
| Next.js Router | app, pages |
Sources: packages/cli/src/commands/init.ts:68-79, packages/cli/src/commands/init.ts:157, packages/cli/src/commands/init.ts:577-622
Code Transformation
A key function of the CLI is to automatically modify project files. It uses a set of transformation functions (previewTransform, applyTransform) to inject the necessary code.
- Layout/HTML Files: For Next.js, it injects a
<Script>tag into the root layout (app/layout.tsxorpages/_document.tsx). For Vite, it modifiesindex.html. package.json: When an agent integration is added, the CLI modifies thescriptssection to chain the agent's server with the existingdevcommand.
The following sequence diagram shows how the CLI transforms files when adding an agent.
Agent Integration
The CLI streamlines the process of integrating react-grab with various coding agents. This feature allows the tool to send context directly from the browser to the agent, enabling a "visual edit" workflow.
The following agents are supported:
| Agent Name | CLI Value | Package |
|---|---|---|
| Claude Code | claude-code | @react-grab/claude-code |
| Cursor | cursor | @react-grab/cursor |
| OpenCode | opencode | @react-grab/opencode |
| Codex | codex | @react-grab/codex |
| Gemini | gemini | @react-grab/gemini |
| Amp | amp | @react-grab/amp |
| Visual Edit | visual-edit | N/A |
When a user selects an agent, the CLI automatically:
- Installs the corresponding npm package (e.g.,
@react-grab/cursor). - Modifies
package.jsonto run the agent's server alongside the application's dev server. - Injects the agent's client-side script into the application's main HTML document or layout file.
Sources: packages/cli/src/commands/init.ts:81-88, packages/cli/src/commands/init.ts:188-196, packages/website/app/blog/agent/page.tsx:232-587
Analytics and Reporting
To help improve the tool, the CLI includes a reporting feature that sends anonymous data upon completion or error. This is handled by the reportToCli function.
- Endpoint:
https://react-grab.com/api/report-cli - Data Sent:
type: "completed" or "error"version: The CLI version.config: An object containing the detected framework, package manager, router, and selected agent.error: The error message and stack trace if an error occurred.timestamp: ISO string of the event time.
This data is used solely for maintenance and to track the tool's adoption and stability across different environments.