-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·312 lines (276 loc) · 10.2 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·312 lines (276 loc) · 10.2 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/bash
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
echo -e "${BLUE}🚀 Setting up Catnip development environment...${NC}"
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to install missing tools
install_instructions() {
echo -e "${RED}❌ $1 is not installed${NC}"
echo -e "${YELLOW}📋 Installation instructions:${NC}"
case $1 in
"pnpm")
echo " • Install via npm: npm install -g pnpm"
echo " • Install via corepack: corepack enable && corepack prepare pnpm@latest --activate"
echo " • Install via curl: curl -fsSL https://get.pnpm.io/install.sh | sh -"
echo " • Install via Homebrew: brew install pnpm"
echo " • More options: https://pnpm.io/installation"
;;
"just")
echo " • Install via npm: npm install -g just-install"
echo " • Install via cargo: cargo install just"
echo " • Install via Homebrew: brew install just"
echo " • Install via package manager: https://github.com/casey/just#installation"
;;
"go")
echo " • Download from: https://golang.org/dl/"
echo " • Install via Homebrew: brew install go"
;;
"node")
echo " • Download from: https://nodejs.org/"
echo " • Install via Homebrew: brew install node"
echo " • Install via nvm: https://github.com/nvm-sh/nvm"
;;
esac
echo ""
}
# Function to attempt automatic installation
try_auto_install() {
local tool=$1
echo -e "${YELLOW}🔧 Attempting to auto-install $tool...${NC}"
case $tool in
"pnpm")
# Try corepack first (available in Node.js 16.13+)
if command_exists corepack; then
echo "Enabling pnpm via corepack..."
if corepack enable && corepack prepare pnpm@latest --activate; then
echo -e "${GREEN}✅ pnpm installed via corepack${NC}"
return 0
fi
fi
# Fall back to npm
if command_exists npm; then
echo "Installing pnpm via npm..."
if npm install -g pnpm; then
echo -e "${GREEN}✅ pnpm installed via npm${NC}"
return 0
fi
fi
;;
"just")
# Try npm installation
if command_exists npm; then
echo "Installing just via npm..."
if npm install -g just-install && command_exists just; then
echo -e "${GREEN}✅ just installed via npm${NC}"
return 0
fi
fi
;;
esac
return 1
}
# Check required dependencies
missing_deps=()
echo -e "${YELLOW}🔍 Checking dependencies...${NC}"
if ! command_exists node; then
install_instructions "node"
missing_deps+=("node")
fi
if ! command_exists pnpm; then
if ! try_auto_install "pnpm"; then
install_instructions "pnpm"
missing_deps+=("pnpm")
fi
fi
if ! command_exists go; then
install_instructions "go"
missing_deps+=("go")
fi
if ! command_exists just; then
if ! try_auto_install "just"; then
install_instructions "just"
missing_deps+=("just")
fi
fi
# Exit if dependencies are missing
if [ ${#missing_deps[@]} -ne 0 ]; then
echo -e "${RED}❌ Missing dependencies: ${missing_deps[*]}${NC}"
echo -e "${YELLOW}Please install the missing dependencies and run this script again.${NC}"
exit 1
fi
echo -e "${GREEN}✅ All dependencies are installed${NC}"
# Install pnpm packages
echo -e "${YELLOW}📦 Installing pnpm packages...${NC}"
if pnpm install --frozen-lockfile; then
echo -e "${GREEN}✅ pnpm packages installed${NC}"
else
echo -e "${RED}❌ Failed to install pnpm packages${NC}"
exit 1
fi
# Install Go dependencies
echo -e "${YELLOW}📦 Installing Go dependencies...${NC}"
cd container
if just deps; then
echo -e "${GREEN}✅ Go dependencies installed${NC}"
else
echo -e "${RED}❌ Failed to install Go dependencies${NC}"
exit 1
fi
cd ..
# Install pre-commit hook
echo -e "${YELLOW}🪝 Installing pre-commit hook...${NC}"
if [ -f ".git/hooks/pre-commit" ]; then
echo -e "${YELLOW}⚠️ Pre-commit hook already exists. Backing up...${NC}"
mv .git/hooks/pre-commit .git/hooks/pre-commit.backup
fi
# Create the pre-commit hook
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/bash
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${YELLOW}🎨 Running pre-commit formatters...${NC}"
# Track files that were formatted
formatted_files_list=""
# Get list of staged TypeScript/JavaScript files before formatting
staged_ts_files=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(ts|tsx|js|jsx)$' || true)
# Format TypeScript/JavaScript files
echo "Formatting TypeScript/JavaScript files..."
if pnpm format:changed 2>/dev/null; then
# Check if any of the staged files have unstaged changes after formatting
if [ -n "$staged_ts_files" ]; then
formatted_ts_files=""
for file in $staged_ts_files; do
if git diff --name-only | grep -q "^$file$"; then
formatted_ts_files="$formatted_ts_files $file"
fi
done
if [ -n "$formatted_ts_files" ]; then
formatted_files_list="$formatted_files_list$formatted_ts_files"
echo -e "${GREEN}✅ TypeScript/JavaScript files formatted${NC}"
else
echo -e "${GREEN}✅ No TypeScript/JavaScript files needed formatting${NC}"
fi
else
echo -e "${GREEN}✅ No TypeScript/JavaScript files to format${NC}"
fi
else
echo -e "${RED}❌ Failed to format TypeScript/JavaScript files${NC}"
echo -e "${RED}💥 Commit blocked - fix formatting issues and try again${NC}"
exit 1
fi
# Check if there are any changes in the src directory
src_changes=$(git diff --cached --name-only --diff-filter=ACM | grep '^src/' || true)
if [ -n "$src_changes" ]; then
# Run TypeScript lint checks
echo "Running TypeScript lint checks..."
if pnpm lint >/dev/null 2>&1; then
echo -e "${GREEN}✅ TypeScript lint checks passed${NC}"
else
echo -e "${RED}❌ TypeScript lint checks failed${NC}"
echo -e "${YELLOW}Run 'pnpm lint' to see details${NC}"
echo -e "${RED}💥 Commit blocked - fix lint issues and try again${NC}"
exit 1
fi
# Run TypeScript type checks
echo "Running TypeScript type checks..."
if pnpm typecheck >/dev/null 2>&1; then
echo -e "${GREEN}✅ TypeScript type checks passed${NC}"
else
echo -e "${RED}❌ TypeScript type checks failed${NC}"
echo -e "${YELLOW}Run 'pnpm typecheck' to see details${NC}"
echo -e "${RED}💥 Commit blocked - fix type errors and try again${NC}"
exit 1
fi
else
echo -e "${GREEN}✅ No src files changed, skipping TypeScript lint and type checks${NC}"
fi
# Check if there are any changes in the container directory
container_changes=$(git diff --cached --name-only --diff-filter=ACM | grep '^container/' || true)
if [ -n "$container_changes" ]; then
# Get list of staged Go files before formatting
staged_go_files=$(echo "$container_changes" | grep '\.go$' || true)
# Format Go files
echo "Formatting Go files..."
cd container
if just format-go-changed 2>/dev/null; then
# Check if any of the staged Go files have unstaged changes after formatting
if [ -n "$staged_go_files" ]; then
formatted_go_files=""
for file in $staged_go_files; do
# Remove container/ prefix for checking
file_without_prefix=${file#container/}
if git diff --name-only | grep -q "^$file_without_prefix$"; then
formatted_go_files="$formatted_go_files $file"
fi
done
if [ -n "$formatted_go_files" ]; then
formatted_files_list="$formatted_files_list$formatted_go_files"
echo -e "${GREEN}✅ Go files formatted${NC}"
else
echo -e "${GREEN}✅ No Go files needed formatting${NC}"
fi
else
echo -e "${GREEN}✅ No Go files to format${NC}"
fi
else
echo -e "${RED}❌ Failed to format Go files${NC}"
echo -e "${RED}💥 Commit blocked - fix Go formatting issues and try again${NC}"
exit 1
fi
# Run Go lint checks
echo "Running Go lint checks..."
if just lint >/dev/null 2>&1; then
echo -e "${GREEN}✅ Go lint checks passed${NC}"
else
echo -e "${RED}❌ Go lint checks failed${NC}"
echo -e "${YELLOW}Run 'cd container && just lint' to see details${NC}"
echo -e "${RED}💥 Commit blocked - fix Go lint issues and try again${NC}"
exit 1
fi
cd ..
else
echo -e "${GREEN}✅ No Go files changed, skipping Go formatting and lint checks${NC}"
fi
# If files were formatted, auto-stage them
if [ -n "$formatted_files_list" ]; then
echo -e "${YELLOW}📝 Auto-staging formatted files:${formatted_files_list}${NC}"
git add $formatted_files_list
echo -e "${GREEN}✅ Formatted files staged automatically${NC}"
else
echo -e "${GREEN}✅ All files already formatted${NC}"
fi
echo -e "${GREEN}🎉 Pre-commit hook completed successfully${NC}"
EOF
# Make the hook executable
chmod +x .git/hooks/pre-commit
echo -e "${GREEN}✅ Pre-commit hook installed${NC}"
# Build initial setup
echo -e "${YELLOW}🏗️ Building initial setup...${NC}"
cd container
if just build; then
echo -e "${GREEN}✅ Go server built successfully${NC}"
else
echo -e "${RED}❌ Failed to build Go server${NC}"
exit 1
fi
cd ..
echo -e "${GREEN}🎉 Catnip development environment is ready!${NC}"
echo -e "${BLUE}📚 Quick start:${NC}"
echo " • Run frontend dev server: pnpm dev"
echo " • Run with Cloudflare: pnpm dev:cf"
echo " • Build Go server: cd container && just build"
echo " • Run tests: cd container && just test"
echo ""
echo -e "${YELLOW}💡 The pre-commit hook will automatically format changed files on commit${NC}"