-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·264 lines (225 loc) · 9.61 KB
/
setup.sh
File metadata and controls
executable file
·264 lines (225 loc) · 9.61 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
#!/usr/bin/env bash
set -euo pipefail
# ─────────────────────────────────────────────────────────────
# setup.sh — Interactive project bootstrapper
# Prompts for project metadata, updates configs, installs deps,
# sets up environment files, and initialises a fresh git repo.
# ─────────────────────────────────────────────────────────────
# ── Colours & helpers ────────────────────────────────────────
BOLD="\033[1m"
GREEN="\033[0;32m"
CYAN="\033[0;36m"
YELLOW="\033[0;33m"
RED="\033[0;31m"
RESET="\033[0m"
info() { echo -e "${CYAN}ℹ ${RESET} $*"; }
success() { echo -e "${GREEN}✔ ${RESET} $*"; }
warn() { echo -e "${YELLOW}⚠ ${RESET} $*"; }
error() { echo -e "${RED}✖ ${RESET} $*"; exit 1; }
divider() { echo -e "\n${BOLD}──────────────────────────────────────────${RESET}\n"; }
# ── Prerequisite checks ─────────────────────────────────────
check_prerequisites() {
local missing=0
if ! command -v node &>/dev/null; then
warn "Node.js is not installed (required >= 22)"
missing=1
else
local node_major
node_major=$(node -v | sed 's/v//' | cut -d. -f1)
if (( node_major < 22 )); then
warn "Node.js version $(node -v) detected — v22+ recommended"
else
success "Node.js $(node -v)"
fi
fi
if ! command -v npm &>/dev/null; then
warn "npm is not installed"
missing=1
else
success "npm $(npm -v)"
fi
if ! command -v git &>/dev/null; then
warn "git is not installed"
missing=1
else
success "git $(git --version | awk '{print $3}')"
fi
if (( missing )); then
error "Please install the missing prerequisites and re-run this script."
fi
}
# ── Prompt helper (with default) ────────────────────────────
prompt() {
local var_name="$1" prompt_text="$2" default="${3:-}"
local input
if [[ -n "$default" ]]; then
echo -en "${BOLD}${prompt_text}${RESET} [${default}]: "
else
echo -en "${BOLD}${prompt_text}${RESET}: "
fi
read -r input
eval "$var_name=\"${input:-$default}\""
}
# ── Confirm helper ──────────────────────────────────────────
confirm() {
local prompt_text="$1"
local answer
echo -en "${BOLD}${prompt_text}${RESET} (y/N): "
read -r answer
[[ "$answer" =~ ^[Yy]$ ]]
}
# ── JSON field updater (portable — no jq required) ──────────
# Uses node to update a field in a JSON file.
update_json_field() {
local file="$1" field="$2" value="$3"
node -e "
const fs = require('fs');
const pkg = JSON.parse(fs.readFileSync('$file', 'utf8'));
pkg['$field'] = '$value';
fs.writeFileSync('$file', JSON.stringify(pkg, null, 2) + '\n');
"
}
# ── Main ────────────────────────────────────────────────────
main() {
echo ""
echo -e "${BOLD}${CYAN} Postgres + Node + React — Project Setup${RESET}"
echo -e " ────────────────────────────────────────"
echo ""
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Step 1 — Prerequisites
divider
info "Checking prerequisites …"
check_prerequisites
# Step 2 — Collect project metadata
divider
info "Tell me about your project:"
echo ""
prompt PROJECT_NAME "Project name" "my-fullstack-app"
prompt PROJECT_DESC "Description" "A full-stack app with React, Express, Prisma & PostgreSQL"
prompt AUTHOR_NAME "Author" ""
prompt PROJECT_VER "Version" "1.0.0"
prompt LICENSE "License" "ISC"
# Database setup
divider
info "Database configuration (used for backend/.env):"
echo ""
prompt DB_USER "PostgreSQL username" "postgres"
prompt DB_PASS "PostgreSQL password" "postgres"
prompt DB_HOST "PostgreSQL host" "localhost"
prompt DB_PORT "PostgreSQL port" "5432"
prompt DB_NAME "Database name" "$PROJECT_NAME"
DATABASE_URL="postgresql://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}?schema=public"
# Summary
divider
echo -e "${BOLD}Summary${RESET}"
echo ""
echo " Project name : $PROJECT_NAME"
echo " Description : $PROJECT_DESC"
echo " Author : ${AUTHOR_NAME:-<none>}"
echo " Version : $PROJECT_VER"
echo " License : $LICENSE"
echo " Database URL : postgresql://${DB_USER}:****@${DB_HOST}:${DB_PORT}/${DB_NAME}?schema=public"
echo ""
if ! confirm "Proceed with setup?"; then
warn "Setup cancelled."
exit 0
fi
# ── Step 3 — Update package.json files ────────────────────
divider
info "Updating backend/package.json …"
update_json_field "backend/package.json" "name" "${PROJECT_NAME}-backend"
update_json_field "backend/package.json" "version" "$PROJECT_VER"
update_json_field "backend/package.json" "description" "$PROJECT_DESC"
update_json_field "backend/package.json" "license" "$LICENSE"
[[ -n "$AUTHOR_NAME" ]] && update_json_field "backend/package.json" "author" "$AUTHOR_NAME"
success "backend/package.json updated"
info "Updating frontend/package.json …"
update_json_field "frontend/package.json" "name" "${PROJECT_NAME}-frontend"
update_json_field "frontend/package.json" "version" "$PROJECT_VER"
[[ -n "$AUTHOR_NAME" ]] && update_json_field "frontend/package.json" "author" "$AUTHOR_NAME"
success "frontend/package.json updated"
info "Updating root package.json …"
update_json_field "package.json" "name" "$PROJECT_NAME"
update_json_field "package.json" "version" "$PROJECT_VER"
update_json_field "package.json" "description" "$PROJECT_DESC"
[[ -n "$AUTHOR_NAME" ]] && update_json_field "package.json" "author" "$AUTHOR_NAME"
success "root package.json updated"
# ── Step 4 — Update VERSION file ──────────────────────────
echo "$PROJECT_VER" > VERSION
success "VERSION file updated to $PROJECT_VER"
# ── Step 5 — Update App.tsx title ─────────────────────────
if [[ -f "frontend/src/App.tsx" ]]; then
sed -i '' "s|Prisma-node-typescript-react-tailwind-vite template|${PROJECT_NAME}|g" frontend/src/App.tsx 2>/dev/null \
|| sed -i "s|Prisma-node-typescript-react-tailwind-vite template|${PROJECT_NAME}|g" frontend/src/App.tsx 2>/dev/null \
|| true
success "frontend/src/App.tsx title updated"
fi
# ── Step 6 — Create environment files ─────────────────────
divider
info "Setting up environment files …"
cat > backend/.env <<EOF
# Auto-generated by setup.sh
# PostgreSQL connection string used by Prisma & pg
DATABASE_URL="${DATABASE_URL}"
EOF
success "backend/.env created"
# Create frontend .env if it doesn't exist
cat > frontend/.env <<EOF
# Auto-generated by setup.sh
# Add frontend environment variables below (prefixed with VITE_)
VITE_API_URL=http://localhost:9000
EOF
success "frontend/.env created"
# ── Step 7 — Install dependencies ─────────────────────────
divider
info "Installing all dependencies (npm workspaces) …"
npm install
success "All dependencies installed (backend + frontend)"
# ── Step 8 — Generate Prisma client ───────────────────────
divider
info "Generating Prisma client …"
(cd backend && npx prisma generate)
success "Prisma client generated"
# ── Step 9 — Initialise git repository ────────────────────
divider
if [[ -d ".git" ]]; then
if confirm "A .git directory already exists. Re-initialise git repo?"; then
rm -rf .git
git init
git add -A
git commit -m "chore: initial project setup — ${PROJECT_NAME}"
success "Git repository re-initialised with initial commit"
else
info "Keeping existing git history"
fi
else
git init
git add -A
git commit -m "chore: initial project setup — ${PROJECT_NAME}"
success "Git repository initialised with initial commit"
fi
# ── Done! ─────────────────────────────────────────────────
divider
echo -e "${BOLD}${GREEN} ✔ Setup complete!${RESET}"
echo ""
echo " Next steps:"
echo ""
echo " 1. Make sure your PostgreSQL database '${DB_NAME}' exists:"
echo " ${CYAN}createdb ${DB_NAME}${RESET}"
echo ""
echo " 2. Run the first Prisma migration:"
echo " ${CYAN}npm run prisma:migrate${RESET}"
echo ""
echo " 3. Start both backend & frontend:"
echo " ${CYAN}npm run dev${RESET}"
echo ""
echo " Or start them individually:"
echo " ${CYAN}npm run dev:backend${RESET}"
echo " ${CYAN}npm run dev:frontend${RESET}"
echo ""
echo " Backend → http://localhost:9000"
echo " Frontend → http://localhost:5173"
echo ""
}
main "$@"