From 543f655bc1fc3fda54d2433bd5ae3e4e1978af3b Mon Sep 17 00:00:00 2001 From: catlog22 Date: Sat, 11 Oct 2025 23:53:18 +0800 Subject: [PATCH] Fix Install-Claude.ps1: Only clear conflicting items, preserve other files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Issue:** Previous logic cleared entire destination directory, removing all existing files including user-created content. **Fix:** Modified `Backup-AndReplaceDirectory` to: 1. Backup entire destination directory (as before) 2. Only remove items in destination that match source item names 3. Copy source items to destination 4. Preserve all other files in destination directory **New Behavior:** - Removes: ~/.claude/commands (if exists in source) - Removes: ~/.claude/workflows (if exists in source) - Preserves: ~/.claude/custom-file.md (user-created) - Preserves: ~/.claude/.workflow/ (session data) **Example:** Source: .claude/{commands, workflows, scripts} Destination before: .claude/{commands, workflows, custom-config.json, .workflow/} Destination after: .claude/{commands, workflows, scripts, custom-config.json, .workflow/} **Benefits:** - Protects user-created files and session data - Only updates files that come from installation source - Safer incremental updates 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- Install-Claude.ps1 | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/Install-Claude.ps1 b/Install-Claude.ps1 index 243fcfd0..4cbaddf5 100644 --- a/Install-Claude.ps1 +++ b/Install-Claude.ps1 @@ -583,7 +583,7 @@ function Backup-AndReplaceDirectory { return $false } - # Backup and clear destination if it exists + # Backup destination if it exists if (Test-Path $Destination) { Write-ColorOutput "Found existing $Description at: $Destination" $ColorInfo @@ -600,15 +600,31 @@ function Backup-AndReplaceDirectory { } } - # Clear destination directory - Write-ColorOutput "Clearing destination $Description..." $ColorInfo - Remove-Item -Path $Destination -Recurse -Force -ErrorAction SilentlyContinue - Write-ColorOutput "Cleared destination $Description" $ColorSuccess + # Get all items from source to determine what to clear in destination + Write-ColorOutput "Clearing conflicting items in destination $Description..." $ColorInfo + $sourceItems = Get-ChildItem -Path $Source -Force + + foreach ($sourceItem in $sourceItems) { + $destItemPath = Join-Path $Destination $sourceItem.Name + if (Test-Path $destItemPath) { + Write-ColorOutput "Removing existing: $($sourceItem.Name)" $ColorInfo + Remove-Item -Path $destItemPath -Recurse -Force -ErrorAction SilentlyContinue + } + } + Write-ColorOutput "Cleared conflicting items in destination" $ColorSuccess + } else { + # Create destination directory if it doesn't exist + New-Item -ItemType Directory -Path $Destination -Force | Out-Null + Write-ColorOutput "Created destination directory: $Destination" $ColorInfo } - # Copy entire source directory to destination + # Copy all items from source to destination Write-ColorOutput "Copying $Description from $Source to $Destination..." $ColorInfo - Copy-Item -Path $Source -Destination $Destination -Recurse -Force + $sourceItems = Get-ChildItem -Path $Source -Force + foreach ($item in $sourceItems) { + $destPath = Join-Path $Destination $item.Name + Copy-Item -Path $item.FullName -Destination $destPath -Recurse -Force + } Write-ColorOutput "$Description installed successfully" $ColorSuccess return $true