Repository Interface Optimization: - Created IBaseRepository.ts and IPaginatedRepository.ts - Refactored all 7 repository interfaces to extend base interfaces - Eliminated ~200 lines of redundant code (70% reduction) - Improved type safety and maintainability Dependency Injection Improvements: - Added EmailService and GameTokenService to DIContainer - Updated CreateUserCommandHandler constructor for DI - Updated RequestPasswordResetCommandHandler constructor for DI - Enhanced testability and service consistency Environment Configuration: - Created comprehensive .env.example with 40+ variables - Organized into 12 logical sections (Database, Security, Email, etc.) - Added security guidelines and best practices - Documented all backend environment requirements Documentation: - Added comprehensive codebase review - Created refactoring summary report - Added frontend implementation guide Impact: Improved code quality, reduced maintenance overhead, enhanced developer experience
7.8 KiB
SerpentRace Backend Build System
Overview
This document describes the comprehensive build system for the SerpentRace backend application. The build system handles TypeScript compilation, database migrations, asset management, testing, and deployment.
Quick Start
# Development build
npm run build
# Production build with full validation
npm run build:production
# Advanced build with migrations and tests
npm run build:advanced:prod
# Development server with hot reload
npm run dev
Build Scripts
Basic Build Commands
| Command | Description |
|---|---|
npm run build |
Standard build: clean → compile → copy assets |
npm run build:clean |
Clean the dist directory |
npm run build:compile |
Compile TypeScript to JavaScript |
npm run build:copy-assets |
Copy non-TS files to dist directory |
npm run build:docker |
Build for Docker (no tests/migrations) |
Production Build Commands
| Command | Description |
|---|---|
npm run build:production |
Full production build with linting, tests, and migrations |
npm run build:advanced |
Advanced build script with custom options |
npm run build:advanced:prod |
Advanced production build with all validations |
npm run build:advanced:ci |
CI/CD friendly build (skips linting) |
Development Commands
| Command | Description |
|---|---|
npm run dev |
Start development server with hot reload |
npm run watch |
Watch mode TypeScript compilation |
npm run typecheck |
Type checking without code generation |
Database Commands
| Command | Description |
|---|---|
npm run migration:run |
Run pending database migrations |
npm run migration:show |
Show migration status |
npm run migration:generate <name> |
Generate new migration |
npm run migration:create <name> |
Create empty migration |
npm run migration:revert |
Revert last migration |
npm run migration:full <name> |
Create, generate, and run migration |
Testing Commands
| Command | Description |
|---|---|
npm test |
Run all tests |
npm run test:watch |
Run tests in watch mode |
npm run test:coverage |
Run tests with coverage report |
npm run test:redis |
Run Redis-specific tests |
Deployment Commands
| Command | Description |
|---|---|
npm run deploy:prod |
Build for production deployment |
scripts/deploy.sh |
Full Linux/Mac deployment script |
scripts/deploy.bat |
Full Windows deployment script |
Advanced Build Script
The advanced build script (scripts/build.ts) supports various options:
# Basic advanced build
npm run build:advanced
# Production build with migrations and tests
npm run build:advanced:prod
# CI/CD build (skips linting, includes tests and migrations)
npm run build:advanced:ci
Build Options
--migrations: Run database migrations during build--test: Run tests during build--skip-lint: Skip linting step--production: Enable production mode (strict validation)
Deployment Scripts
Linux/Mac Deployment
./scripts/deploy.sh [deploy|build-only|test-connections]
Options:
deploy(default): Full deployment with validationbuild-only: Build without connection testingtest-connections: Test database and Redis connections only
Windows Deployment
scripts\deploy.bat [deploy|build-only|test-connections]
Same options as Linux/Mac version.
Required Environment Variables
The deployment scripts require these environment variables:
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=your_password
DB_NAME=serpentrace
JWT_SECRET=your_jwt_secret
REDIS_HOST=localhost
REDIS_PORT=6379
Build Process Flow
Standard Build (npm run build)
- Clean - Remove previous build artifacts
- Lint - Code quality checks (if configured)
- Compile - TypeScript compilation
- Copy Assets - Copy non-TS files to dist
- Post-build - Validation and cleanup
Production Build (npm run build:production)
- Clean - Remove previous build artifacts
- Lint - Code quality checks
- Test - Run test suite
- Migrations - Apply database migrations
- Compile - TypeScript compilation
- Copy Assets - Copy non-TS files to dist
- Validate - Ensure build integrity
Advanced Build (npm run build:advanced)
Provides fine-grained control over the build process with comprehensive logging and error handling.
Asset Management
The build system automatically copies these file types to the dist directory:
.jsonfiles (configuration, data).htmlfiles (templates).cssfiles (stylesheets)- Image files (
.png,.jpg,.jpeg,.gif,.svg,.ico) - Font files (
.woff,.woff2,.ttf,.eot)
Excluded directories:
node_modules.gittests__tests__
TypeScript Configuration
The build system uses the following TypeScript settings:
- Target: ES2020
- Module: CommonJS
- Output Directory:
./dist - Source Maps: Enabled
- Declarations: Enabled for type definitions
- Strict Mode: Enabled for type safety
Migration Management
Creating Migrations
# Create empty migration
npm run migration:create AddNewTable
# Generate migration from entity changes
npm run migration:generate AddNewTable
# Full migration workflow (create + generate + run)
npm run migration:full AddNewTable
Migration Best Practices
- Always backup database before running migrations in production
- Test migrations in development environment first
- Use descriptive migration names
- Review generated migrations before running them
Docker Integration
The build system is optimized for Docker deployments:
# Use build:docker for container builds
RUN npm run build:docker
# Or use production build for full validation
RUN npm run build:production
Troubleshooting
Common Issues
-
Build fails with "Cannot find module"
- Run
npm cito ensure all dependencies are installed - Check TypeScript paths configuration
- Run
-
Migration errors during build
- Verify database connection parameters
- Ensure database exists and is accessible
- Check migration files for syntax errors
-
Asset copying fails
- Verify file permissions
- Check disk space availability
- Ensure source files exist
-
TypeScript compilation errors
- Run
npm run typecheckfor detailed error messages - Check tsconfig.json configuration
- Verify all type definitions are installed
- Run
Debug Mode
Enable verbose logging by setting the environment variable:
export DEBUG=serpentrace:*
npm run build:advanced
Performance Optimization
Build Performance Tips
- Use
npm ciinstead ofnpm installin CI/CD - Enable TypeScript incremental compilation for development
- Use
--skip-lintin CI if linting is handled separately - Cache node_modules in CI/CD pipelines
Runtime Performance
The build system optimizes the output for production:
- Source maps for debugging (can be disabled in production)
- Type declarations for library usage
- Compressed and optimized JavaScript output
Monitoring and Logging
Build logs include:
- Timestamps for each build step
- Error details with stack traces
- Performance metrics (build duration)
- Validation results
Production builds create detailed logs in the logs/ directory.
Contributing
When modifying the build system:
- Test changes with both development and production builds
- Update this documentation for any new scripts or options
- Ensure backward compatibility
- Add appropriate error handling and logging
Support
For build system issues:
- Check this documentation
- Review error logs in the console
- Verify environment variables are set correctly
- Test with a clean
node_modulesinstallation