-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·290 lines (249 loc) · 9.74 KB
/
setup.sh
File metadata and controls
executable file
·290 lines (249 loc) · 9.74 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
#!/bin/bash
# ============================================
# Payload CMS Client Project Setup Script
# ============================================
# Automates the setup of a new client project
# Creates isolated PostgreSQL database per project
# ============================================
set -e # Exit on error
# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${BLUE}============================================${NC}"
echo -e "${BLUE} Payload CMS Client Project Setup${NC}"
echo -e "${BLUE}============================================${NC}"
echo ""
# Clean up any existing Docker containers and volumes from previous runs
echo -e "${BLUE}Cleaning up Docker resources...${NC}"
docker compose down -v 2>/dev/null || true
# Also try to remove any orphaned volumes
if [ -f ../.project-name ]; then
PROJECT_NAME_TEMP=$(cat ../.project-name | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
VOLUME_NAME="${PROJECT_NAME_TEMP}_postgres_data"
docker volume rm "$VOLUME_NAME" 2>/dev/null || true
fi
echo -e "${GREEN}✓${NC} Docker cleanup complete"
echo ""
# Detect project name from parent directory or .project-name file
if [ -f ../.project-name ]; then
PROJECT_NAME=$(cat ../.project-name | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
echo -e "${GREEN}✓${NC} Using project name from .project-name file: ${YELLOW}${PROJECT_NAME}${NC}"
else
PARENT_DIR=$(basename "$(dirname "$(pwd)")")
PROJECT_NAME=$(echo "$PARENT_DIR" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
echo -e "${BLUE}Detected project name: ${YELLOW}${PROJECT_NAME}${NC}"
echo ""
# Ask user to confirm or change project name
read -p "Press Enter to use this name, or type a different name: " USER_INPUT
if [ ! -z "$USER_INPUT" ]; then
PROJECT_NAME=$(echo "$USER_INPUT" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
fi
echo -e "${GREEN}✓${NC} Using project name: ${YELLOW}${PROJECT_NAME}${NC}"
fi
echo ""
# Create project-specific environment variables
POSTGRES_CONTAINER="${PROJECT_NAME}-postgres"
POSTGRES_DB="${PROJECT_NAME}_db"
POSTGRES_PORT_BASE=5432
POSTGRES_PORT=$POSTGRES_PORT_BASE
CMS_PORT_BASE=3000
CMS_PORT=$CMS_PORT_BASE
# Find available database port if 5432 is taken
echo -e "${BLUE}Checking for available ports...${NC}"
while lsof -Pi :$POSTGRES_PORT -sTCP:LISTEN -t >/dev/null 2>&1 || nc -z localhost $POSTGRES_PORT >/dev/null 2>&1 ; do
POSTGRES_PORT=$((POSTGRES_PORT + 1))
done
if [ $POSTGRES_PORT -ne $POSTGRES_PORT_BASE ]; then
echo -e "${YELLOW}⚠${NC} Database port 5432 is in use, using port ${POSTGRES_PORT} instead"
else
echo -e "${GREEN}✓${NC} Database port: ${YELLOW}${POSTGRES_PORT}${NC}"
fi
# Find available CMS port if 3000 is taken
while lsof -Pi :$CMS_PORT -sTCP:LISTEN -t >/dev/null 2>&1 || nc -z localhost $CMS_PORT >/dev/null 2>&1 ; do
CMS_PORT=$((CMS_PORT + 1))
done
if [ $CMS_PORT -ne $CMS_PORT_BASE ]; then
echo -e "${YELLOW}⚠${NC} CMS port 3000 is in use, using port ${CMS_PORT} instead"
else
echo -e "${GREEN}✓${NC} CMS port: ${YELLOW}${CMS_PORT}${NC}"
fi
echo ""
# Step 1: Install dependencies
echo ""
echo -e "${BLUE}Step 1: Installing dependencies...${NC}"
if pnpm install; then
echo -e "${GREEN}✓${NC} Dependencies installed"
else
echo -e "${RED}✗${NC} Failed to install dependencies"
exit 1
fi
# Step 2: Generate secrets
echo ""
echo -e "${BLUE}Step 2: Generating secure secrets...${NC}"
PAYLOAD_SECRET=$(openssl rand -hex 32)
REVALIDATION_SECRET=$(openssl rand -hex 32)
PREVIEW_SECRET=$(openssl rand -hex 32)
echo -e "${GREEN}✓${NC} Secrets generated"
# Step 3: Create .env file
echo ""
echo -e "${BLUE}Step 3: Creating .env file...${NC}"
cat > apps/cms/.env << ENDOFENV
# ===========================================
# Payload CMS Environment Variables
# ===========================================
# Auto-generated by setup.sh
# Project: ${PROJECT_NAME}
# Generated: $(date)
# -------------------------------------------
# Database Configuration (REQUIRED)
# -------------------------------------------
# PostgreSQL connection string
# Project-specific database: ${POSTGRES_DB}
DATABASE_URL=postgresql://payload:payload_secret@localhost:${POSTGRES_PORT}/${POSTGRES_DB}
# -------------------------------------------
# Payload CMS Configuration (REQUIRED)
# -------------------------------------------
# Secret key for JWT signing and encryption
PAYLOAD_SECRET=${PAYLOAD_SECRET}
# Public URL of the CMS server
PAYLOAD_PUBLIC_SERVER_URL=http://localhost:${CMS_PORT}
# -------------------------------------------
# Frontend Configuration (REQUIRED)
# -------------------------------------------
# Public URL of the frontend site
NEXT_PUBLIC_SITE_URL=http://localhost:${CMS_PORT}
# Internal CMS URL for server-side requests
CMS_URL=http://localhost:${CMS_PORT}
# Secret for on-demand revalidation (ISR)
REVALIDATION_SECRET=${REVALIDATION_SECRET}
# -------------------------------------------
# Live Preview Configuration
# -------------------------------------------
# Secret for secure preview token generation
PREVIEW_SECRET=${PREVIEW_SECRET}
# -------------------------------------------
# Node Environment
# -------------------------------------------
NODE_ENV=development
# -------------------------------------------
# Docker Configuration (for this project)
# -------------------------------------------
POSTGRES_USER=payload
POSTGRES_PASSWORD=payload_secret
POSTGRES_DB=${POSTGRES_DB}
POSTGRES_PORT=${POSTGRES_PORT}
COMPOSE_PROJECT_NAME=${PROJECT_NAME}
# -------------------------------------------
# CMS Server Port
# -------------------------------------------
# Auto-assigned to avoid conflicts with other running projects
CMS_PORT=${CMS_PORT}
# -------------------------------------------
# Email Configuration (Optional)
# -------------------------------------------
# Uncomment and configure for email functionality
# SMTP_HOST=smtp.example.com
# SMTP_PORT=587
# SMTP_SECURE=false
# SMTP_USER=your-smtp-username
# SMTP_PASS=your-smtp-password
# EMAIL_FROM_NAME=Payload CMS
# EMAIL_FROM_ADDRESS=noreply@example.com
ENDOFENV
echo -e "${GREEN}✓${NC} .env file created at apps/cms/.env"
echo -e "${BLUE} Database: ${YELLOW}${POSTGRES_DB}${NC}"
# Also create .env in root for docker-compose
cat > .env << ENDOFENV
POSTGRES_USER=payload
POSTGRES_PASSWORD=payload_secret
POSTGRES_DB=${POSTGRES_DB}
POSTGRES_PORT=${POSTGRES_PORT}
CMS_PORT=${CMS_PORT}
COMPOSE_PROJECT_NAME=${PROJECT_NAME}
ENDOFENV
echo -e "${GREEN}✓${NC} .env file created at .env for docker-compose"
echo -e "${BLUE} Port: ${YELLOW}${POSTGRES_PORT}${NC}"
echo -e "${BLUE} Container: ${YELLOW}${POSTGRES_CONTAINER}${NC}"
# Step 4: Start PostgreSQL with project-specific settings
echo ""
echo -e "${BLUE}Step 4: Starting PostgreSQL database...${NC}"
echo -e "${BLUE} Using project-specific container and volume${NC}"
# Export environment variables for docker-compose
export COMPOSE_PROJECT_NAME=${PROJECT_NAME}
export POSTGRES_DB=${POSTGRES_DB}
export POSTGRES_PORT=${POSTGRES_PORT}
if docker compose up -d postgres; then
echo -e "${GREEN}✓${NC} PostgreSQL started"
echo -e "${BLUE} Container: ${YELLOW}${PROJECT_NAME}-postgres-1${NC}"
echo -e "${BLUE} Volume: ${YELLOW}${PROJECT_NAME}_postgres_data${NC}"
else
echo -e "${RED}✗${NC} Failed to start PostgreSQL"
exit 1
fi
# Wait for PostgreSQL to be ready
echo -e "${BLUE}Waiting for PostgreSQL to be ready...${NC}"
sleep 5
# Step 5: Run migrations
echo ""
echo -e "${BLUE}Step 5: Running database migrations...${NC}"
if make db-migrate; then
echo -e "${GREEN}✓${NC} Database migrations completed"
else
echo -e "${RED}✗${NC} Failed to run migrations"
exit 1
fi
# Step 6: Verify package.json scripts use environment variables
echo ""
echo -e "${BLUE}Step 6: Verifying dev scripts for dynamic ports...${NC}"
# Check if CMS package.json already uses PORT variable
if [ -f "apps/cms/package.json" ]; then
if grep -q 'PORT=\${CMS_PORT' apps/cms/package.json; then
echo -e "${GREEN}✓${NC} apps/cms/package.json already configured for dynamic ports"
else
echo -e "${YELLOW}⚠${NC} apps/cms/package.json needs manual update to use PORT=\${CMS_PORT:-3000}"
echo -e "${YELLOW} This should be fixed in the repository${NC}"
fi
fi
# Check if Astro package.json exists and uses PORT variable
if [ -f "apps/astro/package.json" ]; then
if grep -q 'ASTRO_PORT' apps/astro/package.json; then
echo -e "${GREEN}✓${NC} apps/astro/package.json already configured for dynamic ports"
else
echo -e "${YELLOW}⚠${NC} apps/astro/package.json may need update for dynamic ports"
fi
fi
# Success!
echo ""
echo -e "${GREEN}============================================${NC}"
echo -e "${GREEN} ✓ Setup Complete!${NC}"
echo -e "${GREEN}============================================${NC}"
echo ""
echo -e "${BLUE}Project Configuration:${NC}"
echo -e " Name: ${YELLOW}${PROJECT_NAME}${NC}"
echo -e " Database: ${YELLOW}${POSTGRES_DB}${NC}"
echo -e " Database Port: ${YELLOW}${POSTGRES_PORT}${NC}"
echo -e " CMS Port: ${YELLOW}${CMS_PORT}${NC}"
echo -e " Container: ${YELLOW}${PROJECT_NAME}-postgres-1${NC}"
echo ""
echo -e "${BLUE}Next steps:${NC}"
echo ""
echo -e " 1. Start the development server:"
echo -e " ${YELLOW}make dev${NC}"
echo ""
echo -e " 2. Open your browser:"
echo -e " ${YELLOW}http://localhost:${CMS_PORT}/admin${NC}"
echo ""
echo -e " 3. Create your first admin user"
echo ""
echo -e "${BLUE}Your secrets (saved in apps/cms/.env):${NC}"
echo -e " PAYLOAD_SECRET: ${YELLOW}${PAYLOAD_SECRET}${NC}"
echo ""
echo -e "${YELLOW}Note: This project has its own isolated database.${NC}"
echo -e "${YELLOW}You can run multiple client projects simultaneously!${NC}"
echo -e "${YELLOW}Dev scripts automatically configured to use dynamic ports.${NC}"
echo ""
echo -e "${GREEN}Happy coding! 🚀${NC}"
echo ""