Skip to content

Commit ac3c66f

Browse files
committed
- Fix -Json -WhatIf so PowerShell WhatIf host messages are suppressed and
standard output remains a single JSON object - Update JSON output documentation to match the current `Mode` values emitted by the script: `Execute`, `WhatIf (no changes)`, and `Check (no changes)`
1 parent 2e140ed commit ac3c66f

4 files changed

Lines changed: 44 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@ All notable changes to this project will be documented in this file.
44

55
---
66

7+
## [Unreleased]
8+
9+
- Fix `-Json -WhatIf` so PowerShell WhatIf host messages are suppressed and
10+
standard output remains a single JSON object
11+
- Update JSON output documentation to match the current `Mode` values emitted
12+
by the script: `Execute`, `WhatIf (no changes)`, and `Check (no changes)`
13+
714
## [0.12.0] 2026-04-23
815

916
- Add `WinARM64EC` to list of directories in Standard cleanup level
10-
for Delphi 13. [#12](https://github.com/continuous-delphi/delphi-clean/issues/23)
17+
for Delphi 13. [#23](https://github.com/continuous-delphi/delphi-clean/issues/23)
1118

1219
## [0.11.0] 2026-04-23
1320

docs/json-output.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This object is returned after a scan, whether or not files are deleted.
1616
"Root": "C:/code/myproject",
1717
"ExcludeDirectoryPattern": [".git", ".vs", ".claude"],
1818
"IncludeFilePattern": [],
19-
"Mode": "Clean",
19+
"Mode": "Execute",
2020
"Disposition": "Permanent",
2121
"RecycleBin": false,
2222
"Check": false,
@@ -43,7 +43,7 @@ This object is returned after a scan, whether or not files are deleted.
4343
| `Root` | string | Absolute path of the scanned root directory |
4444
| `ExcludeDirectoryPattern` | string array | Combined list of built-in and user-supplied exclusion patterns |
4545
| `IncludeFilePattern` | string array | Additional file patterns supplied via config or `-IncludeFilePattern` |
46-
| `Mode` | string | `Clean`, `WhatIf`, or `Check` |
46+
| `Mode` | string | `Execute`, `WhatIf (no changes)`, or `Check (no changes)` |
4747
| `Disposition` | string | `Permanent` or `Recycle Bin` |
4848
| `RecycleBin` | boolean | Whether `-RecycleBin` was active |
4949
| `Check` | boolean | Whether `-Check` was active |
@@ -73,9 +73,9 @@ during the scan.
7373

7474
| Value | When set |
7575
|-------|----------|
76-
| `Clean` | Normal run -- items are deleted (or recycled) |
77-
| `WhatIf` | `-WhatIf` was supplied -- scan only, no deletions |
78-
| `Check` | `-Check` was supplied -- scan only, no deletions |
76+
| `Execute` | Normal run -- items are deleted (or recycled) |
77+
| `WhatIf (no changes)` | `-WhatIf` was supplied -- scan only, no deletions |
78+
| `Check (no changes)` | `-Check` was supplied -- scan only, no deletions |
7979

8080
---
8181

@@ -90,7 +90,7 @@ with zeroed counters and an empty `Items` array.
9090
"Root": "C:/code/myproject",
9191
"ExcludeDirectoryPattern": [".git", ".vs", ".claude"],
9292
"IncludeFilePattern": [],
93-
"Mode": "Clean",
93+
"Mode": "Execute",
9494
"Disposition": "Permanent",
9595
"RecycleBin": false,
9696
"Check": false,

source/delphi-clean.ps1

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,8 @@ function Remove-FileList {
975975
param(
976976
[System.IO.FileInfo[]]$Files = @(),
977977
[switch]$ReturnRecords,
978-
[switch]$RecycleBin
978+
[switch]$RecycleBin,
979+
[switch]$SuppressWhatIfOutput
979980
)
980981

981982
$result = [PSCustomObject]@{
@@ -995,6 +996,14 @@ function Remove-FileList {
995996
foreach ($file in $Files) {
996997
Write-Verbose "Evaluating file: $($file.FullName)"
997998

999+
if ($WhatIfPreference -and $SuppressWhatIfOutput) {
1000+
Write-Detail "Would $($action.ToLower()): $($file.FullName)"
1001+
if ($ReturnRecords) {
1002+
$result.Records.Add((ConvertTo-DeletionRecord -Type File -Path $file.FullName -Deleted $false -Size $file.Length))
1003+
}
1004+
continue
1005+
}
1006+
9981007
if ($PSCmdlet.ShouldProcess($file.FullName, $action)) {
9991008
try {
10001009
if ($RecycleBin) {
@@ -1036,7 +1045,8 @@ function Remove-DirectoryList {
10361045
param(
10371046
[System.IO.DirectoryInfo[]]$Directories = @(),
10381047
[switch]$ReturnRecords,
1039-
[switch]$RecycleBin
1048+
[switch]$RecycleBin,
1049+
[switch]$SuppressWhatIfOutput
10401050
)
10411051

10421052
$result = [PSCustomObject]@{
@@ -1063,6 +1073,14 @@ function Remove-DirectoryList {
10631073
# Compute size before any deletion so it is available for the record regardless of outcome
10641074
$dirSize = Get-TreeSize -Path $dir.FullName
10651075

1076+
if ($WhatIfPreference -and $SuppressWhatIfOutput) {
1077+
Write-Detail "Would $($action.ToLower()): $($dir.FullName)"
1078+
if ($ReturnRecords) {
1079+
$result.Records.Add((ConvertTo-DeletionRecord -Type Directory -Path $dir.FullName -Deleted $false -Size $dirSize))
1080+
}
1081+
continue
1082+
}
1083+
10661084
if ($PSCmdlet.ShouldProcess($dir.FullName, $action)) {
10671085
try {
10681086
if ($RecycleBin) {
@@ -1344,9 +1362,9 @@ try {
13441362
# Normal clean path
13451363
Write-Section 'Cleaning'
13461364
if (-not $Json) { Write-Progress -Activity 'delphi-clean' -Status "Removing $($filesToDelete.Count) files..." }
1347-
$fileRemovalResult = Remove-FileList -Files $filesToDelete -ReturnRecords:$returnRecords -RecycleBin:$RecycleBin
1365+
$fileRemovalResult = Remove-FileList -Files $filesToDelete -ReturnRecords:$returnRecords -RecycleBin:$RecycleBin -SuppressWhatIfOutput:$Json
13481366
if (-not $Json) { Write-Progress -Activity 'delphi-clean' -Status "Removing $($dirsToDelete.Count) directories..." }
1349-
$dirRemovalResult = Remove-DirectoryList -Directories $dirsToDelete -ReturnRecords:$returnRecords -RecycleBin:$RecycleBin
1367+
$dirRemovalResult = Remove-DirectoryList -Directories $dirsToDelete -ReturnRecords:$returnRecords -RecycleBin:$RecycleBin -SuppressWhatIfOutput:$Json
13501368
if (-not $Json) { Write-Progress -Activity 'delphi-clean' -Completed }
13511369

13521370
$allRecords = New-Object System.Collections.Generic.List[object]

tests/pwsh/delphi-clean.Integration.Tests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ Describe 'delphi-clean.ps1 integration tests' {
6666
@($result.Items).Count | Should -BeGreaterThan 0
6767
}
6868

69+
It 'suppresses WhatIf host output in JSON mode' {
70+
$output = @(& $script:ToolPath -RootPath $script:TempRoot -Level standard -Json -WhatIf *>&1)
71+
72+
$output.Count | Should -Be 1
73+
[string]$output[0] | Should -Not -Match '^What if:'
74+
{ [string]$output[0] | ConvertFrom-Json } | Should -Not -Throw
75+
}
76+
6977
It 'JSON output includes DurationMs as a non-negative integer' {
7078
$jsonText = & $script:ToolPath -RootPath $script:TempRoot -Level standard -Json -WhatIf
7179
$result = $jsonText | ConvertFrom-Json

0 commit comments

Comments
 (0)