<# .SYNOPSIS Resets the finance application development environment. .DESCRIPTION This script resets the development environment by: - Validating dependencies and Node.js version - Resetting the Prisma database - Cleaning up Docker containers - Removing generated files and dependencies - Reinstalling NPM packages - Building the project - Starting Docker containers .NOTES Created: 2025-05-17 Author: GitHub Copilot #> # Stop on first error $ErrorActionPreference = "Stop" # Constants $ScriptName = $MyInvocation.MyCommand.Name $WorkDir = "c:\dev\finance" $RequiredNodeVersion = "20.19.0" # Minimum required version # Functions function Write-Log { param ([string]$Message) Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $Message" } function Write-Error { param ([string]$Message) Write-Host "ERROR: $Message" -ForegroundColor Red } function Test-NodeVersion { $currentVersion = (node -v).Replace('v', '') # Create System.Version objects for proper comparison $minVersion = [System.Version]::new($RequiredNodeVersion) $installedVersion = [System.Version]::new($currentVersion) if ($installedVersion -lt $minVersion) { Write-Error "Node.js version too old. Minimum required: $RequiredNodeVersion, Found: $currentVersion" exit 1 } Write-Log "Node.js version check passed. Required: $RequiredNodeVersion+, Found: $currentVersion" } function Test-Dependencies { $missingDeps = @() # Check core dependencies foreach ($cmd in @('npx', 'docker', 'npm', 'node', 'git')) { if (-not (Get-Command $cmd -ErrorAction SilentlyContinue)) { $missingDeps += $cmd } } # Check for biome in node_modules if (-not (Test-Path "node_modules/@biomejs/biome/bin/biome")) { $missingDeps += "biome.js" } # Check for husky hooks if (-not (Test-Path ".husky/pre-commit")) { $missingDeps += "husky pre-commit hook" } if ($missingDeps.Count -ne 0) { Write-Error "Missing dependencies: $($missingDeps -join ', ')" exit 1 } } # Main script logic Write-Log "Resetting finance development environment..." # Change to working directory try { Set-Location -Path $WorkDir } catch { Write-Error "Failed to change directory to $WorkDir" exit 1 } # Validate dependencies and Node version Test-Dependencies Test-NodeVersion # Reset Prisma database try { npx prisma migrate reset --force } catch { Write-Error "Prisma migration reset failed" exit 1 } # Take down Docker containers try { docker compose down } catch { Write-Error "Failed to take down Docker containers" exit 1 } # Clean up generated files and dependencies Write-Log "Cleaning up generated files and dependencies..." if (Test-Path "package-lock.json") { Remove-Item "package-lock.json" -Force } if (Test-Path "dist") { Remove-Item "dist" -Recurse -Force } if (Test-Path "node_modules") { Remove-Item "node_modules" -Recurse -Force } if (Test-Path ".prisma") { Remove-Item ".prisma" -Recurse -Force } if (Test-Path "prisma/migrations/migration_lock.toml") { Remove-Item "prisma/migrations/migration_lock.toml" -Force } # Reinstall dependencies try { npm install } catch { Write-Error "Failed to install npm dependencies" exit 1 } # Install Husky hooks try { npm run prepare } catch { Write-Error "Failed to install Husky hooks" exit 1 } # Build the project try { npm run build } catch { Write-Error "Failed to build the project" exit 1 } # Start Docker containers try { docker compose -f docker-compose.yml up -d } catch { Write-Error "Failed to bring up Docker containers" exit 1 } Write-Log "Environment reset complete."