#!/bin/bash # Dyad Update Script v2 - Selective Update Approach # This script updates your forked Dyad app by backing up custom code, # resetting to upstream, then restoring custom code. 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' NC='\033[0m' # No Color # Function to print colored output print_status() { echo -e "${BLUE}[INFO]${NC} $1" } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check if we're in a git repository if ! git rev-parse --git-head > /dev/null 2>&1; then print_error "Not in a git repository. Please run this script from the project root." exit 1 fi print_status "Starting Dyad selective update process..." print_status "Current branch: $(git branch --show-current)" # Create a temporary backup directory BACKUP_DIR="dyad-backup-$(date +%Y%m%d-%H%M%S)" print_status "Creating backup in: $BACKUP_DIR" mkdir -p "$BACKUP_DIR" # Backup custom code structure print_status "Backing up your custom code..." # Backup the custom directory if it exists if [ -d "src/custom" ]; then cp -r src/custom "$BACKUP_DIR/" print_success "Backed up src/custom/" fi # Backup any other custom files you might have added # Add any additional files/directories you want to preserve here # Backup current state as a reference print_status "Creating reference backup..." git log --oneline -10 > "$BACKUP_DIR/commit-history.txt" git diff HEAD~1 > "$BACKUP_DIR/last-changes.diff" 2>/dev/null || true # Fetch latest changes from upstream print_status "Fetching latest changes from upstream..." if git fetch upstream; then print_success "Successfully fetched changes from upstream." else print_error "Failed to fetch from upstream. Check your internet connection." exit 1 fi # Get the upstream commit info UPSTREAM_COMMIT=$(git rev-parse upstream/main) print_status "Upstream commit: $UPSTREAM_COMMIT" # Reset to upstream print_status "Resetting to upstream/main..." if git reset --hard upstream/main; then print_success "Successfully reset to upstream/main." else print_error "Failed to reset to upstream/main." exit 1 fi # Restore custom code print_status "Restoring your custom code..." if [ -d "$BACKUP_DIR/custom" ]; then cp -r "$BACKUP_DIR/custom" src/ print_success "Restored src/custom/" fi # Update package.json to include any custom dependencies if needed print_status "Checking for custom dependencies..." # Add custom files to git print_status "Adding custom files to git..." git add src/custom/ UPDATE_GUIDE.md update-dyad*.sh 2>/dev/null || true # Create a commit for the update print_status "Creating commit for the update..." git commit -m "feat: update to upstream $UPSTREAM_COMMIT and restore custom code - Updated to latest upstream version - Restored custom code in src/custom/ - Preserved custom modifications Backup saved in: $BACKUP_DIR" || { print_warning "No changes to commit (custom code already matches upstream)" } # Push to origin print_status "Pushing to your fork..." if git push origin main --force-with-lease; then print_success "Successfully pushed to origin." else print_warning "Push failed. Push manually with: git push origin main --force-with-lease" fi print_success "🎉 Update completed successfully!" print_status "Summary:" print_status "- Updated to latest upstream version" print_status "- Your custom code has been preserved in src/custom/" print_status "- Backup saved in: $BACKUP_DIR" print_status "" print_status "Next steps:" print_status "1. Test the application to ensure everything works" print_status "2. Run 'npm install' to update dependencies if needed" print_status "3. Check if any of your custom code needs updates for the new version" print_status "" print_status "If you need to restore from backup:" print_status "1. Copy files from $BACKUP_DIR back to your project" print_status "2. Commit and push the changes"