mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 01:08:31 +03:00
fix(tui): show update installation progress (#49676)
This commit is contained in:
committed by
GitHub
Unverified
parent
2d0ce64111
commit
10cac9ab5d
@@ -47,7 +47,14 @@ export default Runtime.handler(Commands, (input) =>
|
||||
),
|
||||
)
|
||||
const updater = yield* Updater.Service
|
||||
const update = yield* updater.run().pipe(Effect.forkScoped)
|
||||
let installing: string | undefined
|
||||
const updateListeners = new Set<(version: string) => void>()
|
||||
const update = yield* updater
|
||||
.run((version) => {
|
||||
installing = version
|
||||
updateListeners.forEach((notify) => notify(version))
|
||||
})
|
||||
.pipe(Effect.ensuring(Effect.sync(() => (installing = undefined))), Effect.forkScoped)
|
||||
preflight.loading()
|
||||
const config = yield* Config.Service
|
||||
const npm = yield* Npm.Service
|
||||
@@ -92,7 +99,13 @@ export default Runtime.handler(Commands, (input) =>
|
||||
),
|
||||
{ signal },
|
||||
),
|
||||
check: (signal) => runPromise(Fiber.join(update).pipe(Effect.flatMap(() => updater.check())), { signal }),
|
||||
check: (signal, notify) => {
|
||||
if (installing) notify(installing)
|
||||
updateListeners.add(notify)
|
||||
return runPromise(Fiber.join(update).pipe(Effect.flatMap(() => updater.check())), { signal }).finally(() =>
|
||||
updateListeners.delete(notify),
|
||||
)
|
||||
},
|
||||
apply: (version) => runPromise(updater.apply(version)),
|
||||
},
|
||||
packages: {
|
||||
|
||||
@@ -13,7 +13,7 @@ export type RunResult = { readonly type: "available" | "installed"; readonly ver
|
||||
export type CheckResult = RunResult | { readonly type: "unavailable"; readonly message: string }
|
||||
|
||||
export interface Interface {
|
||||
readonly run: () => Effect.Effect<RunResult | undefined>
|
||||
readonly run: (onInstall?: (version: string) => void) => Effect.Effect<RunResult | undefined>
|
||||
readonly check: () => Effect.Effect<CheckResult | undefined, Error>
|
||||
readonly apply: (version: string) => Effect.Effect<void, Error>
|
||||
readonly method: () => Effect.Effect<Method | undefined>
|
||||
@@ -275,10 +275,11 @@ const make = Effect.gen(function* () {
|
||||
})
|
||||
|
||||
const run = Effect.fn("cli.updater.run")(
|
||||
function* () {
|
||||
function* (onInstall: (version: string) => void = () => {}) {
|
||||
const result = yield* inspect()
|
||||
if (!result) return undefined
|
||||
if (result.policy === "notify") return { type: "available" as const, version: result.version }
|
||||
onInstall(result.version)
|
||||
if (!(yield* install(result.version))) return yield* Effect.fail(new Error("Installation method not found"))
|
||||
return { type: "installed" as const, version: result.version }
|
||||
},
|
||||
|
||||
@@ -32,12 +32,14 @@ export function DialogUpdate(props: {
|
||||
}),
|
||||
)
|
||||
const state = createMemo(() => {
|
||||
const message = error()
|
||||
if (message) return { type: "check-failed" as const, message }
|
||||
const current = props.state()
|
||||
if (current?.type === "installing") return current
|
||||
if (check.loading) return { type: "checking" as const }
|
||||
const unavailable = check()
|
||||
if (unavailable) return { type: "unavailable" as const, message: unavailable }
|
||||
const message = error()
|
||||
if (message) return { type: "check-failed" as const, message }
|
||||
return props.state() ?? { type: "current" as const }
|
||||
return current ?? { type: "current" as const }
|
||||
})
|
||||
const buttons = createMemo(() => {
|
||||
const type = state().type
|
||||
@@ -87,9 +89,11 @@ export function DialogUpdate(props: {
|
||||
<box paddingLeft={2} paddingRight={2} gap={1}>
|
||||
<box flexDirection="row" justifyContent="space-between">
|
||||
<text attributes={TextAttributes.BOLD} fg={theme.text.default}>
|
||||
{state().type === "available" || state().type === "installing" || state().type === "failed"
|
||||
? "Update available"
|
||||
: "Update"}
|
||||
{state().type === "installing"
|
||||
? "Updating OpenCode"
|
||||
: state().type === "available" || state().type === "failed"
|
||||
? "Update available"
|
||||
: "Update"}
|
||||
</text>
|
||||
<text fg={theme.text.subdued} onMouseUp={() => dialog.clear()}>
|
||||
esc
|
||||
|
||||
@@ -20,6 +20,7 @@ export type UpdateSource = {
|
||||
readonly subscribe: (notify: (notice: ClientNotice) => void, signal: AbortSignal) => Promise<void>
|
||||
readonly check: (
|
||||
signal: AbortSignal,
|
||||
onInstall: (version: string) => void,
|
||||
) => Promise<ClientNotice | { readonly type: "unavailable"; readonly message: string } | undefined>
|
||||
readonly apply: (version: string) => Promise<void>
|
||||
}
|
||||
@@ -74,7 +75,13 @@ export const { use: useUpdateNotification, provider: UpdateNotificationProvider
|
||||
const check = async (signal: AbortSignal) => {
|
||||
const updater = props.updater
|
||||
if (!updater || state()?.type === "installing") return
|
||||
const result = await updater.check(signal)
|
||||
const result = await updater
|
||||
.check(signal, (version) => {
|
||||
if (!signal.aborted) setState({ type: "installing", version })
|
||||
})
|
||||
.finally(() => {
|
||||
if (state()?.type === "installing") setState(undefined)
|
||||
})
|
||||
if (signal.aborted) return
|
||||
if (result?.type === "unavailable") return result.message
|
||||
setState(result)
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
/** @jsxImportSource @opentui/solid */
|
||||
import { testRender } from "@opentui/solid"
|
||||
import { expect, test } from "bun:test"
|
||||
import { createSignal } from "solid-js"
|
||||
import { DialogUpdate } from "../../src/component/dialog-update"
|
||||
import { ConfigProvider } from "../../src/config"
|
||||
import type { UpdateState } from "../../src/context/update-notification"
|
||||
import { Keymap } from "../../src/context/keymap"
|
||||
import { ThemeProvider } from "../../src/context/theme"
|
||||
import { DialogProvider } from "../../src/ui/dialog"
|
||||
import { ToastProvider } from "../../src/ui/toast"
|
||||
import { emptyThemeSource, tmpdir } from "../fixture/fixture"
|
||||
import { TestTuiContexts } from "../fixture/tui-environment"
|
||||
import { createTuiResolvedConfig } from "../fixture/tui-runtime"
|
||||
|
||||
test("installation progress replaces checking while the update job is still pending", async () => {
|
||||
await using temporary = await tmpdir()
|
||||
const [state, setState] = createSignal<UpdateState>()
|
||||
const pending = Promise.withResolvers<string | undefined>()
|
||||
const app = await testRender(
|
||||
() => (
|
||||
<TestTuiContexts directory={temporary.path} paths={{ state: temporary.path }}>
|
||||
<ConfigProvider config={createTuiResolvedConfig()}>
|
||||
<ThemeProvider mode="dark" source={emptyThemeSource}>
|
||||
<Keymap.Provider>
|
||||
<ToastProvider>
|
||||
<DialogProvider>
|
||||
<DialogUpdate
|
||||
check={() => pending.promise}
|
||||
state={state}
|
||||
skip={() => {}}
|
||||
install={() => Promise.resolve()}
|
||||
restart={() => {}}
|
||||
/>
|
||||
</DialogProvider>
|
||||
</ToastProvider>
|
||||
</Keymap.Provider>
|
||||
</ThemeProvider>
|
||||
</ConfigProvider>
|
||||
</TestTuiContexts>
|
||||
),
|
||||
{ width: 80, height: 24, kittyKeyboard: true },
|
||||
)
|
||||
|
||||
try {
|
||||
app.renderer.start()
|
||||
await app.waitForFrame((frame) => frame.includes("Checking for updates"))
|
||||
setState({ type: "installing", version: "2.0.0" })
|
||||
await app.waitForFrame(
|
||||
(frame) =>
|
||||
frame.includes("Updating OpenCode") &&
|
||||
frame.includes("Installing OpenCode 2.0.0") &&
|
||||
!frame.includes("Checking"),
|
||||
)
|
||||
expect(app.captureCharFrame()).not.toContain("Skip")
|
||||
pending.reject(new Error("Update service unavailable"))
|
||||
await app.waitForFrame((frame) => frame.includes("Update service unavailable") && !frame.includes("Installing"))
|
||||
} finally {
|
||||
pending.resolve(undefined)
|
||||
app.renderer.destroy()
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user