Private/Write-KadenJsonAtomic.ps1
|
function Write-KadenJsonAtomic { <# .SYNOPSIS Write JSON to a file via a temporary sibling so readers never see a partial document. #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$LiteralPath, [Parameter(Mandatory = $true)] $Object ) $dir = Split-Path -Parent $LiteralPath if (-not (Test-Path -LiteralPath $dir)) { New-Item -ItemType Directory -Path $dir -Force | Out-Null } $temp = Join-Path $dir ([System.IO.Path]::GetFileName($LiteralPath) + '.tmp-' + [guid]::NewGuid().ToString('N')) $json = $Object | ConvertTo-Json -Depth 30 [System.IO.File]::WriteAllText($temp, $json) Move-Item -LiteralPath $temp -Destination $LiteralPath -Force } |