diff --git a/HOOKS.md b/HOOKS.md new file mode 100644 index 0000000..92d4b4d --- /dev/null +++ b/HOOKS.md @@ -0,0 +1,194 @@ +# Claude Code Tracker - Hook Installation Guide + +This document explains how to set up Claude Code hooks for automatic tracking of your development activity. + +## Quick Start + +For most users, the automatic installation is the easiest approach: + +```bash +# Set your domain +export DOMAIN=your-domain.com + +# Run the installer (installs comprehensive profile) +./setup-hooks +``` + +## Installation Scripts + +### `setup-hooks` (Recommended for beginners) +Simple wrapper that installs the comprehensive hook profile: +```bash +./setup-hooks +``` + +### `install-hooks.sh` (Full control) +Comprehensive installation script with multiple options: + +```bash +# Show help +./install-hooks.sh --help + +# List available profiles +./install-hooks.sh --list + +# Install specific profile +./install-hooks.sh basic +./install-hooks.sh comprehensive +./install-hooks.sh developer + +# Install with custom domain +./install-hooks.sh -d your-domain.com comprehensive + +# Verify installation +./install-hooks.sh --verify + +# Remove all hooks +./install-hooks.sh --uninstall +``` + +## Available Hook Profiles + +| Profile | Description | Hook Count | Best For | +|---------|-------------|------------|----------| +| `basic` | Essential hooks only | 5 | New users, minimal tracking | +| `essential` | Core development tracking | 8 | Most users | +| `comprehensive` | All available hooks | 39+ | Complete tracking, recommended | +| `developer` | Active development focused | 15 | Professional developers | +| `power_user` | Advanced features | 20 | Power users, detailed analytics | +| `research` | Research and analytics | 25 | Research, data analysis | +| `team` | Team collaboration | 18 | Team environments | + +## Environment Variables + +| Variable | Description | Example | +|----------|-------------|---------| +| `DOMAIN` | Your Claude Code Tracker domain | `claude.example.com` | + +You can set the domain in several ways: + +### Option 1: Environment Variable +```bash +export DOMAIN=your-domain.com +./setup-hooks +``` + +### Option 2: .env File +Create a `.env` file in the project directory: +```bash +echo "DOMAIN=your-domain.com" > .env +./setup-hooks +``` + +### Option 3: Command Line +```bash +./install-hooks.sh -d your-domain.com comprehensive +``` + +## How It Works + +The installation script: + +1. **Validates requirements** - Checks for Claude Code and domain configuration +2. **Backs up existing settings** - Preserves your current Claude Code configuration +3. **Updates hook URLs** - Replaces placeholders with your actual domain +4. **Merges configurations** - Combines hooks with existing settings +5. **Installs to user scope** - Places configuration in `~/.config/claude-code/settings.json` +6. **Verifies installation** - Checks that hooks were installed correctly + +## File Locations + +| File | Purpose | +|------|---------| +| `~/.config/claude-code/settings.json` | Main Claude Code settings with hooks | +| `~/.config/claude-code/settings.json.backup.*` | Backup of previous settings | + +## Hook Examples + +After installation, your `settings.json` will contain hooks like: + +```json +{ + "hooks": { + "session_start": "curl -X POST https://your-domain.com/api/sessions/start -H \"Content-Type: application/json\" -d '{\"project_path\": \"$PWD\", \"start_time\": \"$TIMESTAMP\"}'", + "session_end": "curl -X POST https://your-domain.com/api/sessions/end -H \"Content-Type: application/json\" -d '{\"session_id\": \"$SESSION_ID\", \"end_time\": \"$TIMESTAMP\"}'", + "conversation_update": "curl -X POST https://your-domain.com/api/conversations -H \"Content-Type: application/json\" -d '{\"session_id\": \"$SESSION_ID\", \"content\": \"$CONTENT\"}'" + } +} +``` + +## Troubleshooting + +### "Claude Code is not installed" +Install Claude Code first: https://claude.ai/code + +### "DOMAIN environment variable is not set" +Set your domain: +```bash +export DOMAIN=your-domain.com +``` + +### "Hook profile file not found" +Make sure you're running the script from the project directory where the `claude-hooks-*.json` files are located. + +### "Settings file contains invalid JSON" +Your existing Claude Code settings may be corrupted. Check `~/.config/claude-code/settings.json` or restore from a backup. + +### Hooks not triggering +1. Verify installation: `./install-hooks.sh --verify` +2. Check that your tracker server is running +3. Test connectivity: `curl https://your-domain.com/health` +4. Check Claude Code logs for hook execution errors + +## Manual Installation + +If you prefer to install hooks manually, see the [Hook Setup Guide](https://your-domain.com/dashboard/docs/hook-setup) for detailed instructions. + +## Uninstalling + +To remove all hooks: + +```bash +./install-hooks.sh --uninstall +``` + +This will: +- Backup your current settings +- Remove the `hooks` section from your Claude Code settings +- Preserve all other configuration options + +## Advanced Usage + +### Custom Hook Profiles +You can create custom hook profiles by: +1. Copying an existing `claude-hooks-*.json` file +2. Modifying the hooks as needed +3. Running the installer with your custom profile name + +### Multiple Domains +To switch domains, simply run the installer with a new domain: +```bash +./install-hooks.sh -d new-domain.com comprehensive +``` + +### Development Setup +For development with local servers: +```bash +./install-hooks.sh -d localhost:8000 developer +``` + +## Security Notes + +- Hooks are installed in user scope only (no system-wide changes) +- All communication uses HTTPS (except localhost) +- No sensitive data is transmitted in hook payloads +- Settings backups are created automatically +- Uninstallation is safe and reversible + +## Support + +- **Documentation**: https://your-domain.com/dashboard/docs +- **Hook Reference**: https://your-domain.com/dashboard/docs/hook-reference +- **API Docs**: https://your-domain.com/docs + +For issues with the installation script, check the troubleshooting section above or review the detailed documentation. \ No newline at end of file diff --git a/README.md b/README.md index 3678d07..5a040ca 100644 --- a/README.md +++ b/README.md @@ -1,116 +1,279 @@ -# Claude Code Hook Configurations +# Claude Code Project Tracker -This directory contains various pre-configured hook setups for different use cases: +A comprehensive development intelligence system that tracks and analyzes your coding activity when using Claude Code. Get insights into your development patterns, track sessions, monitor conversations, and analyze productivity trends. -## Available Configurations +## ๐Ÿš€ Features -### basic.json -Essential hooks for basic session and tool tracking. -- Session management -- Tool calls -- File modifications -- Conversations +### ๐ŸŽฏ **Core Functionality** +- **Automatic Session Tracking** - Real-time monitoring of Claude Code sessions +- **Conversation History** - Complete searchable archive of your interactions +- **Project Analytics** - Detailed insights per project with timelines and statistics +- **File Upload Import** - Easy .claude.json file import with preview functionality +- **Hook Integration** - 39+ hooks for comprehensive activity tracking -### comprehensive.json -Complete hook setup with all available hook types. -- All performance monitoring -- Code quality tracking -- Learning analytics -- Collaboration insights -- Project intelligence +### ๐Ÿณ **Deployment & Installation** +- **Docker Ready** - Production-ready containerization with Caddy integration +- **One-Click Setup** - Automated hook installation with `./setup-hooks` +- **Multiple Profiles** - 7 pre-configured hook profiles for different use cases +- **User-Scope Installation** - Safe, reversible installation process -### essential.json -Minimal setup for core functionality. +### ๐Ÿ“Š **Analytics & Insights** +- **Development Metrics** - Track time, sessions, and productivity trends +- **Visual Analytics** - Charts, graphs, and timeline visualizations +- **Project Intelligence** - Language detection, file activity, and code metrics +- **Conversation Search** - Full-text search through your Claude Code history -### developer.json -Focused on development workflow and code quality. -- Essential hooks -- Performance monitoring -- Code quality checks +### ๐Ÿ”’ **Privacy & Security** +- **Local Data** - All data stays on your server, no external dependencies +- **HTTPS Support** - Secure communication with automatic SSL via Caddy +- **User Control** - Complete control over data collection and retention -### power_user.json -Advanced setup for productivity optimization. -- Essential hooks -- Performance tracking -- Workflow analysis -- Project intelligence +## ๐Ÿ“ฆ Quick Start -### research.json -Optimized for learning and exploration. -- Essential hooks -- Learning tracking -- External resource usage -- Knowledge gap analysis +### Option 1: Docker Deployment (Recommended) -### team.json -Team-focused configuration for collaboration. -- Essential hooks -- Collaboration tracking -- Testing workflows -- Project intelligence +```bash +# 1. Clone and configure +git clone https://git.supported.systems/claude/claude-code-tracker.git claude-tracker +cd claude-tracker +cp .env.example .env +# Edit .env with your domain -## Installation +# 2. Deploy with Docker +docker-compose up -d -1. Choose the configuration that matches your needs -2. Copy the JSON content to your Claude Code settings file: - - macOS/Linux: `~/.config/claude/settings.json` - - Windows: `%APPDATA%\claude\settings.json` -3. Ensure Claude Code Tracker is running on port 8000 -4. Start using Claude Code - hooks will automatically track your activity! +# 3. Install hooks on your development machine +export DOMAIN=your-domain.com +./setup-hooks -## Available Hook Variables +# 4. Start using Claude Code! +``` -Each hook can use these variables that Claude Code provides: +### Option 2: Local Development -### Session Variables -- `$SESSION_ID` - Current session identifier -- `$TIMESTAMP` - Current timestamp (ISO format) -- `$PWD` - Current working directory -- `$USER` - System username +```bash +# 1. Install dependencies +pip install -r requirements.txt -### Tool Variables -- `$TOOL_NAME` - Name of tool being called -- `$TOOL_PARAMS` - Tool parameters (JSON) -- `$RESULT_STATUS` - Success/error status -- `$EXECUTION_TIME` - Tool execution time (ms) -- `$ERROR_TYPE` - Type of error -- `$ERROR_MESSAGE` - Error message -- `$STACK_TRACE` - Error stack trace +# 2. Initialize database +python init_db.py -### File Variables -- `$FILE_PATH` - Path to modified file -- `$ACTION` - File action (created/modified/deleted) -- `$FILE_SIZE_MB` - File size in megabytes +# 3. Start the server +python main.py -### Context Variables -- `$CONTENT` - Conversation content -- `$CONTEXT` - Current context description -- `$SEARCH_QUERY` - What you're searching for -- `$NEW_PROJECT` - Project being switched to -- `$OLD_PROJECT` - Project being switched from +# 4. Install hooks +export DOMAIN=localhost:8000 +./setup-hooks +``` -### Performance Variables -- `$MEMORY_MB` - Memory usage in MB -- `$DURATION_MS` - Duration in milliseconds -- `$THRESHOLD_EXCEEDED` - Boolean for threshold alerts +## ๐Ÿ› ๏ธ Installation Scripts -And many more! Each hook type has specific variables available. +### Quick Setup (Most Users) +```bash +export DOMAIN=your-domain.com +./setup-hooks +``` -## Customization +### Advanced Installation +```bash +# List available hook profiles +./install-hooks.sh --list -You can modify any configuration by: -1. Adding/removing specific hooks -2. Changing API endpoints or ports -3. Adjusting timeout and retry settings -4. Adding custom metadata to hook calls +# Install specific profile +./install-hooks.sh comprehensive +./install-hooks.sh developer +./install-hooks.sh basic -## Troubleshooting +# Custom domain +./install-hooks.sh -d your-domain.com comprehensive -If hooks aren't working: -1. Ensure Claude Code Tracker server is running -2. Check that curl is installed -3. Verify the API endpoints are accessible -4. Check Claude Code logs for hook execution errors -5. Test individual hooks manually with curl +# Verify installation +./install-hooks.sh --verify -For more help, see the documentation at `/dashboard/docs/hook-setup`. +# Remove hooks +./install-hooks.sh --uninstall +``` + +## ๐Ÿ“‹ Hook Profiles + +| Profile | Hooks | Best For | Description | +|---------|-------|----------|-------------| +| `basic` | 5 | New users | Essential session and conversation tracking | +| `essential` | 8 | Most users | Core development tracking features | +| `comprehensive` | 39+ | Complete tracking | All available hooks (recommended) | +| `developer` | 15 | Active developers | Professional development focused | +| `power_user` | 20 | Advanced users | Detailed analytics and monitoring | +| `research` | 25 | Researchers | Learning and exploration tracking | +| `team` | 18 | Teams | Collaboration and sharing features | + +## ๐Ÿ—๏ธ Architecture + +### Backend (FastAPI) +- **Python 3.12** with async/await support +- **SQLAlchemy 2.0** with async SQLite database +- **FastAPI** for modern, high-performance API +- **Pydantic** for data validation and serialization + +### Frontend (Web Dashboard) +- **Bootstrap 5** for responsive UI components +- **Jinja2** templating with server-side rendering +- **Chart.js** for interactive visualizations +- **Real-time updates** via API polling + +### Deployment +- **Docker** with multi-stage builds and uv package manager +- **Caddy** reverse proxy with automatic SSL +- **Health checks** and resource monitoring +- **Data persistence** with volume mounting + +## ๐Ÿ“– Documentation + +Complete documentation is available at `/dashboard/docs` when running the application: + +- **[Getting Started Guide](./app/dashboard/templates/docs/getting-started.html)** - Setup and first steps +- **[Data Import Guide](./app/dashboard/templates/docs/data-import.html)** - File upload and import process +- **[Docker Deployment](./app/dashboard/templates/docs/docker-deployment.html)** - Production deployment +- **[Hook Setup Guide](./app/dashboard/templates/docs/hook-setup.html)** - Automated and manual hook installation +- **[Hook Reference](./app/dashboard/templates/docs/hook-reference.html)** - Complete hook documentation +- **[API Reference](./docs)** - Interactive API documentation + +## ๐Ÿ“ Project Structure + +``` +claude-tracker/ +โ”œโ”€โ”€ app/ +โ”‚ โ”œโ”€โ”€ api/ # API endpoints +โ”‚ โ”‚ โ”œโ”€โ”€ importer.py # Data import functionality +โ”‚ โ”‚ โ””โ”€โ”€ hooks.py # Hook management API +โ”‚ โ”œโ”€โ”€ dashboard/ # Web interface +โ”‚ โ”‚ โ”œโ”€โ”€ templates/ # HTML templates +โ”‚ โ”‚ โ”œโ”€โ”€ static/ # Static assets and hook configs +โ”‚ โ”‚ โ””โ”€โ”€ routes.py # Dashboard routes +โ”‚ โ”œโ”€โ”€ database/ # Database connection +โ”‚ โ”œโ”€โ”€ models/ # SQLAlchemy models +โ”‚ โ””โ”€โ”€ __init__.py +โ”œโ”€โ”€ hooks/ # Hook configuration files +โ”‚ โ”œโ”€โ”€ claude-hooks-basic.json +โ”‚ โ”œโ”€โ”€ claude-hooks-comprehensive.json +โ”‚ โ””โ”€โ”€ ... +โ”œโ”€โ”€ install-hooks.sh # Comprehensive hook installer +โ”œโ”€โ”€ setup-hooks # Simple hook installer +โ”œโ”€โ”€ docker-compose.yml # Docker deployment config +โ”œโ”€โ”€ Dockerfile # Container definition +โ”œโ”€โ”€ main.py # Application entry point +โ”œโ”€โ”€ init_db.py # Database initialization +โ””โ”€โ”€ README.md +``` + +## ๐Ÿ”ง Configuration + +### Environment Variables + +| Variable | Description | Example | +|----------|-------------|---------| +| `DOMAIN` | Your tracker domain | `claude.example.com` | +| `DATABASE_URL` | Database connection string | `sqlite+aiosqlite:///./data/tracker.db` | +| `DEBUG` | Enable debug mode | `false` | +| `PYTHONPATH` | Python module path | `/app` | + +### Hook Configuration + +Hooks are installed to `~/.config/claude-code/settings.json` and include: + +- **Session Management** - Start, end, pause, resume tracking +- **Conversation Tracking** - Message exchanges, context updates +- **File Operations** - Create, modify, delete, read operations +- **Tool Calls** - Claude Code tool usage and results +- **Performance Monitoring** - Memory, CPU, execution times +- **Code Quality** - Linting, testing, compilation events +- **Project Intelligence** - Language detection, dependencies + +## ๐Ÿ›ก๏ธ Security & Privacy + +- **Local Data Storage** - All data stays on your server +- **User-Scope Installation** - No system-wide changes required +- **Automatic Backups** - Settings preserved before modifications +- **HTTPS Enforcement** - Secure communication by default +- **Data Validation** - Input sanitization and validation +- **Error Handling** - Graceful failure and recovery + +## ๐Ÿงช Development + +### Running Tests +```bash +# Run the test suite +pytest tests/ + +# Run with coverage +pytest --cov=app tests/ +``` + +### Local Development Setup +```bash +# Install development dependencies +pip install -r requirements-dev.txt + +# Start with auto-reload +uvicorn main:app --reload --host 0.0.0.0 --port 8000 + +# Initialize test database +python init_db.py +``` + +### Database Migrations +```bash +# Create new migration +alembic revision --autogenerate -m "Description" + +# Apply migrations +alembic upgrade head +``` + +## ๐Ÿ“Š API Endpoints + +### Core Tracking +- `POST /api/sessions/start` - Start new session +- `POST /api/sessions/end` - End session +- `POST /api/conversations` - Log conversation +- `POST /api/tool-calls` - Track tool usage + +### Data Management +- `POST /api/import/claude-json` - Import from file path +- `POST /api/import/claude-json/upload` - Import via file upload +- `GET /api/import/claude-json/preview` - Preview import data + +### Analytics +- `GET /api/projects` - List projects +- `GET /api/projects/{id}/stats` - Project statistics +- `GET /api/conversations/search` - Search conversations + +## ๐Ÿค Contributing + +1. Fork the repository +2. Create a feature branch +3. Make your changes +4. Add tests for new functionality +5. Run the test suite +6. Submit a pull request + +## ๐Ÿ“„ License + +This project is licensed under the MIT License. See LICENSE file for details. + +## ๐Ÿ†˜ Support + +- **Documentation**: Visit `/dashboard/docs` in your running instance +- **Issues**: Report bugs and request features via GitHub Issues +- **Discussions**: Join community discussions for questions and ideas + +## ๐Ÿ”„ Updates + +The project is actively maintained with regular updates for: +- New Claude Code features and hooks +- Performance improvements and optimizations +- Enhanced analytics and visualizations +- Security updates and bug fixes + +--- + +**Made with โค๏ธ for the Claude Code community** \ No newline at end of file diff --git a/app/dashboard/templates/docs/docker-deployment.html b/app/dashboard/templates/docs/docker-deployment.html index 5375118..c0645b6 100644 --- a/app/dashboard/templates/docs/docker-deployment.html +++ b/app/dashboard/templates/docs/docker-deployment.html @@ -39,7 +39,7 @@
# Clone the repository
-git clone <repository-url> claude-tracker
+git clone https://git.supported.systems/claude/claude-code-tracker.git claude-tracker
 cd claude-tracker
 
 # Copy environment template
