File size: 4,484 Bytes
6a7089a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
# Build React dashboard and copy to internal/dashboard/dashboard/
set -e

cd "$(dirname "$0")/.."

BUN_MIN_VERSION="1.2.0"

confirm() {
  if [ ! -t 0 ]; then
    return 1
  fi

  echo -n "$1 [y/N] "
  read -r answer
  [[ "$answer" =~ ^[Yy]$ ]]
}

version_ge() {
  local current="${1#v}"
  local minimum="${2#v}"
  local current_major current_minor current_patch
  local minimum_major minimum_minor minimum_patch

  IFS='.' read -r current_major current_minor current_patch <<< "${current}"
  IFS='.' read -r minimum_major minimum_minor minimum_patch <<< "${minimum}"

  current_minor=${current_minor:-0}
  current_patch=${current_patch:-0}
  minimum_minor=${minimum_minor:-0}
  minimum_patch=${minimum_patch:-0}

  if [ "$current_major" -ne "$minimum_major" ]; then
    [ "$current_major" -gt "$minimum_major" ]
    return
  fi

  if [ "$current_minor" -ne "$minimum_minor" ]; then
    [ "$current_minor" -gt "$minimum_minor" ]
    return
  fi

  [ "$current_patch" -ge "$minimum_patch" ]
}

if ! command -v bun &> /dev/null; then
  echo "❌ ERROR: Bun is required to build the dashboard"
  echo "   Install it with: curl -fsSL https://bun.sh/install | bash"
  exit 1
fi

BUN_VERSION="$(bun -v)"
if ! version_ge "$BUN_VERSION" "$BUN_MIN_VERSION"; then
  echo "❌ ERROR: Bun $BUN_VERSION is too old for dashboard/bun.lock"
  echo "   Clean installs require Bun $BUN_MIN_VERSION+"
  echo "   Upgrade with: curl -fsSL https://bun.sh/install | bash"
  exit 1
fi

echo "πŸ“¦ Building React dashboard..."
cd dashboard

# Sync deps on every build so stale dashboard node_modules do not drift from bun.lock.
echo "πŸ“₯ Syncing dashboard dependencies..."
bun install --frozen-lockfile

# Generate TypeScript types from Go structs (ensures types are in sync)
TYGO="${GOPATH:-$HOME/go}/bin/tygo"
if [ -x "$TYGO" ]; then
  echo "πŸ”„ Generating TypeScript types..."
  "$TYGO" generate
elif command -v tygo &> /dev/null; then
  echo "πŸ”„ Generating TypeScript types..."
  tygo generate
else
  if command -v go &> /dev/null && confirm "Install tygo via go install?"; then
    go install github.com/gzuidhof/tygo@latest
    TYGO="${GOPATH:-$HOME/go}/bin/tygo"
    if [ -x "$TYGO" ]; then
      echo "πŸ”„ Generating TypeScript types..."
      "$TYGO" generate
    elif command -v tygo &> /dev/null; then
      echo "πŸ”„ Generating TypeScript types..."
      tygo generate
    else
      echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
      echo "⚠️  WARNING: tygo install completed but binary was not found on PATH"
      echo "   Restart your shell or ensure \$GOPATH/bin is on PATH."
      echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    fi
  else
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "⚠️  WARNING: tygo not found β€” skipping TypeScript type generation"
    echo "   Types in the dashboard might fall out of sync with Go structs."
    echo "   Install it with: go install github.com/gzuidhof/tygo@latest"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  fi
fi

# Normalize tygo output with prettier so generation doesn't dirty git
if [ -f "src/generated/types.ts" ]; then
  npx prettier --write src/generated/types.ts 2>/dev/null || true
fi

bun run build

echo "πŸ“‹ Copying build to internal/dashboard/dashboard/..."
cd ..

# Clear old dashboard assets (keep favicon.png)
rm -rf internal/dashboard/dashboard/assets/
rm -f internal/dashboard/dashboard/dashboard.html

# Copy React build and rename entry HTML.
# Vite always outputs index.html; Go's embed serves it as dashboard.html to
# avoid http.FileServer's automatic index.html handling at /dashboard/.
cp -r dashboard/dist/* internal/dashboard/dashboard/
mv internal/dashboard/dashboard/index.html internal/dashboard/dashboard/dashboard.html

echo "βœ… Dashboard built: internal/dashboard/dashboard/"