Skip to content

Commit 002da3b

Browse files
committed
added work directory clean script
1 parent 84dba17 commit 002da3b

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

docs/clean.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,124 @@ Note: Sometimes the log file name might be different. So check them in the first
2626

2727
``` powershell
2828
Get-ChildItem .\ -include bin,obj,packages -Recurse | ForEach-Object ($_) { Remove-Item $_.FullName -Force -Recurse }
29+
```
30+
31+
### Clean up work directory
32+
```powershell
33+
$baseDirectory = Get-Location
34+
35+
function Find-GitRepos {
36+
param([string]$Path)
37+
38+
Get-ChildItem -Path $Path -Directory | ForEach-Object {
39+
try {
40+
$gitFolder = Join-Path $_.FullName ".git"
41+
if (Test-Path $gitFolder) {
42+
# Found a git repo, process it
43+
Set-Location $_.FullName
44+
$gitStatus = git status --porcelain
45+
if ($gitStatus) {
46+
Write-Host "Skipping git cleanup for repository: $($_.FullName) as it has changes." -ForegroundColor Red
47+
}
48+
else {
49+
Write-Host "Performing git cleanup for repository: $($_.FullName)" -ForegroundColor Green
50+
git clean -ffdx
51+
}
52+
} else {
53+
# Recurse into subdirectories
54+
Find-GitRepos -Path $_.FullName
55+
}
56+
} catch {
57+
Write-Host "Error processing directory: $($_.FullName)" -ForegroundColor Red
58+
}
59+
finally {
60+
Set-Location $baseDirectory
61+
}
62+
}
63+
}
64+
65+
Find-GitRepos -Path $baseDirectory
66+
67+
function Remove-FoldersRecursively {
68+
param([string]$Path)
69+
70+
Get-ChildItem -Path $Path -Directory -Recurse | Where-Object {
71+
$_.Name -in @(
72+
'node_modules', # npm packages
73+
'bin', # build output
74+
'obj', # build output
75+
'.vs', # Visual Studio workspace
76+
'.idea', # JetBrains IDE settings
77+
'.vscode', # VS Code settings
78+
'dist', # distribution/build output
79+
'out', # build output
80+
'coverage', # test coverage reports
81+
'target', # Maven/Gradle build output
82+
'.pytest_cache', # pytest cache
83+
'.cache', # general cache
84+
'.next', # Next.js build output
85+
'.nuxt', # Nuxt.js build output
86+
'.angular', # Angular build output
87+
'build', # generic build output
88+
'.sass-cache', # Sass cache
89+
'.serverless', # Serverless framework output
90+
'.expo', # Expo React Native
91+
'.turbo', # Turborepo cache
92+
'.parcel-cache' # Parcel cache
93+
)
94+
} | ForEach-Object {
95+
try {
96+
Remove-Item -Path $_.FullName -Recurse -Force -ErrorAction Stop
97+
Write-Host "Deleted folder: $($_.FullName)" -ForegroundColor Green
98+
} catch {
99+
Write-Host "Failed to delete folder: $($_.FullName)" -ForegroundColor Red
100+
}
101+
}
102+
}
103+
104+
Remove-FoldersRecursively -Path $baseDirectory
105+
106+
function Remove-EmptyDirectories {
107+
param([string]$Path)
108+
109+
Get-ChildItem -Path $Path -Directory -Recurse | Where-Object {
110+
$_.GetFileSystemInfos().Count -eq 0
111+
} | ForEach-Object {
112+
try {
113+
Remove-Item -Path $_.FullName -Force -ErrorAction Stop
114+
Write-Host "Deleted empty directory: $($_.FullName)" -ForegroundColor Green
115+
} catch {
116+
Write-Host "Failed to delete empty directory: $($_.FullName)" -ForegroundColor Red
117+
}
118+
}
119+
}
120+
121+
Remove-EmptyDirectories -Path $baseDirectory
122+
123+
function Remove-TemporaryFiles {
124+
param([string]$Path)
125+
126+
Get-ChildItem -Path $Path -File -Recurse | Where-Object {
127+
$_.Extension -in @(
128+
'.tmp', '.temp', '.log', '.cache', '.bak', '.swp', '.swo', '.DS_Store'
129+
)
130+
} | ForEach-Object {
131+
try {
132+
Remove-Item -Path $_.FullName -Force -ErrorAction Stop
133+
Write-Host "Deleted temporary file: $($_.FullName)" -ForegroundColor Green
134+
} catch {
135+
Write-Host "Failed to delete temporary file: $($_.FullName)" -ForegroundColor Red
136+
}
137+
}
138+
}
139+
140+
Remove-TemporaryFiles -Path $baseDirectory
141+
# Restore the original location
142+
Set-Location $baseDirectory
143+
Write-Host "Cleanup completed." -ForegroundColor Green
144+
Write-Host "Current directory: $(Get-Location)" -ForegroundColor Cyan
145+
# End of clean.ps1
146+
# This script performs a cleanup of git repositories, removes specific folders, deletes empty directories, and removes temporary files recursively from the specified base directory.
147+
# It also handles errors gracefully and provides feedback on the operations performed.
148+
# Usage: Run this script in PowerShell to clean up your workspace.
29149
```

0 commit comments

Comments
 (0)