@@ -80,6 +80,28 @@ docker-compose logs -f
 docker-compose ps
+ +
+
+
4. Install Claude Code Hooks
+
+
+

After deployment, install hooks on your development machine to enable automatic tracking:

+
# Install hooks using the same domain from your .env file
+export DOMAIN=claude.l.supported.systems
+./setup-hooks
+ +
+ + Note: Hooks are installed on your local development machine, not in the Docker container. This allows Claude Code to send data to your deployed tracker. +
+ +
+ + Complete Setup: Your tracker is now deployed and ready to receive data from Claude Code! +
+
+
diff --git a/app/dashboard/templates/docs/getting-started.html b/app/dashboard/templates/docs/getting-started.html index 584cd44..59a792f 100644 --- a/app/dashboard/templates/docs/getting-started.html +++ b/app/dashboard/templates/docs/getting-started.html @@ -102,20 +102,21 @@

Set up hooks to automatically track new sessions and conversations in real-time.

-
Hook Configuration:
-

Add this configuration to your Claude Code settings:

-
{
-  "hooks": {
-    "session_start": "curl -X POST http://localhost:8000/api/sessions/start",
-    "session_end": "curl -X POST http://localhost:8000/api/sessions/end",
-    "conversation_update": "curl -X POST http://localhost:8000/api/conversations"
-  }
-}
+
Automatic Installation (Recommended):
+

