|
| 1 | +import { theme } from "../../../packages/terminal-core/src/theme.js"; |
| 2 | +import type { UpdateChannel } from "../../infra/update-channels.js"; |
| 3 | +import { canResolveRegistryVersionForPackageTarget } from "../../infra/update-global.js"; |
| 4 | +import type { UpdateRunResult } from "../../infra/update-runner.js"; |
| 5 | +import { defaultRuntime } from "../../runtime.js"; |
| 6 | +import type { OpenClawDatabaseSchemaPreflight } from "../../state/openclaw-database-preflight.js"; |
| 7 | +import { resolveGlobalManager } from "./shared.js"; |
| 8 | +import { formatSchemaRefusalLines, hasSchemaRefusal } from "./update-command-git.js"; |
| 9 | +import type { ManagedServiceRootRedirect } from "./update-command-service.js"; |
| 10 | + |
| 11 | +type UpdateDryRunPreview = { |
| 12 | + dryRun: true; |
| 13 | + root: string; |
| 14 | + installKind: "git" | "package" | "unknown"; |
| 15 | + mode: UpdateRunResult["mode"]; |
| 16 | + updateInstallKind: "git" | "package" | "unknown"; |
| 17 | + switchToGit: boolean; |
| 18 | + switchToPackage: boolean; |
| 19 | + restart: boolean; |
| 20 | + requestedChannel: UpdateChannel | null; |
| 21 | + storedChannel: UpdateChannel | null; |
| 22 | + effectiveChannel: UpdateChannel; |
| 23 | + tag: string; |
| 24 | + currentVersion: string | null; |
| 25 | + targetVersion: string | null; |
| 26 | + downgradeRisk: boolean; |
| 27 | + actions: string[]; |
| 28 | + notes: string[]; |
| 29 | +}; |
| 30 | + |
| 31 | +function printDryRunPreview(preview: UpdateDryRunPreview, jsonMode: boolean): void { |
| 32 | + if (jsonMode) { |
| 33 | + defaultRuntime.writeJson(preview); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + defaultRuntime.log(theme.heading("Update dry-run")); |
| 38 | + defaultRuntime.log(theme.muted("No changes were applied.")); |
| 39 | + defaultRuntime.log(""); |
| 40 | + defaultRuntime.log(` Root: ${theme.muted(preview.root)}`); |
| 41 | + defaultRuntime.log(` Install kind: ${theme.muted(preview.installKind)}`); |
| 42 | + defaultRuntime.log(` Mode: ${theme.muted(preview.mode)}`); |
| 43 | + defaultRuntime.log(` Channel: ${theme.muted(preview.effectiveChannel)}`); |
| 44 | + defaultRuntime.log(` Tag/spec: ${theme.muted(preview.tag)}`); |
| 45 | + if (preview.currentVersion) { |
| 46 | + defaultRuntime.log(` Current version: ${theme.muted(preview.currentVersion)}`); |
| 47 | + } |
| 48 | + if (preview.targetVersion) { |
| 49 | + defaultRuntime.log(` Target version: ${theme.muted(preview.targetVersion)}`); |
| 50 | + } |
| 51 | + if (preview.downgradeRisk) { |
| 52 | + defaultRuntime.log(theme.warn(" Downgrade confirmation would be required in a real run.")); |
| 53 | + } |
| 54 | + |
| 55 | + defaultRuntime.log(""); |
| 56 | + defaultRuntime.log(theme.heading("Planned actions:")); |
| 57 | + for (const action of preview.actions) { |
| 58 | + defaultRuntime.log(` - ${action}`); |
| 59 | + } |
| 60 | + |
| 61 | + if (preview.notes.length > 0) { |
| 62 | + defaultRuntime.log(""); |
| 63 | + defaultRuntime.log(theme.heading("Notes:")); |
| 64 | + for (const note of preview.notes) { |
| 65 | + defaultRuntime.log(` - ${theme.muted(note)}`); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +export async function printUpdateDryRun(params: { |
| 71 | + root: string; |
| 72 | + installKind: "git" | "package" | "unknown"; |
| 73 | + updateInstallKind: "git" | "package" | "unknown"; |
| 74 | + switchToGit: boolean; |
| 75 | + switchToPackage: boolean; |
| 76 | + shouldRestart: boolean; |
| 77 | + requestedChannel: UpdateChannel | null; |
| 78 | + storedChannel: UpdateChannel | null; |
| 79 | + channel: UpdateChannel; |
| 80 | + tag: string; |
| 81 | + packageInstallSpec: string | null; |
| 82 | + currentVersion: string | null; |
| 83 | + targetVersion: string | null; |
| 84 | + downgradeRisk: boolean; |
| 85 | + packageAlreadyCurrent: boolean; |
| 86 | + fallbackToLatest: boolean; |
| 87 | + managedServiceRootRedirect: ManagedServiceRootRedirect | null; |
| 88 | + explicitTag: string | null; |
| 89 | + packageSchemaPreflight: OpenClawDatabaseSchemaPreflight; |
| 90 | + timeoutMs: number; |
| 91 | + opts: { tag?: string; json?: boolean }; |
| 92 | +}): Promise<void> { |
| 93 | + let mode: UpdateRunResult["mode"] = "unknown"; |
| 94 | + if (params.updateInstallKind === "git") { |
| 95 | + mode = "git"; |
| 96 | + } else if (params.updateInstallKind === "package") { |
| 97 | + mode = await resolveGlobalManager({ |
| 98 | + root: params.root, |
| 99 | + installKind: params.installKind, |
| 100 | + timeoutMs: params.timeoutMs, |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + const actions: string[] = []; |
| 105 | + if (params.requestedChannel && params.requestedChannel !== params.storedChannel) { |
| 106 | + actions.push(`Persist update.channel=${params.requestedChannel} in config`); |
| 107 | + } |
| 108 | + if (params.switchToGit) { |
| 109 | + actions.push("Switch install mode from package to git checkout (dev channel)"); |
| 110 | + } else if (params.switchToPackage) { |
| 111 | + actions.push(`Switch install mode from git to package manager (${mode})`); |
| 112 | + } else if (params.updateInstallKind === "git") { |
| 113 | + actions.push(`Run git update flow on channel ${params.channel} (fetch/rebase/build/doctor)`); |
| 114 | + } else if (params.packageAlreadyCurrent) { |
| 115 | + actions.push( |
| 116 | + `Refresh package install with spec ${params.packageInstallSpec ?? params.tag}; current version already matches ${params.targetVersion}`, |
| 117 | + ); |
| 118 | + } else { |
| 119 | + actions.push( |
| 120 | + `Run global package manager update with spec ${params.packageInstallSpec ?? params.tag}`, |
| 121 | + ); |
| 122 | + } |
| 123 | + actions.push("Run plugin update sync after core update"); |
| 124 | + actions.push("Refresh shell completion cache (if needed)"); |
| 125 | + actions.push( |
| 126 | + params.shouldRestart |
| 127 | + ? "Restart gateway service and run doctor checks" |
| 128 | + : "Skip restart (because --no-restart is set)", |
| 129 | + ); |
| 130 | + |
| 131 | + const notes: string[] = []; |
| 132 | + if (params.opts.tag && params.updateInstallKind === "git") { |
| 133 | + notes.push("--tag applies to npm installs only; git updates ignore it."); |
| 134 | + } |
| 135 | + if (params.fallbackToLatest) { |
| 136 | + notes.push("Beta channel resolves to latest for this run (fallback)."); |
| 137 | + } |
| 138 | + if (params.managedServiceRootRedirect) { |
| 139 | + notes.push( |
| 140 | + `Package update targets managed service root ${params.managedServiceRootRedirect.root} instead of invoking root ${params.managedServiceRootRedirect.previousRoot}.`, |
| 141 | + ); |
| 142 | + } |
| 143 | + if (params.explicitTag && !canResolveRegistryVersionForPackageTarget(params.tag)) { |
| 144 | + notes.push("Non-registry package specs skip npm version lookup and downgrade previews."); |
| 145 | + } |
| 146 | + if (hasSchemaRefusal(params.packageSchemaPreflight)) { |
| 147 | + notes.push(...formatSchemaRefusalLines(params.packageSchemaPreflight, true)); |
| 148 | + } |
| 149 | + if (params.updateInstallKind === "git") { |
| 150 | + // The git target revision is resolved inside the real update run, so its |
| 151 | + // schema support cannot be previewed here without duplicating that flow. |
| 152 | + notes.push( |
| 153 | + "Database schema compatibility of the git target is verified during the real update; this preview does not check it.", |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + printDryRunPreview( |
| 158 | + { |
| 159 | + dryRun: true, |
| 160 | + root: params.root, |
| 161 | + installKind: params.installKind, |
| 162 | + mode, |
| 163 | + updateInstallKind: params.updateInstallKind, |
| 164 | + switchToGit: params.switchToGit, |
| 165 | + switchToPackage: params.switchToPackage, |
| 166 | + restart: params.shouldRestart, |
| 167 | + requestedChannel: params.requestedChannel, |
| 168 | + storedChannel: params.storedChannel, |
| 169 | + effectiveChannel: params.channel, |
| 170 | + tag: params.packageInstallSpec ?? params.tag, |
| 171 | + currentVersion: params.currentVersion, |
| 172 | + targetVersion: params.targetVersion, |
| 173 | + downgradeRisk: params.downgradeRisk, |
| 174 | + actions, |
| 175 | + notes, |
| 176 | + }, |
| 177 | + Boolean(params.opts.json), |
| 178 | + ); |
| 179 | +} |
0 commit comments