Installation via CLI
Relevant source files
The following files were used as context for generating this wiki page:The react-grab project provides a Command Line Interface (CLI) tool to automate its integration into various React projects. The primary goal of the CLI is to detect the project's framework and package manager, then automatically inject the necessary scripts and configurations. This simplifies the setup process, including the integration of various coding agents.
The main entry point for installation is the init command, which can be run directly using npx. The CLI also provides an add command to manage agent integrations after the initial setup. The tool is designed to be interactive, guiding the user through a series of prompts, but also supports a non-interactive mode for automated environments.
Sources: packages/cli/src/commands/init.ts, README.md:16-19, packages/cli/src/commands/add.ts
Core Commands
The CLI is organized around two main commands, init and add.
| Command | Description |
|---|---|
init | Initializes React Grab in a project. It performs framework detection, file transformation, package installation, and optional agent/option configuration. |
add | Adds a new agent integration to an existing React Grab installation. It can replace or add alongside currently installed agents. |
Sources: packages/cli/src/commands/init.ts:138, packages/cli/src/commands/add.ts:36
init Command Options
| Option | Alias | Description | Default |
|---|---|---|---|
--yes | -y | Skips all confirmation prompts for non-interactive use. | false |
--force | -f | Forces reconfiguration if React Grab is already installed. | false |
--agent <agent> | -a | Specifies an agent to integrate during initialization. | none |
--skip-install | Skips the automatic installation of npm packages. | false | |
--cwd <cwd> | -c | Sets the working directory for the command. | process.cwd() |
Sources: packages/cli/src/commands/init.ts:140-144
The init Command Workflow
The init command follows a multi-step process to configure a project. It begins with preflight checks and project analysis, proceeds to file transformation, and finishes with package installation and reporting.
The following diagram illustrates the high-level workflow of the init command.
1. Project Detection
The process starts by calling detectProject to analyze the current directory. This function determines:
- The project's root directory.
- The JavaScript framework (
next,vite,webpack, orunknown). - For Next.js, the router type (
apporpages). - The package manager (
npm,pnpm,yarn,bun). - Whether
react-grabis already installed (hasReactGrab). - A list of any currently installed agent integrations.
If an unsupported framework like Remix or Astro is detected, the CLI exits with an informative message. In a monorepo, if the root is not a specific project type, it searches for workspace projects and prompts the user to select one.
Sources: packages/cli/src/commands/init.ts:147, packages/cli/src/commands/init.ts:511-574
2. File Transformation
The core of the CLI is its ability to automatically modify project files. It uses previewTransform to generate the new file content and applyTransform to write it to disk.
Transformation Logic
- Next.js (App Router): Injects a
<Script>tag forreact-grabinto the<head>ofapp/layout.tsx. - Next.js (Pages Router): Injects a
<Script>tag into the<Head>component ofpages/_document.tsx. - Vite: Adds a
<script type="module">block toindex.htmlthat dynamically importsreact-grabin development mode. - Webpack: Adds an
if (process.env.NODE_ENV === 'development')block to the main entry file (e.g.,src/index.tsx) to importreact-grab. package.json: Modifies thedevscript to prefix it with the appropriate agent runner command (e.g.,npx @react-grab/cursor@latest && ...).
The following sequence diagram shows the interaction for transforming a file.
3. Package Management
If not skipped via the --skip-install flag, the CLI manages npm dependencies.
- The
getPackagesToInstallutility determines which packages are needed (react-graband/or an agent package). - The
installPackagesfunction executes the installation using the detected package manager. - Similarly,
getPackagesToUninstallanduninstallPackagesare used when replacing an agent.
Sources: packages/cli/src/commands/init.ts:771-789
Agent Integration Management
A key feature of the CLI is its handling of agent integrations, which allow react-grab to send context directly to a coding agent.
Supported Agents
The CLI supports a predefined list of agents. When an agent is selected, the CLI installs its corresponding package and configures the package.json dev script to launch the agent's server.
| Agent Name | Package | dev Script Prefix |
|---|---|---|
| Claude Code | @react-grab/claude-code | npx @react-grab/claude-code@latest && |
| Cursor | @react-grab/cursor | npx @react-grab/cursor@latest && |
| OpenCode | @react-grab/opencode | npx @react-grab/opencode@latest && |
| Codex | @react-grab/codex | npx @react-grab/codex@latest && |
| Gemini | @react-grab/gemini | npx @react-grab/gemini@latest && |
| Amp | @react-grab/amp | npx @react-grab/amp@latest && |
| Visual Edit | @react-grab/visual-edit | npx @react-grab/visual-edit@latest && |
Sources: packages/cli/src/commands/init.ts:91-100, packages/cli/src/commands/add.ts:25-33, README.md:65-259, packages/website/app/blog/agent/page.tsx:210-580
Adding and Replacing Agents
- The
addcommand is used exclusively to add an agent to an already-installedreact-grabinstance. - The
initcommand, when run on a project wherereact-grabis already present, allows the user to add a new agent, replace existing ones, or proceed to option customization. - In both scenarios, the user is prompted whether to
replacean existing agent oradda new one alongside it.
Sources: packages/cli/src/commands/add.ts:114-162, packages/cli/src/commands/init.ts:205-243
Post-Install Customization
If react-grab is already installed, the init command offers to customize its runtime options. These options are injected into the script tag or import statement.
| Option | Description | Type | Prompt |
|---|---|---|---|
activationKey | The keyboard shortcut to activate React Grab. | object | "Enter the activation key (e.g., g, k, space)" |
activationMode | toggle (press to activate/deactivate) or hold (hold to keep active). | string | "Select activation mode" |
keyHoldDuration | The time in milliseconds to hold a key in hold mode. | number | "Enter key hold duration in milliseconds" |
allowActivationInsideInput | Whether the tool can be activated while an input field is focused. | boolean | "Allow activation inside input fields?" |
maxContextLines | The number of surrounding HTML lines to include when copying context. | number | "Enter max context lines to include" |