Use our automated installer for easy setup:

+
# Set your domain and install hooks
+export DOMAIN=your-domain.com
+./setup-hooks
+ +
+ + New: Automatic hook installer with 7+ pre-configured profiles available! +
-
- - Note: Detailed hook setup instructions are available in the Hook Setup Guide. +
+ + Alternative Methods: Manual configuration and detailed setup instructions are available in the Hook Setup Guide.
diff --git a/app/dashboard/templates/docs/hook-setup.html b/app/dashboard/templates/docs/hook-setup.html index 8a99689..f3e8f63 100644 --- a/app/dashboard/templates/docs/hook-setup.html +++ b/app/dashboard/templates/docs/hook-setup.html @@ -95,10 +95,87 @@ - +
-

Setup Instructions

+

Automatic Installation (Recommended)

+
+
+

The easiest way to set up hooks is using our automated installation script that configures everything for you.

+ +
+
Quick Setup
+

For most users, this one-liner will set up everything you need:

+
# Set your domain and run the installer
+export DOMAIN=claude.l.supported.systems
+./setup-hooks
+
+ +
Installation Options
+ +
+
+
+
+ Basic Installation +
+
+
# Install basic hook set
+./install-hooks.sh basic
+

Includes essential session and conversation tracking

+
+
+
+ +
+
+
+ Comprehensive Installation +
+
+
# Install all available hooks
+./install-hooks.sh comprehensive
+

