param( [string]$Version ) $ErrorActionPreference = 'Stop' $dshRepoRoot = Split-Path -Parent $PSScriptRoot $dshRuntimeRoot = Join-Path $dshRepoRoot 'integration/dsh' $dshNodeCommand = Get-Command node -ErrorAction Stop $dshNodeVersion = & $dshNodeCommand.Source -p 'process.versions.node' if ([version]$dshNodeVersion -lt [version]'24.9.0') { throw 'DSH requires Node.js 24.9.0 or later.' } $dshNpm = (Get-Command npm.cmd -ErrorAction Stop).Source $dshRegistry = 'https://registry.npmjs.org/' $dshBuild = Join-Path $dshRepoRoot 'build' New-Item -ItemType Directory -Path $dshBuild -Force | Out-Null $dshInstallLock = [IO.File]::Open((Join-Path $dshBuild 'dsh-install.lock'), 'OpenOrCreate', 'ReadWrite', 'None') try { if (-not $Version) { & $dshNpm --prefix $dshRuntimeRoot ci --ignore-scripts --no-audit --no-fund if ($LASTEXITCODE -ne 0) { throw 'DSH installation failed.' } & $dshNodeCommand.Source (Join-Path $dshRuntimeRoot 'start.mjs') --doctor if ($LASTEXITCODE -ne 0) { throw 'DSH runtime verification failed.' } return } if ($Version -cnotmatch '\A(?:latest|\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)\z') { throw 'Use latest or an exact version, for example 0.2.2-rc.5.' } # Keep the candidate and previous installation on the same volume for rename. $dshStage = Join-Path $dshBuild ('dsh-update-' + [Guid]::NewGuid().ToString('N')) $dshCandidate = Join-Path $dshStage 'candidate' $dshPrevious = Join-Path $dshStage 'previous' $dshEntries = @('package.json', 'package-lock.json', 'node_modules') $dshBackedUp = @() $dshPublished = @() New-Item -ItemType Directory -Path $dshCandidate, $dshPrevious -Force | Out-Null try { Copy-Item -LiteralPath @( (Join-Path $dshRuntimeRoot 'package.json') (Join-Path $dshRuntimeRoot 'package-lock.json') (Join-Path $dshRuntimeRoot 'start.mjs') (Join-Path $dshRuntimeRoot 'task-plugin') ) -Destination $dshCandidate -Recurse & $dshNpm --prefix $dshCandidate install --save-exact --ignore-scripts --no-audit --no-fund --registry=$dshRegistry "@deepseek-ai/dsh@$Version" if ($LASTEXITCODE -ne 0) { throw 'DSH candidate installation failed.' } & $dshNodeCommand.Source (Join-Path $PSScriptRoot 'verify_dsh_runtime.mjs') $dshCandidate (Join-Path $dshStage 'verification') if ($LASTEXITCODE -ne 0) { throw 'DSH candidate failed the runtime check. The previous runtime is still available.' } foreach ($dshEntry in $dshEntries) { $dshLive = Join-Path $dshRuntimeRoot $dshEntry if (Test-Path -LiteralPath $dshLive) { Move-Item -LiteralPath $dshLive -Destination (Join-Path $dshPrevious $dshEntry) $dshBackedUp += $dshEntry } Move-Item -LiteralPath (Join-Path $dshCandidate $dshEntry) -Destination $dshLive $dshPublished += $dshEntry } } catch { try { foreach ($dshEntry in $dshPublished) { Move-Item -LiteralPath (Join-Path $dshRuntimeRoot $dshEntry) -Destination (Join-Path $dshCandidate $dshEntry) } foreach ($dshEntry in $dshBackedUp) { Move-Item -LiteralPath (Join-Path $dshPrevious $dshEntry) -Destination (Join-Path $dshRuntimeRoot $dshEntry) } } catch { throw "DSH rollback failed. Previous files: $dshPrevious. $($_.Exception.Message)" } throw } Write-Output "DSH updated. Previous files and verification: $dshStage" } finally { $dshInstallLock.Dispose() }