File size: 2,945 Bytes
25b930c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env bash
set -euo pipefail

# Pre-commit hook: Go + Dashboard checks

echo "πŸ” Running pre-commit checks..."

# ============================================
# Go checks (gofmt + golangci-lint)
# ============================================
staged_go=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$' || true)
if [ -n "$staged_go" ]; then
  echo "πŸ“¦ Checking Go files..."
  
  # gofmt check
  unformatted=$(gofmt -l $staged_go 2>/dev/null || true)
  if [ -n "$unformatted" ]; then
    echo "❌ gofmt: files not formatted:"
    echo "$unformatted"
    echo "Run: gofmt -w $unformatted"
    exit 1
  fi

  # golangci-lint (if available)
  GOLANGCI_LINT=""
  if command -v golangci-lint &>/dev/null; then
    GOLANGCI_LINT="golangci-lint"
  elif [ -x "${GOPATH:-$HOME/go}/bin/golangci-lint" ]; then
    GOLANGCI_LINT="${GOPATH:-$HOME/go}/bin/golangci-lint"
  elif [ -x "$HOME/go/bin/golangci-lint" ]; then
    GOLANGCI_LINT="$HOME/go/bin/golangci-lint"
  fi

  if [ -n "$GOLANGCI_LINT" ]; then
    dirs=$(echo "$staged_go" | xargs -n1 dirname | sort -u | sed 's|$|/...|')
    if ! $GOLANGCI_LINT run --timeout=5m $dirs; then
      echo "❌ golangci-lint: issues found"
      exit 1
    fi
  else
    echo "⚠️  golangci-lint not found, skipping Go lint"
    echo "   Install: go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest"
  fi
  
  echo "βœ… Go checks passed"
fi

# ============================================
# Dashboard checks (TypeScript + ESLint + Prettier)
# ============================================
staged_web=$(git diff --cached --name-only --diff-filter=ACM | grep '^dashboard/.*\.\(ts\|tsx\|js\|jsx\|css\)$' || true)
if [ -n "$staged_web" ]; then
  echo "🌐 Checking dashboard files..."
  
  cd dashboard
  
  # Check if bun is available
  if ! command -v bun &>/dev/null; then
    echo "⚠️  bun not found, skipping dashboard checks"
    cd ..
  else
    # TypeScript check
    echo "  β†’ TypeScript..."
    if ! bun run tsc --noEmit 2>/dev/null; then
      echo "❌ TypeScript: type errors found"
      cd ..
      exit 1
    fi
    
    # ESLint check
    echo "  β†’ ESLint..."
    if ! bun run lint 2>/dev/null; then
      echo "❌ ESLint: issues found"
      cd ..
      exit 1
    fi
    
    # Prettier check (on staged files only)
    echo "  β†’ Prettier..."
    staged_rel=$(echo "$staged_web" | sed 's|^dashboard/||')
    if ! echo "$staged_rel" | xargs bunx prettier --check 2>/dev/null; then
      echo "❌ Prettier: files not formatted"
      echo "Run: cd dashboard && bun run format"
      cd ..
      exit 1
    fi
    
    cd ..
    echo "βœ… Dashboard checks passed"
  fi
fi

# ============================================
# Summary
# ============================================
if [ -z "$staged_go" ] && [ -z "$staged_web" ]; then
  echo "⏭️  No Go or dashboard files staged, skipping checks"
fi

echo "βœ… All pre-commit checks passed"