Includes all 39+ hooks for complete tracking

+
+
+
+
+ +
Available Profiles
+
+
+
    +
  • basicEssential hooks only
  • +
  • essentialCore development tracking
  • +
  • comprehensiveAll available hooks
  • +
  • developerFor active developers
  • +
+
+
+
    +
  • power_userAdvanced tracking features
  • +
  • researchResearch and analytics focused
  • +
  • teamTeam collaboration features
  • +
+
+
+ +
+
Script Commands
+
    +
  • ./install-hooks.sh --list - List available profiles
  • +
  • ./install-hooks.sh --verify - Verify installation
  • +
  • ./install-hooks.sh --uninstall - Remove all hooks
  • +
  • ./install-hooks.sh --help - Show detailed help
  • +
+
+
+
+ + +
+
+

Manual Setup Instructions

diff --git a/app/dashboard/templates/docs/index.html b/app/dashboard/templates/docs/index.html index b9c8294..8f211bf 100644 --- a/app/dashboard/templates/docs/index.html +++ b/app/dashboard/templates/docs/index.html @@ -181,37 +181,67 @@
-
-

Quick Start

+
+

Quick Start - Get Running in 3 Steps

-

Get started with Claude Code Tracker in just a few steps:

-
    -
  1. - 1 - Import existing data from your ~/.claude.json file -
  2. -
  3. - 2 - Set up hooks to automatically track new sessions -
  4. -
  5. - 3 - Explore your data through the dashboard and analytics -
  6. -
