-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·383 lines (357 loc) · 10.7 KB
/
run.sh
File metadata and controls
executable file
·383 lines (357 loc) · 10.7 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/bin/bash
# 🚀 MasterFabric Website - Next.js Development and Deploy Script
# This script sets up, tests, and deploys the Next.js project in the root directory
set -e # Exit on error
# 🎨 Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 🎯 Logo and header
echo -e "${BLUE}"
echo "╔═══════════════════════════════════════════════════════════╗"
echo "║ 🚀 MasterFabric Website ║"
echo "║ 💻 Next.js Development & Deploy Script ║"
echo "╚═══════════════════════════════════════════════════════════╝"
echo -e "${NC}"
# 📢 Functions
print_step() {
echo -e "${BLUE}[🔄 STEP]${NC} $1"
}
print_success() {
echo -e "${GREEN}[✅ SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[⚠️ WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[❌ ERROR]${NC} $1"
}
# 📁 Check Next.js directory
check_directory() {
if [[ ! -f "package.json" ]]; then
print_error "package.json not found. Make sure you're in the project root."
exit 1
fi
print_success "Next.js project directory verified."
}
# 🟢 Check Node.js and npm versions
check_node() {
print_step "Checking Node.js version..."
if ! command -v node &> /dev/null; then
print_error "Node.js is not installed. Please install Node.js 18+."
exit 1
fi
NODE_VERSION=$(node --version | cut -d'v' -f2 | cut -d'.' -f1)
if [[ $NODE_VERSION -lt 18 ]]; then
print_error "Node.js 18+ required. Current version: $(node --version)"
exit 1
fi
print_success "Node.js version is compatible: $(node --version)"
}
# 📦 Detect package manager
detect_package_manager() {
if [[ -f "pnpm-lock.yaml" ]]; then
PKG_MANAGER="pnpm"
print_success "Package manager: pnpm"
elif [[ -f "yarn.lock" ]]; then
PKG_MANAGER="yarn"
print_success "Package manager: yarn"
else
PKG_MANAGER="npm"
print_success "Package manager: npm"
fi
}
# 📥 Install dependencies
install_dependencies() {
print_step "Installing dependencies..."
case $PKG_MANAGER in
"pnpm")
if ! command -v pnpm &> /dev/null; then
print_warning "pnpm not installed. Installing via npm..."
npm install -g pnpm
fi
pnpm install
;;
"yarn")
if ! command -v yarn &> /dev/null; then
print_warning "yarn not installed. Installing via npm..."
npm install -g yarn
fi
yarn install
;;
*)
npm install
;;
esac
print_success "Dependencies successfully installed."
}
# ▲ Check and install Vercel CLI
setup_vercel() {
print_step "Checking Vercel CLI..."
if ! command -v vercel &> /dev/null; then
print_warning "Vercel CLI not installed. Installing..."
npm install -g vercel@latest
print_success "Vercel CLI installed"
else
print_success "Vercel CLI already installed"
fi
}
# 🔥 Check and install Firebase CLI
setup_firebase() {
print_step "Checking Firebase CLI..."
if ! command -v firebase &> /dev/null; then
print_warning "Firebase CLI not installed. Installing..."
npm install -g firebase-tools
print_success "Firebase CLI installed"
else
print_success "Firebase CLI already installed"
fi
}
# 🔨 Build project
build_project() {
print_step "Building Next.js project..."
case $PKG_MANAGER in
"pnpm")
pnpm run build
;;
"yarn")
yarn build
;;
*)
npm run build
;;
esac
print_success "Build completed successfully."
}
# 🧪 Setup test environment
setup_test_environment() {
print_step "Setting up test environment..."
# Create .env.test file (if not exists)
if [[ ! -f ".env.test" ]]; then
cat > .env.test << EOF
# Test Environment Variables
NODE_ENV=test
NEXT_PUBLIC_BASE_URL=http://localhost:3000
EOF
print_success ".env.test file created"
fi
# Check test dependencies
print_step "Checking test dependencies..."
case $PKG_MANAGER in
"pnpm")
if ! pnpm list jest &> /dev/null; then
print_warning "Test framework not found. Installing Jest..."
pnpm add -D jest @testing-library/react @testing-library/jest-dom
fi
;;
"yarn")
if ! yarn list jest &> /dev/null; then
print_warning "Test framework not found. Installing Jest..."
yarn add -D jest @testing-library/react @testing-library/jest-dom
fi
;;
*)
if ! npm list jest &> /dev/null; then
print_warning "Test framework not found. Installing Jest..."
npm install -D jest @testing-library/react @testing-library/jest-dom
fi
;;
esac
print_success "Test environment ready."
}
# 🌐 Start localhost server
start_localhost() {
print_step "Starting Next.js development server..."
case $PKG_MANAGER in
"pnpm")
echo -e "${GREEN}🚀 Starting Next.js dev server... http://localhost:3000${NC}"
pnpm run dev
;;
"yarn")
echo -e "${GREEN}🚀 Starting Next.js dev server... http://localhost:3000${NC}"
yarn dev
;;
*)
echo -e "${GREEN}🚀 Starting Next.js dev server... http://localhost:3000${NC}"
npm run dev
;;
esac
}
# 👀 Start preview server
start_preview() {
print_step "Starting Next.js preview server..."
build_project
case $PKG_MANAGER in
"pnpm")
echo -e "${GREEN}🔍 Starting Next.js preview server... http://localhost:3000${NC}"
pnpm run start
;;
"yarn")
echo -e "${GREEN}🔍 Starting Next.js preview server... http://localhost:3000${NC}"
yarn start
;;
*)
echo -e "${GREEN}🔍 Starting Next.js preview server... http://localhost:3000${NC}"
npm run start
;;
esac
}
# ▲ Vercel deploy
deploy_vercel() {
print_step "Deploying Next.js app to Vercel..."
# Create vercel.json if not exists
if [[ ! -f "vercel.json" ]]; then
cat > vercel.json << EOF
{
"buildCommand": "npm run build",
"outputDirectory": ".next",
"framework": "nextjs",
"devCommand": "npm run dev"
}
EOF
print_success "vercel.json file created (Next.js)"
fi
# Build first
build_project
# Deploy
if [[ "$1" == "--prod" ]]; then
vercel --prod
print_success "Deployed to production (Vercel)"
else
vercel
print_success "Deployed to preview (Vercel)"
fi
}
# 🔥 Firebase deploy
deploy_firebase() {
print_step "Deploying Next.js app to Firebase..."
# Create firebase.json if not exists
if [[ ! -f "firebase.json" ]]; then
cat > firebase.json << EOF
{
"hosting": {
"public": "out",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
EOF
print_success "firebase.json file created (Next.js)"
fi
# Build static export
case $PKG_MANAGER in
"pnpm")
pnpm run build && pnpm run export
;;
"yarn")
yarn build && yarn export
;;
*)
npm run build && npm run export
;;
esac
# Firebase login check
if ! firebase projects:list &> /dev/null; then
print_warning "Not logged in to Firebase. Please log in:"
firebase login
fi
# Deploy
firebase deploy
print_success "Deployed to Firebase (Next.js)"
}
# 📚 Help message
show_help() {
echo -e "${BLUE}Usage:${NC}"
echo " ./run.sh [COMMAND] [OPTIONS]"
echo ""
echo -e "${BLUE}Commands:${NC}"
echo " setup - 🛠️ Complete setup (dependencies, CLI tools) for Next.js"
echo " dev - 🌐 Start Next.js development server"
echo " build - 🔨 Build the Next.js project"
echo " preview - 👀 Build and start Next.js preview server"
echo " test - 🧪 Setup test environment (Jest)"
echo " deploy-vercel - ▲ Deploy Next.js app to Vercel"
echo " deploy-firebase - 🔥 Deploy Next.js app to Firebase (static export)"
echo " deploy-all - 🚀 Deploy to both Vercel and Firebase"
echo " help - 📚 Show this help message"
echo ""
echo -e "${BLUE}Options:${NC}"
echo " --prod - 🎯 Production deploy (Vercel only)"
echo ""
echo -e "${BLUE}Examples:${NC}"
echo " ./run.sh setup # Complete setup for Next.js"
echo " ./run.sh dev # Start Next.js development"
echo " ./run.sh deploy-vercel --prod # Deploy to production (Vercel)"
echo " ./run.sh deploy-all # Deploy everywhere"
}
# 🎯 Main function
main() {
check_directory
detect_package_manager
case "${1:-setup}" in
"setup")
print_step "Starting comprehensive setup for Next.js..."
check_node
install_dependencies
setup_vercel
setup_firebase
setup_test_environment
build_project
print_success "Setup completed! You can start with './run.sh dev'."
;;
"dev")
check_node
start_localhost
;;
"build")
check_node
build_project
;;
"preview")
check_node
start_preview
;;
"test")
check_node
setup_test_environment
;;
"deploy-vercel")
check_node
setup_vercel
deploy_vercel $2
;;
"deploy-firebase")
check_node
setup_firebase
deploy_firebase
;;
"deploy-all")
check_node
setup_vercel
setup_firebase
deploy_vercel
deploy_firebase
;;
"help")
show_help
;;
*)
print_error "Unknown command: $1"
show_help
exit 1
;;
esac
}
# 🚀 Execute the script
main "$@"