feat(update): add progress spinner during update steps

This commit is contained in:
Benjamin Jesuiter
2026-01-11 00:48:39 +01:00
committed by Peter Steinberger
parent 920436da65
commit 88c404bcfc
2 changed files with 202 additions and 88 deletions

View File

@@ -4,10 +4,13 @@ import { resolveClawdbotPackageRoot } from "../infra/clawdbot-root.js";
import { import {
runGatewayUpdate, runGatewayUpdate,
type UpdateRunResult, type UpdateRunResult,
type UpdateStepInfo,
type UpdateStepProgress,
} from "../infra/update-runner.js"; } from "../infra/update-runner.js";
import { defaultRuntime } from "../runtime.js"; import { defaultRuntime } from "../runtime.js";
import { formatDocsLink } from "../terminal/links.js"; import { formatDocsLink } from "../terminal/links.js";
import { theme } from "../terminal/theme.js"; import { theme } from "../terminal/theme.js";
import { createCliProgress, type ProgressReporter } from "./progress.js";
export type UpdateCommandOptions = { export type UpdateCommandOptions = {
json?: boolean; json?: boolean;
@@ -15,6 +18,46 @@ export type UpdateCommandOptions = {
timeout?: string; timeout?: string;
}; };
const STEP_LABELS: Record<string, string> = {
"git status": "Checking for uncommitted changes",
"git upstream": "Checking upstream branch",
"git fetch": "Fetching latest changes",
"git rebase": "Rebasing onto upstream",
"deps install": "Installing dependencies",
build: "Building",
"ui:build": "Building UI",
"clawdbot doctor": "Running doctor checks",
"git rev-parse HEAD (after)": "Verifying update",
};
function getStepLabel(step: UpdateStepInfo): string {
const friendlyLabel = STEP_LABELS[step.name] ?? step.name;
const commandHint = step.command.startsWith("git ")
? step.command.split(" ").slice(0, 2).join(" ")
: step.command.split(" ")[0];
return `${friendlyLabel} (${commandHint})`;
}
function createUpdateProgress(enabled: boolean): {
progress: UpdateStepProgress;
reporter: ProgressReporter;
} {
const reporter = createCliProgress({
label: "Preparing update...",
indeterminate: true,
enabled,
delayMs: 0,
});
const progress: UpdateStepProgress = {
onStepStart: (step) => {
reporter.setLabel(getStepLabel(step));
},
};
return { progress, reporter };
}
function formatDuration(ms: number): string { function formatDuration(ms: number): string {
if (ms < 1000) return `${ms}ms`; if (ms < 1000) return `${ms}ms`;
const seconds = (ms / 1000).toFixed(1); const seconds = (ms / 1000).toFixed(1);
@@ -99,6 +142,8 @@ export async function updateCommand(opts: UpdateCommandOptions): Promise<void> {
return; return;
} }
const showProgress = !opts.json && process.stderr.isTTY;
if (!opts.json) { if (!opts.json) {
defaultRuntime.log(theme.heading("Updating Clawdbot...")); defaultRuntime.log(theme.heading("Updating Clawdbot..."));
defaultRuntime.log(""); defaultRuntime.log("");
@@ -111,12 +156,17 @@ export async function updateCommand(opts: UpdateCommandOptions): Promise<void> {
cwd: process.cwd(), cwd: process.cwd(),
})) ?? process.cwd(); })) ?? process.cwd();
const { progress, reporter } = createUpdateProgress(showProgress);
const result = await runGatewayUpdate({ const result = await runGatewayUpdate({
cwd: root, cwd: root,
argv1: process.argv[1], argv1: process.argv[1],
timeoutMs, timeoutMs,
progress,
}); });
reporter.done();
printResult(result, opts); printResult(result, opts);
if (result.status === "error") { if (result.status === "error") {

View File

@@ -30,11 +30,26 @@ type CommandRunner = (
options: CommandOptions, options: CommandOptions,
) => Promise<{ stdout: string; stderr: string; code: number | null }>; ) => Promise<{ stdout: string; stderr: string; code: number | null }>;
export type UpdateStepInfo = {
name: string;
command: string;
index: number;
total: number;
};
export type UpdateStepProgress = {
onStepStart?: (step: UpdateStepInfo) => void;
onStepComplete?: (
step: UpdateStepInfo & { durationMs: number; exitCode: number | null },
) => void;
};
type UpdateRunnerOptions = { type UpdateRunnerOptions = {
cwd?: string; cwd?: string;
argv1?: string; argv1?: string;
timeoutMs?: number; timeoutMs?: number;
runCommand?: CommandRunner; runCommand?: CommandRunner;
progress?: UpdateStepProgress;
}; };
const DEFAULT_TIMEOUT_MS = 20 * 60_000; const DEFAULT_TIMEOUT_MS = 20 * 60_000;
@@ -142,20 +157,54 @@ async function detectPackageManager(root: string) {
return "npm"; return "npm";
} }
async function runStep( type RunStepOptions = {
runCommand: CommandRunner, runCommand: CommandRunner;
name: string, name: string;
argv: string[], argv: string[];
cwd: string, cwd: string;
timeoutMs: number, timeoutMs: number;
env?: NodeJS.ProcessEnv, env?: NodeJS.ProcessEnv;
): Promise<UpdateStepResult> { progress?: UpdateStepProgress;
stepIndex: number;
totalSteps: number;
};
async function runStep(opts: RunStepOptions): Promise<UpdateStepResult> {
const {
runCommand,
name,
argv,
cwd,
timeoutMs,
env,
progress,
stepIndex,
totalSteps,
} = opts;
const command = argv.join(" ");
const stepInfo: UpdateStepInfo = {
name,
command,
index: stepIndex,
total: totalSteps,
};
progress?.onStepStart?.(stepInfo);
const started = Date.now(); const started = Date.now();
const result = await runCommand(argv, { cwd, timeoutMs, env }); const result = await runCommand(argv, { cwd, timeoutMs, env });
const durationMs = Date.now() - started; const durationMs = Date.now() - started;
progress?.onStepComplete?.({
...stepInfo,
durationMs,
exitCode: result.code,
});
return { return {
name, name,
command: argv.join(" "), command,
cwd, cwd,
durationMs, durationMs,
exitCode: result.code, exitCode: result.code,
@@ -181,6 +230,9 @@ function managerInstallArgs(manager: "pnpm" | "bun" | "npm") {
return ["npm", "install"]; return ["npm", "install"];
} }
// Total number of visible steps in a successful git update flow
const GIT_UPDATE_TOTAL_STEPS = 9;
export async function runGatewayUpdate( export async function runGatewayUpdate(
opts: UpdateRunnerOptions = {}, opts: UpdateRunnerOptions = {},
): Promise<UpdateRunResult> { ): Promise<UpdateRunResult> {
@@ -192,9 +244,33 @@ export async function runGatewayUpdate(
return { stdout: res.stdout, stderr: res.stderr, code: res.code }; return { stdout: res.stdout, stderr: res.stderr, code: res.code };
}); });
const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS; const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
const progress = opts.progress;
const steps: UpdateStepResult[] = []; const steps: UpdateStepResult[] = [];
const candidates = buildStartDirs(opts); const candidates = buildStartDirs(opts);
let stepIndex = 0;
const step = (
name: string,
argv: string[],
cwd: string,
env?: NodeJS.ProcessEnv,
): RunStepOptions => {
const currentIndex = stepIndex;
stepIndex += 1;
return {
runCommand,
name,
argv,
cwd,
timeoutMs,
env,
progress,
stepIndex: currentIndex,
totalSteps: GIT_UPDATE_TOTAL_STEPS,
};
};
const pkgRoot = await findPackageRoot(candidates); const pkgRoot = await findPackageRoot(candidates);
let gitRoot = await resolveGitRoot(runCommand, candidates, timeoutMs); let gitRoot = await resolveGitRoot(runCommand, candidates, timeoutMs);
@@ -214,23 +290,20 @@ export async function runGatewayUpdate(
} }
if (gitRoot && pkgRoot && path.resolve(gitRoot) === path.resolve(pkgRoot)) { if (gitRoot && pkgRoot && path.resolve(gitRoot) === path.resolve(pkgRoot)) {
const beforeSha = ( // Get current SHA (not a visible step, no progress)
await runStep( const beforeShaResult = await runCommand(
runCommand, ["git", "-C", gitRoot, "rev-parse", "HEAD"],
"git rev-parse HEAD", { cwd: gitRoot, timeoutMs },
["git", "-C", gitRoot, "rev-parse", "HEAD"], );
gitRoot, const beforeSha = beforeShaResult.stdout.trim() || null;
timeoutMs,
)
).stdoutTail?.trim();
const beforeVersion = await readPackageVersion(gitRoot); const beforeVersion = await readPackageVersion(gitRoot);
const statusStep = await runStep( const statusStep = await runStep(
runCommand, step(
"git status", "git status",
["git", "-C", gitRoot, "status", "--porcelain"], ["git", "-C", gitRoot, "status", "--porcelain"],
gitRoot, gitRoot,
timeoutMs, ),
); );
steps.push(statusStep); steps.push(statusStep);
if ((statusStep.stdoutTail ?? "").trim()) { if ((statusStep.stdoutTail ?? "").trim()) {
@@ -239,26 +312,26 @@ export async function runGatewayUpdate(
mode: "git", mode: "git",
root: gitRoot, root: gitRoot,
reason: "dirty", reason: "dirty",
before: { sha: beforeSha ?? null, version: beforeVersion }, before: { sha: beforeSha, version: beforeVersion },
steps, steps,
durationMs: Date.now() - startedAt, durationMs: Date.now() - startedAt,
}; };
} }
const upstreamStep = await runStep( const upstreamStep = await runStep(
runCommand, step(
"git upstream", "git upstream",
[ [
"git", "git",
"-C", "-C",
gitRoot,
"rev-parse",
"--abbrev-ref",
"--symbolic-full-name",
"@{upstream}",
],
gitRoot, gitRoot,
"rev-parse", ),
"--abbrev-ref",
"--symbolic-full-name",
"@{upstream}",
],
gitRoot,
timeoutMs,
); );
steps.push(upstreamStep); steps.push(upstreamStep);
if (upstreamStep.exitCode !== 0) { if (upstreamStep.exitCode !== 0) {
@@ -267,7 +340,7 @@ export async function runGatewayUpdate(
mode: "git", mode: "git",
root: gitRoot, root: gitRoot,
reason: "no-upstream", reason: "no-upstream",
before: { sha: beforeSha ?? null, version: beforeVersion }, before: { sha: beforeSha, version: beforeVersion },
steps, steps,
durationMs: Date.now() - startedAt, durationMs: Date.now() - startedAt,
}; };
@@ -275,89 +348,80 @@ export async function runGatewayUpdate(
steps.push( steps.push(
await runStep( await runStep(
runCommand, step(
"git fetch", "git fetch",
["git", "-C", gitRoot, "fetch", "--all", "--prune"], ["git", "-C", gitRoot, "fetch", "--all", "--prune"],
gitRoot, gitRoot,
timeoutMs, ),
), ),
); );
const rebaseStep = await runStep( const rebaseStep = await runStep(
runCommand, step(
"git rebase", "git rebase",
["git", "-C", gitRoot, "rebase", "@{upstream}"], ["git", "-C", gitRoot, "rebase", "@{upstream}"],
gitRoot, gitRoot,
timeoutMs, ),
); );
steps.push(rebaseStep); steps.push(rebaseStep);
if (rebaseStep.exitCode !== 0) { if (rebaseStep.exitCode !== 0) {
steps.push( // Abort rebase (error recovery, not counted in total)
await runStep( const abortResult = await runCommand(
runCommand, ["git", "-C", gitRoot, "rebase", "--abort"],
"git rebase --abort", { cwd: gitRoot, timeoutMs },
["git", "-C", gitRoot, "rebase", "--abort"],
gitRoot,
timeoutMs,
),
); );
steps.push({
name: "git rebase --abort",
command: "git rebase --abort",
cwd: gitRoot,
durationMs: 0,
exitCode: abortResult.code,
stdoutTail: trimLogTail(abortResult.stdout, MAX_LOG_CHARS),
stderrTail: trimLogTail(abortResult.stderr, MAX_LOG_CHARS),
});
return { return {
status: "error", status: "error",
mode: "git", mode: "git",
root: gitRoot, root: gitRoot,
reason: "rebase-failed", reason: "rebase-failed",
before: { sha: beforeSha ?? null, version: beforeVersion }, before: { sha: beforeSha, version: beforeVersion },
steps, steps,
durationMs: Date.now() - startedAt, durationMs: Date.now() - startedAt,
}; };
} }
const manager = await detectPackageManager(gitRoot); const manager = await detectPackageManager(gitRoot);
steps.push(
await runStep(step("deps install", managerInstallArgs(manager), gitRoot)),
);
steps.push( steps.push(
await runStep( await runStep(
runCommand, step("build", managerScriptArgs(manager, "build"), gitRoot),
"deps install",
managerInstallArgs(manager),
gitRoot,
timeoutMs,
), ),
); );
steps.push( steps.push(
await runStep( await runStep(
runCommand, step("ui:build", managerScriptArgs(manager, "ui:build"), gitRoot),
"build",
managerScriptArgs(manager, "build"),
gitRoot,
timeoutMs,
), ),
); );
steps.push( steps.push(
await runStep( await runStep(
runCommand, step(
"ui:build", "clawdbot doctor",
managerScriptArgs(manager, "ui:build"), managerScriptArgs(manager, "clawdbot", ["doctor"]),
gitRoot, gitRoot,
timeoutMs, { CLAWDBOT_UPDATE_IN_PROGRESS: "1" },
), ),
);
steps.push(
await runStep(
runCommand,
"clawdbot doctor",
managerScriptArgs(manager, "clawdbot", ["doctor"]),
gitRoot,
timeoutMs,
{ CLAWDBOT_UPDATE_IN_PROGRESS: "1" },
), ),
); );
const failedStep = steps.find((step) => step.exitCode !== 0); const failedStep = steps.find((s) => s.exitCode !== 0);
const afterShaStep = await runStep( const afterShaStep = await runStep(
runCommand, step(
"git rev-parse HEAD (after)", "git rev-parse HEAD (after)",
["git", "-C", gitRoot, "rev-parse", "HEAD"], ["git", "-C", gitRoot, "rev-parse", "HEAD"],
gitRoot, gitRoot,
timeoutMs, ),
); );
steps.push(afterShaStep); steps.push(afterShaStep);
const afterVersion = await readPackageVersion(gitRoot); const afterVersion = await readPackageVersion(gitRoot);
@@ -367,7 +431,7 @@ export async function runGatewayUpdate(
mode: "git", mode: "git",
root: gitRoot, root: gitRoot,
reason: failedStep ? failedStep.name : undefined, reason: failedStep ? failedStep.name : undefined,
before: { sha: beforeSha ?? null, version: beforeVersion }, before: { sha: beforeSha, version: beforeVersion },
after: { after: {
sha: afterShaStep.stdoutTail?.trim() ?? null, sha: afterShaStep.stdoutTail?.trim() ?? null,
version: afterVersion, version: afterVersion,