-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.init
More file actions
executable file
·309 lines (260 loc) · 9.33 KB
/
Copy pathscript.init
File metadata and controls
executable file
·309 lines (260 loc) · 9.33 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
#!/bin/bash
# =============================================================================
# Leasing API - Complete Local Development Setup Script
# =============================================================================
# This script automates the entire setup process for local development
# Run this once after cloning the repository
# =============================================================================
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Function to print colored output
print_step() {
echo -e "${BLUE}🔄 $1${NC}"
}
print_success() {
echo -e "${GREEN}✅ $1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_error() {
echo -e "${RED}❌ $1${NC}"
}
print_header() {
echo -e "${PURPLE}🚀 $1${NC}"
}
# Show header
print_header "Leasing API - Complete Local Development Setup"
echo ""
echo -e "${CYAN}This script will set up everything you need for local development:${NC}"
echo " • Install Node.js 14 (if not present)"
echo " • Install all dependencies"
echo " • Set up database with migrations"
echo " • Build the project"
echo " • Run tests"
echo " • Prepare for development"
echo ""
# Check if we're in the right directory and change to project root if needed
if [ ! -f "package.json" ]; then
print_warning "package.json not found in current directory. Looking for project root..."
# Try to find the project root by looking for package.json
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CURRENT_DIR="$(pwd)"
# Check if we're in a subdirectory of the project
if [ -f "${SCRIPT_DIR}/package.json" ]; then
print_step "Found project root at: ${SCRIPT_DIR}"
cd "${SCRIPT_DIR}"
elif [ -f "${CURRENT_DIR}/package.json" ]; then
print_step "Found project root at: ${CURRENT_DIR}"
cd "${CURRENT_DIR}"
else
print_error "Could not find project root. Please run this script from the project directory."
exit 1
fi
fi
print_step "Checking system requirements..."
# Check Node.js version and handle setup
print_step "Checking Node.js installation..."
# Check if Node.js is installed at all
if ! command -v node &> /dev/null; then
print_error "Node.js is not installed!"
echo ""
echo -e "${CYAN}📦 Node.js Installation Options:${NC}"
echo ""
echo -e "${YELLOW}Option 1 - Using Node Version Manager (nvm) - RECOMMENDED:${NC}"
echo " # Install nvm first:"
echo " curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash"
echo " # Then install Node.js 14:"
echo " nvm install 14 && nvm use 14"
echo ""
echo -e "${YELLOW}Option 2 - Direct installation:${NC}"
echo " # macOS (using Homebrew):"
echo " brew install node@14"
echo " # Or download from: https://nodejs.org/en/download/"
echo ""
echo -e "${YELLOW}Option 3 - Using package manager:${NC}"
echo " # Ubuntu/Debian:"
echo " curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -"
echo " sudo apt-get install -y nodejs"
echo ""
print_error "Please install Node.js 14.x and run this script again."
exit 1
fi
CURRENT_NODE_VERSION=$(node --version 2>/dev/null || echo "not found")
print_step "Current Node.js version: $CURRENT_NODE_VERSION"
# Extract major version number
NODE_MAJOR_VERSION=$(echo "$CURRENT_NODE_VERSION" | cut -d'v' -f2 | cut -d'.' -f1 2>/dev/null || echo "0")
if [ "$NODE_MAJOR_VERSION" -ge 14 ] && [ "$NODE_MAJOR_VERSION" -lt 15 ]; then
print_success "Node.js $CURRENT_NODE_VERSION is compatible with Azure Functions v3"
elif [ "$NODE_MAJOR_VERSION" -eq 0 ]; then
print_error "Could not determine Node.js version. Please reinstall Node.js."
exit 1
else
print_warning "Node.js 14.x is recommended for Azure Functions v3"
print_warning "Current version: $CURRENT_NODE_VERSION"
print_warning "The setup will continue, but you may encounter issues."
print_warning "Consider using nvm to switch to Node.js 14:"
print_warning " nvm install 14 && nvm use 14"
echo ""
read -p "Continue anyway? (y/N): " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_error "Setup cancelled. Please install Node.js 14 and try again."
exit 1
fi
fi
# Check if Azure Functions Core Tools is installed
print_step "Setting up Azure Functions Core Tools..."
if ! command -v func &> /dev/null; then
print_step "Installing Azure Functions Core Tools..."
npm install -g azure-functions-core-tools@3 --unsafe-perm true
print_success "Azure Functions Core Tools installed"
else
print_success "Azure Functions Core Tools is already installed"
fi
# Install npm dependencies
print_step "Installing npm dependencies..."
npm install
if [ $? -ne 0 ]; then
print_error "Failed to install dependencies"
exit 1
fi
print_success "Dependencies installed successfully"
# Set up environment variables
print_step "Setting up environment variables..."
export DATABASE_URL="file:./prisma/dev.db"
export API_KEY="local-dev-key"
# Load environment variables from .env.local if it exists
if [ -f ".env.local" ]; then
print_step "Loading environment variables from .env.local..."
export $(grep -v '^#' .env.local | xargs)
print_success "Environment variables loaded from .env.local"
else
print_warning ".env.local not found, using default values"
fi
# Create environment configuration
print_step "Setting up environment configuration..."
# Create .env.local if it doesn't exist
if [ ! -f ".env.local" ]; then
print_step "Creating .env.local from template..."
cp env.example .env.local
print_success ".env.local created from template"
print_warning "Please update .env.local with your secure values"
else
print_success ".env.local already exists"
fi
# Create local.settings.json for Azure Functions
if [ ! -f "local.settings.json" ]; then
print_step "Creating local.settings.json..."
cat > local.settings.json << 'EOF'
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "node",
"API_KEY": "local-dev-key",
"DATABASE_URL": "file:./prisma/dev.db"
}
}
EOF
print_success "local.settings.json created"
else
print_success "local.settings.json already exists"
fi
print_success "Environment variables set"
# Generate Prisma client
print_step "Generating Prisma client..."
DATABASE_URL="file:./prisma/dev.db" npx prisma generate
if [ $? -ne 0 ]; then
print_error "Failed to generate Prisma client"
exit 1
fi
print_success "Prisma client generated"
# Regenerate Prisma client after build to ensure correct binary targets
print_step "Regenerating Prisma client for correct binary targets..."
DATABASE_URL="file:./prisma/dev.db" npx prisma generate
if [ $? -ne 0 ]; then
print_error "Failed to regenerate Prisma client"
exit 1
fi
print_success "Prisma client regenerated with correct binary targets"
# Run database migrations
print_step "Setting up database with migrations..."
DATABASE_URL="file:./prisma/dev.db" npx prisma migrate dev --name init
if [ $? -ne 0 ]; then
print_error "Failed to run database migrations"
exit 1
fi
print_success "Database setup complete"
# Build the project
print_step "Building TypeScript project..."
DATABASE_URL="file:./prisma/dev.db" API_KEY="local-dev-key" npm run build
if [ $? -ne 0 ]; then
print_error "Build failed. Please check the errors above."
exit 1
fi
print_success "Build successful"
# Regenerate Prisma client in dist folder
print_step "Regenerating Prisma client in dist folder..."
cd dist && DATABASE_URL="file:./prisma/dev.db" npx prisma generate && cd ..
if [ $? -ne 0 ]; then
print_error "Failed to regenerate Prisma client in dist folder"
exit 1
fi
print_success "Prisma client regenerated in dist folder"
# Run tests
print_step "Running tests..."
npm test
if [ $? -ne 0 ]; then
print_warning "Some tests failed, but setup will continue..."
else
print_success "All tests passed"
fi
# Create a simple start script
print_step "Creating development start script..."
cat > start-dev.sh << 'EOF'
#!/bin/bash
echo "🚀 Starting Leasing API..."
echo "Building project..."
npm run build
echo "Starting Azure Functions..."
func start --script-root dist
EOF
chmod +x start-dev.sh
print_success "Development start script created"
# Final success message
echo ""
print_header "🎉 Setup Complete!"
echo ""
echo -e "${GREEN}Your Leasing API is ready for development!${NC}"
echo ""
echo -e "${CYAN}📋 Quick Start Commands:${NC}"
echo ""
echo -e "${YELLOW}Start the API:${NC}"
echo " ./start-dev.sh"
echo " # OR"
echo " npm start"
echo ""
echo -e "${YELLOW}Test the API:${NC}"
echo " curl -H \"x-api-key: local-dev-key\" http://localhost:7071/api/health"
echo ""
echo -e "${YELLOW}Development Commands:${NC}"
echo " npm run dev # Watch TypeScript compilation"
echo " npm test # Run all tests"
echo " npm run prisma:studio # Open database GUI"
echo ""
echo -e "${CYAN}🔗 API Endpoints:${NC}"
echo " Health: http://localhost:7071/api/health"
echo " Quote: http://localhost:7071/api/quote"
echo " Leases: http://localhost:7071/api/leases"
echo " Payments: http://localhost:7071/api/payments"
echo ""
echo -e "${GREEN}🚀 Ready to develop! Run ./start-dev.sh to begin.${NC}"
echo ""