1#!/bin/bash
  2
  3# Script to install pre-commit and pre-push hooks for webui
  4# Pre-commit: formats code and runs checks
  5# Pre-push: builds the project, stashes unstaged changes
  6
  7REPO_ROOT=$(git rev-parse --show-toplevel)
  8PRE_COMMIT_HOOK="$REPO_ROOT/.git/hooks/pre-commit"
  9PRE_PUSH_HOOK="$REPO_ROOT/.git/hooks/pre-push"
 10
 11echo "Installing pre-commit and pre-push hooks for webui..."
 12
 13# Create the pre-commit hook
 14cat > "$PRE_COMMIT_HOOK" << 'EOF'
 15#!/bin/bash
 16
 17# Check if there are any changes in the webui directory
 18if git diff --cached --name-only | grep -q "^tools/server/webui/"; then
 19    echo "Formatting and checking webui code..."
 20    
 21    # Change to webui directory and run format
 22    cd tools/server/webui
 23    
 24    # Check if npm is available and package.json exists
 25    if [ ! -f "package.json" ]; then
 26        echo "Error: package.json not found in tools/server/webui"
 27        exit 1
 28    fi
 29    
 30    # Run the format command
 31    npm run format
 32
 33    # Check if format command succeeded
 34    if [ $? -ne 0 ]; then
 35        echo "Error: npm run format failed"
 36        exit 1
 37    fi
 38
 39    # Run the lint command
 40    npm run lint
 41    
 42    # Check if lint command succeeded
 43    if [ $? -ne 0 ]; then
 44        echo "Error: npm run lint failed"
 45        exit 1
 46    fi
 47
 48    # Run the check command
 49    npm run check
 50    
 51    # Check if check command succeeded
 52    if [ $? -ne 0 ]; then
 53        echo "Error: npm run check failed"
 54        exit 1
 55    fi
 56
 57    # Go back to repo root
 58    cd ../../..
 59    
 60    echo "✅ Webui code formatted and checked successfully"
 61fi
 62
 63exit 0
 64EOF
 65
 66# Create the pre-push hook
 67cat > "$PRE_PUSH_HOOK" << 'EOF'
 68#!/bin/bash
 69
 70# Check if there are any webui changes that need building
 71WEBUI_CHANGES=$(git diff --name-only @{push}..HEAD | grep "^tools/server/webui/" || true)
 72
 73if [ -n "$WEBUI_CHANGES" ]; then
 74    echo "Webui changes detected, checking if build is up-to-date..."
 75    
 76    # Change to webui directory
 77    cd tools/server/webui
 78    
 79    # Check if npm is available and package.json exists
 80    if [ ! -f "package.json" ]; then
 81        echo "Error: package.json not found in tools/server/webui"
 82        exit 1
 83    fi
 84    
 85    # Check if build output exists and is newer than source files
 86    BUILD_FILE="../public/index.html.gz"
 87    NEEDS_BUILD=false
 88    
 89    if [ ! -f "$BUILD_FILE" ]; then
 90        echo "Build output not found, building..."
 91        NEEDS_BUILD=true
 92    else
 93        # Check if any source files are newer than the build output
 94        if find src -newer "$BUILD_FILE" -type f | head -1 | grep -q .; then
 95            echo "Source files are newer than build output, rebuilding..."
 96            NEEDS_BUILD=true
 97        fi
 98    fi
 99    
100    if [ "$NEEDS_BUILD" = true ]; then
101        echo "Building webui..."
102        
103        # Stash any unstaged changes to avoid conflicts during build
104        echo "Checking for unstaged changes..."
105        if ! git diff --quiet || ! git diff --cached --quiet --diff-filter=A; then
106            echo "Stashing unstaged changes..."
107            git stash push --include-untracked -m "Pre-push hook: stashed unstaged changes"
108            STASH_CREATED=$?
109        else
110            echo "No unstaged changes to stash"
111            STASH_CREATED=1
112        fi
113        
114        # Run the build command
115        npm run build
116        
117        # Check if build command succeeded
118        if [ $? -ne 0 ]; then
119            echo "Error: npm run build failed"
120            if [ $STASH_CREATED -eq 0 ]; then
121                echo "You can restore your unstaged changes with: git stash pop"
122            fi
123            exit 1
124        fi
125
126        # Go back to repo root
127        cd ../../..
128        
129        # Check if build output was created/updated
130        if [ -f "tools/server/public/index.html.gz" ]; then
131            # Add the build output and commit it
132            git add tools/server/public/index.html.gz
133            if ! git diff --cached --quiet; then
134                echo "Committing updated build output..."
135                git commit -m "chore: update webui build output"
136                echo "✅ Build output committed successfully"
137            else
138                echo "Build output unchanged"
139            fi
140        else
141            echo "Error: Build output not found after build"
142            if [ $STASH_CREATED -eq 0 ]; then
143                echo "You can restore your unstaged changes with: git stash pop"
144            fi
145            exit 1
146        fi
147        
148        if [ $STASH_CREATED -eq 0 ]; then
149            echo "✅ Build completed. Your unstaged changes have been stashed."
150            echo "They will be automatically restored after the push."
151            # Create a marker file to indicate stash was created by pre-push hook
152            touch .git/WEBUI_PUSH_STASH_MARKER
153        fi
154    else
155        echo "✅ Build output is up-to-date"
156    fi
157    
158    echo "✅ Webui ready for push"
159fi
160
161exit 0
162EOF
163
164# Create the post-push hook (for restoring stashed changes after push)
165cat > "$REPO_ROOT/.git/hooks/post-push" << 'EOF'
166#!/bin/bash
167
168# Check if we have a stash marker from the pre-push hook
169if [ -f .git/WEBUI_PUSH_STASH_MARKER ]; then
170    echo "Restoring your unstaged changes after push..."
171    git stash pop
172    rm -f .git/WEBUI_PUSH_STASH_MARKER
173    echo "✅ Your unstaged changes have been restored."
174fi
175
176exit 0
177EOF
178
179# Make all hooks executable
180chmod +x "$PRE_COMMIT_HOOK"
181chmod +x "$PRE_PUSH_HOOK"
182chmod +x "$REPO_ROOT/.git/hooks/post-push"
183
184if [ $? -eq 0 ]; then
185    echo "✅ Git hooks installed successfully!"
186    echo "   Pre-commit: $PRE_COMMIT_HOOK"
187    echo "   Pre-push:   $PRE_PUSH_HOOK"
188    echo "   Post-push:  $REPO_ROOT/.git/hooks/post-push"
189    echo ""
190    echo "The hooks will automatically:"
191    echo "  • Format and check webui code before commits (pre-commit)"
192    echo "  • Build webui code before pushes (pre-push)"
193    echo "  • Stash unstaged changes during build process"
194    echo "  • Restore your unstaged changes after the push"
195    echo ""
196    echo "To test the hooks:"
197    echo "  • Make a change to a file in the webui directory and commit it (triggers format/check)"
198    echo "  • Push your commits to trigger the build process"
199else
200    echo "❌ Failed to make hooks executable"
201    exit 1
202fi