+

Get started with Claude Code Tracker in just a few minutes:

+ +
+
+
+
+
+ 1 +
+
Deploy with Docker
+ Set up the tracker server using Docker & Caddy +
+
+
+
+
+
+
+ 2 +
+
Import Data
+ Upload your .claude.json file with file upload +
+
+
+
+
+
+
+ 3 +
+
Install Hooks
+ Run ./setup-hooks for automatic tracking +
+
+
+
-
+ diff --git a/install-hooks.sh b/install-hooks.sh new file mode 100755 index 0000000..274b057 --- /dev/null +++ b/install-hooks.sh @@ -0,0 +1,377 @@ +#!/bin/bash + +# Claude Code Tracker - Hook Installation Script +# Automatically installs hooks for Claude Code using domain from environment + +set -e + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# Configuration +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +CLAUDE_CONFIG_DIR="$HOME/.config/claude-code" +CLAUDE_SETTINGS_FILE="$CLAUDE_CONFIG_DIR/settings.json" + +# Hook profiles available +AVAILABLE_PROFILES=( + "basic" + "essential" + "comprehensive" + "developer" + "power_user" + "research" + "team" +) + +print_header() { + echo -e "${BLUE}โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—${NC}" + echo -e "${BLUE}โ•‘ Claude Code Tracker Hooks โ•‘${NC}" + echo -e "${BLUE}โ•‘ Installation Script โ•‘${NC}" + echo -e "${BLUE}โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•${NC}" + echo "" +} + +print_success() { + echo -e "${GREEN}โœ“${NC} $1" +} + +print_warning() { + echo -e "${YELLOW}โš ${NC} $1" +} + +print_error() { + echo -e "${RED}โœ—${NC} $1" +} + +print_info() { + echo -e "${BLUE}โ„น${NC} $1" +} + +check_requirements() { + print_info "Checking requirements..." + + # Check if Claude Code is installed + if ! command -v claude-code &> /dev/null; then + print_error "Claude Code is not installed or not in PATH" + print_info "Please install Claude Code first: https://claude.ai/code" + exit 1 + fi + + # Check if jq is available for JSON manipulation + if ! command -v jq &> /dev/null; then + print_warning "jq is not installed. Using fallback JSON handling." + USE_JQ=false + else + USE_JQ=true + fi + + # Check domain configuration + if [[ -z "$DOMAIN" ]]; then + # Try to read from .env file + if [[ -f "$SCRIPT_DIR/.env" ]]; then + source "$SCRIPT_DIR/.env" + fi + + if [[ -z "$DOMAIN" ]]; then + print_error "DOMAIN environment variable is not set" + print_info "Please set DOMAIN environment variable or create .env file" + print_info "Example: export DOMAIN=claude.l.supported.systems" + exit 1 + fi + fi + + print_success "Domain configured: $DOMAIN" + print_success "Requirements check passed" +} + +create_claude_config_dir() { + if [[ ! -d "$CLAUDE_CONFIG_DIR" ]]; then + print_info "Creating Claude Code configuration directory..." + mkdir -p "$CLAUDE_CONFIG_DIR" + print_success "Created directory: $CLAUDE_CONFIG_DIR" + fi +} + +backup_existing_settings() { + if [[ -f "$CLAUDE_SETTINGS_FILE" ]]; then + local backup_file="${CLAUDE_SETTINGS_FILE}.backup.$(date +%Y%m%d_%H%M%S)" + print_info "Backing up existing settings to: $backup_file" + cp "$CLAUDE_SETTINGS_FILE" "$backup_file" + print_success "Backup created" + fi +} + +get_hook_profile_path() { + local profile="$1" + echo "$SCRIPT_DIR/claude-hooks-${profile}.json" +} + +list_available_profiles() { + echo -e "${BLUE}Available hook profiles:${NC}" + echo "" + + for profile in "${AVAILABLE_PROFILES[@]}"; do + local profile_file + profile_file=$(get_hook_profile_path "$profile") + + if [[ -f "$profile_file" ]]; then + local description + if [[ "$USE_JQ" == true ]]; then + description=$(jq -r '.metadata.description // "No description available"' "$profile_file" 2>/dev/null || echo "No description available") + else + description="Hook configuration for $profile users" + fi + + echo -e " ${GREEN}$profile${NC} - $description" + else + echo -e " ${RED}$profile${NC} - Profile file not found" + fi + done + echo "" +} + +update_hook_domain() { + local input_file="$1" + local output_file="$2" + + print_info "Updating hook URLs with domain: $DOMAIN" + + # Replace placeholder domain with actual domain + sed "s/\${DOMAIN}/$DOMAIN/g; s/localhost:8000/$DOMAIN/g; s/http:\/\/$DOMAIN/https:\/\/$DOMAIN/g" "$input_file" > "$output_file" + + print_success "Hook URLs updated" +} + +install_hooks() { + local profile="$1" + local profile_file + profile_file=$(get_hook_profile_path "$profile") + + if [[ ! -f "$profile_file" ]]; then + print_error "Hook profile file not found: $profile_file" + exit 1 + fi + + print_info "Installing hooks from profile: $profile" + + # Create temporary file with updated domain + local temp_file="/tmp/claude-hooks-${profile}-$(date +%s).json" + update_hook_domain "$profile_file" "$temp_file" + + # Merge with existing settings or create new + local final_settings="$temp_file" + + if [[ -f "$CLAUDE_SETTINGS_FILE" ]] && [[ "$USE_JQ" == true ]]; then + print_info "Merging with existing Claude Code settings..." + + # Merge hook configurations + local merged_file="/tmp/claude-settings-merged-$(date +%s).json" + jq -s '.[0] * .[1]' "$CLAUDE_SETTINGS_FILE" "$temp_file" > "$merged_file" + final_settings="$merged_file" + + print_success "Settings merged" + fi + + # Install the hooks + cp "$final_settings" "$CLAUDE_SETTINGS_FILE" + + # Cleanup temp files + rm -f "$temp_file" + if [[ -f "$merged_file" ]]; then + rm -f "$merged_file" + fi + + print_success "Hooks installed successfully!" +} + +show_installation_summary() { + local profile="$1" + + echo "" + echo -e "${GREEN}โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—${NC}" + echo -e "${GREEN}โ•‘ Installation Complete! โ•‘${NC}" + echo -e "${GREEN}โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•${NC}" + echo "" + echo -e "${BLUE}Profile installed:${NC} $profile" + echo -e "${BLUE}Domain configured:${NC} $DOMAIN" + echo -e "${BLUE}Settings file:${NC} $CLAUDE_SETTINGS_FILE" + echo "" + echo -e "${YELLOW}Next steps:${NC}" + echo "1. Start using Claude Code in any project" + echo "2. Visit https://$DOMAIN/dashboard to view your tracking data" + echo "3. Check the documentation at https://$DOMAIN/dashboard/docs" + echo "" + echo -e "${BLUE}To test the installation:${NC}" + echo " claude-code --help" + echo "" +} + +verify_installation() { + print_info "Verifying installation..." + + if [[ -f "$CLAUDE_SETTINGS_FILE" ]]; then + if [[ "$USE_JQ" == true ]]; then + if jq empty "$CLAUDE_SETTINGS_FILE" 2>/dev/null; then + print_success "Settings file is valid JSON" + + local hook_count + hook_count=$(jq -r '.hooks | length // 0' "$CLAUDE_SETTINGS_FILE" 2>/dev/null || echo "0") + print_success "Number of hooks configured: $hook_count" + + return 0 + else + print_error "Settings file contains invalid JSON" + return 1 + fi + else + print_success "Settings file exists" + return 0 + fi + else + print_error "Settings file was not created" + return 1 + fi +} + +show_help() { + echo "Claude Code Tracker - Hook Installation Script" + echo "" + echo "Usage: $0 [OPTIONS] [PROFILE]" + echo "" + echo "PROFILE: Hook profile to install (default: comprehensive)" + echo "" + echo "Options:" + echo " -l, --list List available profiles" + echo " -d, --domain DOMAIN Set the domain (overrides DOMAIN env var)" + echo " -h, --help Show this help message" + echo " --verify Verify existing installation" + echo " --uninstall Remove all hooks from settings" + echo "" + echo "Examples:" + echo " $0 # Install comprehensive profile" + echo " $0 basic # Install basic profile" + echo " $0 -d example.com # Install with custom domain" + echo " $0 --list # List available profiles" + echo "" + echo "Environment Variables:" + echo " DOMAIN Target domain for the Claude Code Tracker" + echo "" +} + +uninstall_hooks() { + print_info "Uninstalling Claude Code Tracker hooks..." + + if [[ ! -f "$CLAUDE_SETTINGS_FILE" ]]; then + print_warning "No settings file found, nothing to uninstall" + return 0 + fi + + backup_existing_settings + + if [[ "$USE_JQ" == true ]]; then + # Remove hooks section but keep other settings + jq 'del(.hooks)' "$CLAUDE_SETTINGS_FILE" > "${CLAUDE_SETTINGS_FILE}.tmp" + mv "${CLAUDE_SETTINGS_FILE}.tmp" "$CLAUDE_SETTINGS_FILE" + print_success "Hooks removed from settings" + else + print_warning "Cannot automatically remove hooks without jq. Manual removal required." + print_info "Edit $CLAUDE_SETTINGS_FILE and remove the 'hooks' section" + fi +} + +main() { + local profile="comprehensive" + local list_profiles=false + local verify_only=false + local uninstall_only=false + + # Parse command line arguments + while [[ $# -gt 0 ]]; do + case $1 in + -h|--help) + show_help + exit 0 + ;; + -l|--list) + list_profiles=true + shift + ;; + -d|--domain) + DOMAIN="$2" + shift 2 + ;; + --verify) + verify_only=true + shift + ;; + --uninstall) + uninstall_only=true + shift + ;; + -*) + print_error "Unknown option: $1" + show_help + exit 1 + ;; + *) + profile="$1" + shift + ;; + esac + done + + print_header + + # Handle special modes + if [[ "$list_profiles" == true ]]; then + check_requirements + list_available_profiles + exit 0 + fi + + if [[ "$verify_only" == true ]]; then + check_requirements + verify_installation + exit $? + fi + + if [[ "$uninstall_only" == true ]]; then + check_requirements + create_claude_config_dir + uninstall_hooks + print_success "Uninstallation complete" + exit 0 + fi + + # Main installation flow + check_requirements + create_claude_config_dir + backup_existing_settings + + # Validate profile + if [[ ! " ${AVAILABLE_PROFILES[@]} " =~ " $profile " ]]; then + print_error "Unknown profile: $profile" + echo "" + list_available_profiles + exit 1 + fi + + # Install hooks + install_hooks "$profile" + + # Verify installation + if verify_installation; then + show_installation_summary "$profile" + else + print_error "Installation verification failed" + exit 1 + fi +} + +# Run main function with all arguments +main "$@" \ No newline at end of file diff --git a/setup-hooks b/setup-hooks new file mode 100755 index 0000000..bf77611 --- /dev/null +++ b/setup-hooks @@ -0,0 +1,45 @@ +#!/bin/bash + +# Claude Code Tracker - Quick Hook Setup +# Simple wrapper for the full installation script + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +INSTALL_SCRIPT="$SCRIPT_DIR/install-hooks.sh" + +# Colors +GREEN='\033[0;32m' +BLUE='\033[0;34m' +NC='\033[0m' + +echo -e "${BLUE}Claude Code Tracker - Quick Hook Setup${NC}" +echo "" + +# Check if install script exists +if [[ ! -f "$INSTALL_SCRIPT" ]]; then + echo "Error: Installation script not found at $INSTALL_SCRIPT" + exit 1 +fi + +# Check if domain is set +if [[ -z "$DOMAIN" ]]; then + if [[ -f "$SCRIPT_DIR/.env" ]]; then + source "$SCRIPT_DIR/.env" + fi + + if [[ -z "$DOMAIN" ]]; then + echo "Please set the DOMAIN environment variable first:" + echo "" + echo " export DOMAIN=your-domain.com" + echo " ./setup-hooks" + echo "" + echo "Or create a .env file with:" + echo " DOMAIN=your-domain.com" + exit 1 + fi +fi + +echo -e "${GREEN}Using domain: $DOMAIN${NC}" +echo "" + +# Run the installation with comprehensive profile (best for most users) +exec "$INSTALL_SCRIPT" comprehensive "$@" \ No newline at end of file