CLI Commands: `add`, `remove`, `configure`
Relevant source files
The following files were used as context for generating this wiki page:
The react-grab CLI provides a suite of commands to manage agent integrations and customize behavior within a project. While the init command handles the initial setup, the add, remove, and configure commands offer granular control for post-installation management. These commands are registered in the main CLI entry point and leverage a shared set of utilities for project detection, file transformation, and package management.
Sources: packages/cli/src/cli.ts:18-22
Core Architecture
The add, remove, and configure commands are built upon a common set of utilities that handle the core logic of interacting with a user's project.
Project Detection
All commands begin by analyzing the project structure using the detectProject utility. This function is responsible for:
- Identifying the project's framework (Next.js, Vite, Webpack).
- Detecting the package manager (npm, pnpm, yarn, bun).
- Checking if
react-grabis already installed. - Identifying which agent integrations are currently installed.
Sources: packages/cli/src/utils/detect.ts:11-14, packages/cli/src/commands/add.ts:22, packages/cli/src/commands/remove.ts:21, packages/cli/src/commands/configure.ts:21
File Transformation
The CLI modifies project files non-destructively by first previewing changes and then applying them after user confirmation. This is handled by a set of transformation functions.
- Preview Functions:
previewTransform,previewPackageJsonTransform,previewOptionsTransform, andpreviewAgentRemovalgenerate the new file content without writing to disk. They return the original content, new content, and the file path. - Apply Functions:
applyTransform,applyPackageJsonTransform, andapplyOptionsTransformtake the result from a preview function and write the new content to the specified file path.
This two-step process allows the CLI to show a diff of the proposed changes to the user before any files are modified.
Sources: packages/cli/src/utils/transform.ts
The diagram below illustrates the general file modification flow used by the CLI commands.
add Command
The add command is used to install and configure a new agent integration in a project that already has react-grab initialized.
Sources: packages/cli/src/commands/add.ts:16-18
Workflow
The command follows a guided, interactive process to add an agent.
- Detect Project: It first runs
detectProjectto find existingreact-graband agent installations. - Select Agent: It prompts the user to select an agent from a list of available (not yet installed) integrations.
- Handle Conflicts: If other agents are already installed, it asks the user whether to replace the existing agents or add the new one alongside them.
- Preview Changes: It generates previews of the required modifications to the project's layout file (e.g.,
app/layout.tsx) andpackage.json. - Confirm & Apply: After showing a diff and getting user confirmation, it proceeds to:
- Uninstall packages for any agents being replaced using
uninstallPackages. - Install the new agent's package using
installPackages. - Apply the file transformations to the layout file and
package.json.
- Uninstall packages for any agents being replaced using
The following diagram shows the execution flow of the add command.
Supported Agents
The add command supports the same set of agents as the init command.
| Agent Value | Display Name |
|---|---|
claude-code | Claude Code |
cursor | Cursor |
opencode | OpenCode |
codex | Codex |
gemini | Gemini |
amp | Amp |
visual-edit | Visual Edit |
Sources: packages/cli/src/commands/init.ts:88-96, packages/cli/src/commands/add.ts:56-64
remove Command
The remove command uninstalls an agent integration from the project.
Sources: packages/cli/src/commands/remove.ts:15-17
Workflow
- Detect Project: It identifies which agents are currently installed.
- Select Agent: If multiple agents are found, it prompts the user to select which one(s) to remove.
- Preview Changes: It uses
previewAgentRemovalandpreviewPackageJsonAgentRemovalto prepare the file modifications. - Confirm & Apply: After user confirmation, it uninstalls the associated npm package and applies the code changes to remove the agent's script tag and
package.jsonscript.
This diagram outlines the remove command's process.
configure Command
The configure command allows for fine-tuning of react-grab's runtime options after installation.
Sources: packages/cli/src/commands/configure.ts:15-17
Workflow
The command interactively guides the user through the available settings, generates the corresponding options object, and injects it into the react-grab script tag or initialization call.
- Detect Project: Ensures
react-grabis installed. - Prompt for Options: Interactively asks the user to configure settings like the activation key, activation mode, and more.
- Preview Changes: Uses
previewOptionsTransformto generate the updated script tag with adata-optionsattribute or aninit({...})call. - Confirm & Apply: After confirmation, it applies the changes using
applyOptionsTransform.
Configurable Options
The following options can be set using the configure command.
| Option | Type | Description | Default |
|---|---|---|---|
activationKey | object | The keyboard shortcut to activate react-grab. | { altKey: true } (Option/Alt) |
activationMode | 'toggle' | 'hold' | Determines if the key toggles or must be held. | 'toggle' |
keyHoldDuration | number | Milliseconds to hold the key in hold mode. | 150 |
allowActivationInsideInput | boolean | Whether react-grab can be activated while focused on an input field. | true |
maxContextLines | number | The maximum number of surrounding HTML lines to include in the copied context. | 3 |
Sources: packages/cli/src/commands/configure.ts:40-120, packages/cli/src/commands/init.ts:400-488