Created
June 22, 2024 15:06
-
-
Save VIRUXE/26e38bf5c1c8a4c423eaa213955f7f8d to your computer and use it in GitHub Desktop.
Deletes node_modules folders, recursively.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param( | |
[Parameter(Mandatory)] | |
[string]$RootPath | |
) | |
if (-not $RootPath) { $RootPath = Read-Host "Enter the root path to search for 'node_modules' folders" } | |
function Remove-NodeModules($path) { | |
$success = $true | |
Get-ChildItem -Path $path -Directory -Recurse | Where-Object { $_.Name -eq "node_modules" } | ForEach-Object { | |
try { | |
Write-Host "Deleting:", $_.FullName | |
Remove-Item -Recurse -Force -Path $_.FullName -ErrorAction Stop | |
} catch { | |
$success = $false | |
Write-Host "Error deleting '$($_.FullName)'. Attempting partial deletion..." | |
Remove-NodeModules $_.FullName | |
} | |
} | |
return $success | |
} | |
try { | |
if (-not (Test-Path $RootPath)) { throw "Invalid RootPath provided: $RootPath" } | |
$success = Remove-NodeModules $RootPath | |
if (-not $success) { Write-Host "Operation completed with errors." } else { Write-Host "Operation completed successfully." } | |
} catch { | |
Write-Error $_.Exception.Message | |
} finally { | |
Write-Host "Press any key to exit..." | |
[Console]::ReadKey($true) | Out-Null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment