scripts/entrypoint-dev.sh
#!/bin/sh

# Load environment variables from .env file
if [ -f .env ]; then
    export $(grep '^NODE_ENV=' .env | xargs)
fi

# Check if node_modules exists or if package.json is newer than node_modules
if [ ! -d "node_modules" ] || [ "package.json" -nt "node_modules" ]; then
    echo "Installing dependencies..."
    # TODO: Remove force when React 19 is released
    npm install --force
    npx prisma generate
else
    # Check if prisma client needs to be generated by comparing timestamps
    if [ "prisma/schema.prisma" -nt "node_modules/.prisma/client" ] 2>/dev/null; then
        echo "Regenerating Prisma client..."
        npx prisma generate
    else
        echo "Dependencies are up to date..."
    fi
fi

# Check migration status
MIGRATION_STATUS=$(npx prisma migrate status)
if echo "$MIGRATION_STATUS" | grep -q "Database schema is up to date"; then
    echo "Database schema is up to date, skipping migration..."
else
    echo "Deploying database migrations..."
    npx prisma migrate deploy
fi

# Run different commands based on NODE_ENV
if [ "$NODE_ENV" = "production" ]; then
    echo "Running in production mode..."
    npm run build
    npm start
else
    echo "Running in development mode..."
    npm run dev
fi
