CLI Command: `init`
Relevant source files
The following files were used as context for generating this wiki page:
The grab@latest init command is the primary entry point for integrating react-grab into a project. It is a command-line interface (CLI) tool designed to automate the installation and configuration process. The command inspects the project to detect the framework (e.g., Next.js, Vite) and package manager, then modifies the necessary files to add the react-grab script.
The init command can be run in a non-interactive mode for quick setup or in an interactive mode that guides the user through configuration, including setting up integrations with various AI coding agents. Its goal is to provide a seamless onboarding experience, eliminating the need for manual setup.
Sources: packages/cli/src/commands/init.ts:142-156, packages/react-grab/README.md:15-18, packages/website/app/blog/agent/page.tsx:186-194
Command Invocation & Options
The init command is executed via npx. It accepts several command-line options to customize its behavior, such as skipping prompts or forcing reconfiguration.
Sources: packages/cli/src/commands/init.ts:142-156
| Option | Alias | Description | Default |
|---|---|---|---|
--yes | -y | Skips all confirmation prompts for non-interactive setup. | false |
--force | -f | Forces the command to overwrite an existing react-grab configuration. | false |
--agent <agent> | -a | Specifies an agent integration to install. | none |
--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:144-155
Execution Flow
The init command follows a structured process to inspect the user's project, gather configuration preferences, and apply the necessary changes. The process includes preflight checks, project detection, interactive prompts (if not skipped), code transformation, and package installation.
The following diagram illustrates the high-level execution flow of the init command.
Project Detection
Before making any changes, the CLI runs a detection process to understand the project's structure. This is handled by the detectProject utility.
- Framework Detection: It checks for dependencies and configuration files to identify the framework. Supported frameworks are Next.js, Vite, and Webpack. It can also identify unsupported frameworks like Remix, Astro, and SvelteKit to provide a helpful message.
- Package Manager Detection: It determines the package manager (npm, yarn, pnpm, bun) being used.
- Monorepo Handling: If it detects a monorepo, it can identify individual workspace projects and prompt the user to select which one to install
react-grabinto. - Existing Installation: The detector also checks if
react-grabor any agent integrations are already present.
Sources: packages/cli/src/commands/init.ts:164, packages/cli/src/utils/detect.ts
This diagram shows the logic for detecting the project's framework.
Interactive Mode & Configuration
If the --yes flag is not provided, the CLI enters an interactive mode.
- New Installation: It prompts the user to select an optional agent integration.
- Existing Installation: If
react-grabis already installed, it offers to:- Add a new agent integration.
- Replace or add to existing agent integrations.
- Customize
react-graboptions, such as the activation key, activation mode, and more.
The following options can be configured interactively:
activationKey: The keyboard shortcut to activatereact-grab.activationMode: Whether the key acts as a 'toggle' or 'hold'.keyHoldDuration: Duration in ms for 'hold' mode.allowActivationInsideInput: Whetherreact-grabcan be activated while inside an input field.maxContextLines: The number of surrounding HTML lines to include in the copied context.
Sources: packages/cli/src/commands/init.ts:182-498
Code Transformation
The core of the init command is its ability to modify project files automatically. It uses a set of transformation utilities to inject code.
previewTransform: Generates a preview of the changes to be made to layout files (e.g.,app/layout.tsxin Next.js) to add thereact-grab<Script>tag.previewPackageJsonTransform: Previews changes topackage.jsonto add agent-specific run scripts.applyTransform/applyPackageJsonTransform: Writes the proposed changes to the files after user confirmation.printDiff: Displays a color-coded diff of the changes to the user for review before they are applied.
Sources: packages/cli/src/commands/init.ts, packages/cli/src/utils/transform.ts
Package Installation
The CLI also handles the installation of required npm packages.
getPackagesToInstall: Determines the list of packages to install based on whether it's a new installation and which agent is being added.installPackages: Runs the appropriate install command for the detected package manager (e.g.,npm install,yarn add).uninstallPackages: Used during agent replacement to remove packages for the old agent.
This functionality can be disabled with the --skip-install flag.
Sources: packages/cli/src/commands/initts:730-749, packages/cli/src/utils/install.ts
Agent Integrations
A key feature of the init command is its ability to configure integrations with AI coding agents. This allows react-grab to send context directly to an agent, streamlining the development workflow.
The CLI can add a script to package.json that starts the agent's local server alongside the project's development server.
Sources: packages/react-grab/README.md:100-108, packages/website/app/blog/agent/page.tsx:392-409
An example of a modified package.json script for the Claude Code agent:
{
"scripts": {
"dev": "npx @react-grab/claude-code@latest && next dev"
}
}
Sources: packages/react-grab/README.md:113-117
The following agents are supported:
| Agent Name | CLI Flag | 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 |
Sources: packages/cli/src/commands/init.ts:108-116, 563-571
This sequence diagram shows the process of adding an agent integration.
Summary
The init command is an essential tool that encapsulates the complexity of setting up react-grab. By automatically detecting the project environment, handling file modifications, and managing package installations, it provides a robust and user-friendly way for developers to start using react-grab and its agent integrations with a single command.
Sources: packages/cli/src/commands/init.ts