diff --git a/Reset-Environment.ps1 b/Reset-Environment.ps1 new file mode 100644 index 0000000..1429016 --- /dev/null +++ b/Reset-Environment.ps1 @@ -0,0 +1,149 @@ +<# +.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." diff --git a/reset-environment.sh b/reset-environment.sh index 4a009c8..fdee4aa 100755 --- a/reset-environment.sh +++ b/reset-environment.sh @@ -9,7 +9,7 @@ set -o pipefail # Error if any command in a pipe fails # Constants readonly SCRIPT_NAME="$(basename "$0")" readonly WORK_DIR="/home/acedanger/dev/finance" -readonly REQUIRED_NODE_VERSION="20.19.0" # Match the version from devcontainer +readonly REQUIRED_NODE_VERSION="20.19.0" # Minimum required version # Functions log() { @@ -23,10 +23,15 @@ error() { validate_node_version() { local current_version current_version=$(node -v | cut -d 'v' -f 2) - if [[ "$current_version" != "$REQUIRED_NODE_VERSION" ]]; then - error "Node.js version mismatch. Required: $REQUIRED_NODE_VERSION, Found: $current_version" + + # Use sort -V for version comparison + if [[ "$(printf '%s\n' "$REQUIRED_NODE_VERSION" "$current_version" | sort -V | head -n1)" != "$REQUIRED_NODE_VERSION" ]]; then + # If the first entry is not the required version, it means current < required + error "Node.js version too old. Minimum required: $REQUIRED_NODE_VERSION, Found: $current_version" exit 1 fi + + log "Node.js version check passed. Required: $REQUIRED_NODE_VERSION+, Found: $current_version" } validate_dependencies() {