feat(app): simplify the browse setup card states

This commit is contained in:
Aarav Sareen 2026-11-17 17:42:13 +05:30
parent 18af04509b
commit 22c6ed6575
11 changed files with 330 additions and 131 deletions

View file

@ -1029,21 +1029,22 @@ export const dict = {
"settings.extensions.browse.name": "Browse",
"settings.extensions.browse.description": "Let OpenCode work in your browser with your tabs, logins, and extensions.",
"settings.extensions.browse.install": "Install",
"settings.extensions.browse.installing": "Installing browse…",
"settings.extensions.browse.waitingExtension": "Add browse to your browser from the Chrome Web Store to finish",
"settings.extensions.browse.openStore": "Open Chrome Web Store",
"settings.extensions.browse.finish": "Finish setup",
"settings.extensions.browse.connect": "Connect",
"settings.extensions.browse.update": "Update",
"settings.extensions.browse.openAgain": "Open again",
"settings.extensions.browse.installing": "Installing Browse…",
"settings.extensions.browse.waitingExtension": "Add Browse in the browser tab that just opened",
"settings.extensions.browse.needsUpdate": "Needs an update",
"settings.extensions.browse.notConnectedOpencode": "Not connected to OpenCode",
"settings.extensions.browse.notConnectedBrowser": "Not connected to your browser",
"settings.extensions.browse.connected": "Connected to your browser",
"settings.extensions.browse.installed": "Installed. Connects when an agent first uses it",
"settings.extensions.browse.failed.start": "Could not start the browse installer",
"settings.extensions.browse.failed.download": "Could not download the browse installer",
"settings.extensions.browse.failed.timeout": "The browse installer took too long",
"settings.extensions.browse.failed.installer": "The browse installer failed",
"settings.extensions.browse.unavailable": "Could not check whether browse is installed",
"settings.extensions.browse.incomplete.config": "Installed, but not added to OpenCode yet",
"settings.extensions.browse.incomplete.extension": "Add the browse extension to your browser",
"settings.extensions.browse.incomplete.pairing": "The browse extension isn't paired yet",
"settings.extensions.browse.incomplete.update": "The browse extension needs an update",
"settings.extensions.browse.failed": "Couldn't install Browse",
"settings.extensions.browse.stalled": "Browse didn't connect to your browser",
"settings.extensions.browse.pairManually": "Pair manually",
"settings.extensions.browse.seeError": "See error",
"settings.extensions.browse.hideError": "Hide error",
"settings.extensions.browse.copyError": "Copy",
"settings.extensions.browse.copyFailed": "Couldn't copy the error",
"settings.extensions.plugins.loadFailed": "Could not load plugins",
"settings.extensions.sources.loadFailed": "Could not load extension sources",
"settings.extensions.console.temporary":

View file

@ -160,10 +160,14 @@ export type BrowseStatus = {
job: { status: "idle" } | { status: "installing" } | { status: "failed"; failure: BrowseInstallFailure }
}
/** `start`: the desktop didn't take the request. `installer`: browse's installer failed; `detail` is its own words. */
/**
* `start`: the desktop didn't take the request. `installer`: browse's installer failed; `detail` is its own words.
* `output` is the end of what the installer (or the download) printed, for the user to read or copy.
*/
export type BrowseInstallFailure = {
reason: "start" | "download" | "timeout" | "installer"
detail?: string
output?: string
}
export type BrowseBridge = {

View file

@ -60,7 +60,7 @@ describe("browse card machine", () => {
{ type: "read", attempt: 4 },
{ type: "sync-mcps" },
])
expect(browseCardView(run.state)).toEqual({ phase: "ready", connected: true })
expect(browseCardView(run.state)).toEqual({ phase: "ready" })
expect(browseCardPolling(run.state)).toBe(false)
})
@ -102,21 +102,30 @@ describe("browse card machine", () => {
{ type: "install.failed", attempt: 2 },
])
expect(browseCardView(run.state)).toEqual({ phase: "absent", failure: { reason: "start" } })
expect(browseCardView(run.state)).toEqual({ phase: "failed", failure: { reason: "start" } })
expect(transitionBrowseCard(run.state, { type: "install.requested" }).effects).toEqual([
{ type: "install", attempt: 3 },
])
})
test.each([
{ browse: status({ installed: true }), view: { phase: "incomplete", reason: "config", store: false } },
{ browse: status(), view: { phase: "setup", step: "install" } },
{ browse: status({ installed: true }), view: { phase: "setup", step: "opencode" } },
{
browse: status({ installed: true, configured: true, extension: "not-seen" }),
view: { phase: "incomplete", reason: "extension", store: true },
view: { phase: "setup", step: "browser" },
},
{
browse: status({ installed: true, configured: true, extension: "refused" }),
view: { phase: "incomplete", reason: "pairing", store: false },
view: { phase: "setup", step: "browser" },
},
{
browse: status({ installed: true, configured: true, extension: "incompatible" }),
view: { phase: "setup", step: "update" },
},
{
browse: status({ job: { status: "failed", failure: { reason: "download" } } }),
view: { phase: "failed", failure: { reason: "download" } },
},
{
browse: status({
@ -125,18 +134,14 @@ describe("browse card machine", () => {
extension: "not-seen",
job: { status: "failed", failure: { reason: "installer", detail: "Not connected yet" } },
}),
view: {
phase: "incomplete",
reason: "extension",
store: true,
failure: { reason: "installer", detail: "Not connected yet" },
},
view: { phase: "stalled" },
},
{ browse: status({ installed: true, configured: true }), view: { phase: "ready", connected: false } },
{
browse: status({ installed: true, configured: true, extension: "paired-offline" }),
view: { phase: "ready", connected: false },
browse: status({ installed: true, job: { status: "failed", failure: { reason: "installer" } } }),
view: { phase: "setup", step: "opencode" },
},
{ browse: status({ installed: true, configured: true }), view: { phase: "ready" } },
{ browse: status({ installed: true, configured: true, extension: "paired-offline" }), view: { phase: "ready" } },
])("view of $browse", ({ browse, view }) => {
expect(browseCardView(replay([{ type: "read.succeeded", attempt: 1, browse }]).state)).toEqual(view)
})

View file

@ -33,17 +33,22 @@ export type BrowseCardTransition = { readonly state: BrowseCardState; readonly e
export type BrowseCardView =
| { readonly phase: "checking" }
| { readonly phase: "unavailable" }
| { readonly phase: "absent"; readonly failure?: BrowseInstallFailure }
/**
* Not working yet. Every step runs the installer, which does whatever is left: `opencode` is browse installed but
* missing from OpenCode's config, `browser` its extension not added or not paired.
*/
| { readonly phase: "setup"; readonly step: BrowseSetupStep }
| { readonly phase: "installing"; readonly store: boolean }
| {
readonly phase: "incomplete"
readonly reason: BrowseIncomplete
readonly store: boolean
readonly failure?: BrowseInstallFailure
}
| { readonly phase: "ready"; readonly connected: boolean }
/**
* The installer gave up waiting for the extension (or failed some other way after installing browse) and the
* extension still isn't connected. Running it again fixes most of these; the rest pair by hand.
*/
| { readonly phase: "stalled" }
/** The installer failed before browse was on this computer. */
| { readonly phase: "failed"; readonly failure: BrowseInstallFailure }
| { readonly phase: "ready" }
export type BrowseIncomplete = "config" | "extension" | "pairing" | "update"
export type BrowseSetupStep = "install" | "opencode" | "browser" | "update"
/**
* Whether the `browse_setup` flag is on, resolved as the server resolves flags: `experimental.browse_setup` from the
@ -152,15 +157,17 @@ export function browseCardView(state: BrowseCardState): BrowseCardView {
if (browse.job.status === "installing") return { phase: "installing", store: browse.extension === "not-seen" }
const failure = browse.job.status === "failed" ? browse.job.failure : undefined
if (!browse.installed && browse.job.status === "failed") return { phase: "failed", failure: browse.job.failure }
if (!browse.installed) return { phase: "absent", failure }
const step = setupStep(browse)
const reason = incomplete(browse)
// An installer that fails after installing browse isn't an error to show: most often its wait for the extension ran
// out. Other steps it left offer themselves again.
if (step === "browser" && browse.job.status === "failed") return { phase: "stalled" }
if (reason) return { phase: "incomplete", reason, store: browse.extension === "not-seen", failure }
if (step) return { phase: "setup", step }
return { phase: "ready", connected: browse.extension === "connected" }
return { phase: "ready" }
}
/** The row polls while the installer runs: it waits on the user to add the extension and pair it. */
@ -168,14 +175,15 @@ export function browseCardPolling(state: BrowseCardState) {
return browseCardView(state).phase === "installing"
}
function incomplete(browse: BrowseStatus): BrowseIncomplete | undefined {
if (!browse.configured) return "config"
function setupStep(browse: BrowseStatus): BrowseSetupStep | undefined {
if (!browse.installed) return "install"
if (browse.extension === "not-seen") return "extension"
if (browse.extension === "unpaired" || browse.extension === "refused") return "pairing"
if (!browse.configured) return "opencode"
if (browse.extension === "incompatible") return "update"
if (browse.extension === "not-seen" || browse.extension === "unpaired" || browse.extension === "refused")
return "browser"
}
function unchanged(state: BrowseCardState): BrowseCardTransition {

View file

@ -11,6 +11,8 @@ const states = {
waiting: { ...installed, extension: "not-seen", job: { status: "installing" } },
extension: { ...installed, extension: "not-seen", job: { status: "idle" } },
config: { installed: true, configured: false, extension: "connected", job: { status: "idle" } },
pairing: { ...installed, extension: "unpaired", job: { status: "idle" } },
update: { ...installed, extension: "incompatible", job: { status: "idle" } },
timedOut: {
...installed,
extension: "not-seen",
@ -27,24 +29,48 @@ const states = {
installed: false,
configured: false,
extension: "unknown",
job: { status: "failed", failure: { reason: "download" } },
job: {
status: "failed",
failure: { reason: "download", output: "https://opencode.ai/browse/install answered HTTP 503" },
},
},
installerFailed: {
installed: false,
configured: false,
extension: "unknown",
job: {
status: "failed",
failure: {
reason: "installer",
detail: "checksum mismatch for browse-darwin-arm64",
output: [
"Installing browse (stable) for darwin-arm64",
"Downloading https://anoma.ly/release/browse/stable/browse-darwin-arm64.gz",
" expected sha256 4f1c…9a2e",
" got 0b77…d310",
"browse: checksum mismatch for browse-darwin-arm64",
].join("\n"),
},
},
},
installed: { ...installed, extension: "unknown", job: { status: "idle" } },
connected: { ...installed, extension: "connected", job: { status: "idle" } },
} satisfies Record<string, BrowseStatus>
type StateName = keyof typeof states
// "installed" and "connected" show only after an install on this page: a browse already set up isn't offered.
// "connected" shows only after an install on this page: a browse already set up isn't offered. "timedOut" is an
// installer that gave up waiting for the extension, which offers the step that's left rather than an error.
const STATE_NAMES: StateName[][] = [
["absent"],
["installing"],
["waiting"],
["extension"],
["config"],
["extension"],
["pairing"],
["update"],
["timedOut"],
["download"],
["waiting", "installed"],
["installerFailed"],
["waiting", "connected"],
]
@ -82,4 +108,4 @@ export default {
export const States = { render: () => <BrowseStory names={STATE_NAMES} /> }
export const Narrow = { render: () => <BrowseStory names={[["waiting"], ["timedOut"], ["extension"]]} width={360} /> }
export const Narrow = { render: () => <BrowseStory names={[["installing"], ["waiting"], ["download"]]} width={360} /> }

View file

@ -1,9 +1,10 @@
import { Match, Show, Switch, createEffect, createMemo, createSignal, onCleanup, type Component } from "solid-js"
import { Button } from "@opencode/ui/button"
import { Loader } from "@opencode/ui/loader"
import { Spinner } from "@opencode/ui/spinner"
import { useLanguage } from "@/runtime/i18n/language"
import type { BrowseBridge, BrowseInstallFailure } from "@/runtime/platform/platform"
import type { BrowseBridge } from "@/runtime/platform/platform"
import { usePlatform } from "@/runtime/platform/platform"
import { showToast } from "@/shell/notifications/toast"
import "@/settings/settings.css"
import "@/shell/search-list.css"
import {
@ -13,26 +14,35 @@ import {
transitionBrowseCard,
type BrowseCardEffect,
type BrowseCardEvent,
type BrowseIncomplete,
type BrowseSetupStep,
} from "./browse-machine"
export const BROWSE_STORE_URL = "https://chromewebstore.google.com/detail/mfnicocicmmlkpjnaffgihfjhdgjkdjg"
const BROWSE_PAIRING_URL = "https://github.com/anomalyco/browse#pairing"
const POLL_MS = 2000
const failureLabels = {
start: "settings.extensions.browse.failed.start",
download: "settings.extensions.browse.failed.download",
timeout: "settings.extensions.browse.failed.timeout",
installer: "settings.extensions.browse.failed.installer",
} as const satisfies Record<BrowseInstallFailure["reason"], string>
const stepActions = {
install: "settings.extensions.browse.install",
opencode: "settings.extensions.browse.connect",
browser: "settings.extensions.browse.connect",
update: "settings.extensions.browse.update",
} as const satisfies Record<BrowseSetupStep, string>
const incompleteLabels = {
config: "settings.extensions.browse.incomplete.config",
extension: "settings.extensions.browse.incomplete.extension",
pairing: "settings.extensions.browse.incomplete.pairing",
update: "settings.extensions.browse.incomplete.update",
} as const satisfies Record<BrowseIncomplete, string>
const stepDetails = {
install: "settings.extensions.browse.description",
opencode: "settings.extensions.browse.notConnectedOpencode",
browser: "settings.extensions.browse.notConnectedBrowser",
update: "settings.extensions.browse.needsUpdate",
} as const satisfies Record<BrowseSetupStep, string>
type ActionLabel = (typeof stepActions)[BrowseSetupStep] | "common.retry"
type DetailLabel =
| (typeof stepDetails)[BrowseSetupStep]
| "settings.extensions.browse.failed"
| "settings.extensions.browse.stalled"
/** Installs and connects browse on this computer. Only the desktop's own server runs agents here. */
export const SettingsBrowse: Component<{ bridge: BrowseBridge; onInstalled: () => void }> = (props) => {
@ -83,63 +93,88 @@ export const SettingsBrowse: Component<{ bridge: BrowseBridge; onInstalled: () =
const view = createMemo(() => browseCardView(state()))
// Offered until set up: a browse already set up when the page opened is listed with the MCPs instead, while one set up
// here stays so the row shows it worked.
// here stays so the row shows it worked. A status that can't be read hides the row: it's only a recommendation.
const [offered, setOffered] = createSignal<boolean>()
createEffect(() => {
const phase = view().phase
if (offered() === undefined && phase !== "checking") setOffered(phase !== "ready")
if (offered() === undefined && phase !== "checking") setOffered(phase !== "ready" && phase !== "unavailable")
})
const install = () => dispatch({ type: "install.requested" })
// The busy button keeps the label of the one pressed, so the row doesn't shift while the installer runs.
// While the installer runs, the row keeps the button's label (for its width) and the line it showed when pressed. An
// install already running when the page opened falls back to the description.
const [pressed, setPressed] = createSignal<{ action: ActionLabel; detail: DetailLabel }>({
action: "settings.extensions.browse.install",
detail: "settings.extensions.browse.description",
})
const install = (action: ActionLabel, detail: DetailLabel) => {
setPressed({ action, detail })
dispatch({ type: "install.requested" })
}
const [errorOpen, setErrorOpen] = createSignal(false)
// The output last copied, so a new failure shows Copy again.
const [copied, setCopied] = createSignal<string>()
// A memo, so the status reads that poll or follow window focus don't count as a new failure.
const output = createMemo(() => {
const current = view()
return current.phase === "failed" ? (current.failure.output ?? current.failure.detail) : undefined
})
// A new failure starts closed.
createEffect(() => {
output()
setErrorOpen(false)
})
const copy = (text: string) =>
void (platform.writeClipboardText?.(text) ?? navigator.clipboard.writeText(text)).then(
() => setCopied(text),
(error) =>
showToast({
variant: "error",
title: language.t("settings.extensions.browse.copyFailed"),
description: error instanceof Error ? error.message : String(error),
}),
)
const store = () => platform.openExternal(`${BROWSE_STORE_URL}?utm_source=opencode-desktop&utm_campaign=settings`)
const failure = () => {
const current = view()
return current.phase === "absent" || current.phase === "incomplete" ? current.failure : undefined
}
const detail = () => {
const current = view()
const failed = failure()
if (failed) return failed.detail ?? language.t(failureLabels[failed.reason])
switch (current.phase) {
case "checking":
return language.t("settings.extensions.browse.description")
case "unavailable":
return language.t("settings.extensions.browse.unavailable")
case "absent":
return language.t("settings.extensions.browse.description")
case "setup":
return language.t(stepDetails[current.step])
case "installing":
return language.t(
current.store ? "settings.extensions.browse.waitingExtension" : "settings.extensions.browse.installing",
)
case "incomplete":
return language.t(incompleteLabels[current.reason])
return language.t(current.store ? "settings.extensions.browse.waitingExtension" : pressed().detail)
case "stalled":
return language.t("settings.extensions.browse.stalled")
case "failed":
return language.t("settings.extensions.browse.failed")
case "ready":
return language.t(
current.connected ? "settings.extensions.browse.connected" : "settings.extensions.browse.installed",
)
return language.t("settings.extensions.browse.connected")
}
}
const error = () => !!failure()
const connected = () => {
const step = () => {
const current = view()
return current.phase === "ready" && current.connected
return current.phase === "setup" ? current.step : undefined
}
const showStore = () => {
const waiting = () => {
const current = view()
return (current.phase === "installing" || current.phase === "incomplete") && current.store
return current.phase === "installing" && current.store
}
return (
@ -150,14 +185,14 @@ export const SettingsBrowse: Component<{ bridge: BrowseBridge; onInstalled: () =
<div class="settings-feature-card-text">
<span class="settings-feature-card-name">
<span class="settings-extension-name">{language.t("settings.extensions.browse.name")}</span>
<Show when={connected()}>
<Show when={view().phase === "ready"}>
<span class="mcp-status-dot" data-status="connected" aria-hidden="true" />
</Show>
</span>
<span
class="settings-feature-card-detail text-13-regular"
data-error={error() ? "" : undefined}
role={error() ? "alert" : undefined}
data-error={view().phase === "failed" ? "" : undefined}
role={view().phase === "failed" ? "alert" : undefined}
>
{detail()}
</span>
@ -165,40 +200,87 @@ export const SettingsBrowse: Component<{ bridge: BrowseBridge; onInstalled: () =
<Show when={view().phase !== "ready"}>
<div class="settings-feature-card-actions">
<Switch>
<Match when={view().phase === "checking"}>
<Loader aria-label={language.t("common.loading")} />
<Match when={step()}>
{(current) => (
<Button
size="small"
variant="neutral"
onClick={() => install(stepActions[current()], stepDetails[current()])}
>
{language.t(stepActions[current()])}
</Button>
)}
</Match>
<Match when={view().phase === "unavailable"}>
<Button size="small" variant="ghost-muted" onClick={() => dispatch({ type: "refresh.requested" })}>
<Match when={view().phase === "stalled"}>
<Button size="small" variant="ghost-muted" onClick={() => platform.openExternal(BROWSE_PAIRING_URL)}>
{language.t("settings.extensions.browse.pairManually")}
</Button>
<Button
size="small"
variant="neutral"
onClick={() => install("common.retry", "settings.extensions.browse.stalled")}
>
{language.t("common.retry")}
</Button>
</Match>
<Match when={view().phase === "absent"}>
<Button size="small" variant="neutral" onClick={install}>
{language.t(error() ? "common.retry" : "settings.extensions.browse.install")}
<Match when={view().phase === "failed"}>
<Show when={output()}>
<Button
size="small"
variant="ghost-muted"
aria-expanded={errorOpen()}
aria-controls="settings-browse-error"
onClick={() => setErrorOpen((open) => !open)}
>
{language.t(
errorOpen() ? "settings.extensions.browse.hideError" : "settings.extensions.browse.seeError",
)}
</Button>
</Show>
<Button
size="small"
variant="neutral"
onClick={() => install("common.retry", "settings.extensions.browse.failed")}
>
{language.t("common.retry")}
</Button>
</Match>
<Match when={view().phase === "installing"}>
<Show when={showStore()}>
<Button size="small" variant="neutral" onClick={store}>
{language.t("settings.extensions.browse.openStore")}
</Button>
</Show>
<Loader aria-label={language.t("settings.extensions.browse.installing")} />
</Match>
<Match when={view().phase === "incomplete"}>
<Button size="small" variant="neutral" onClick={install}>
{language.t("settings.extensions.browse.finish")}
</Button>
<Show when={showStore()}>
<Show when={waiting()}>
<Button size="small" variant="ghost-muted" onClick={store}>
{language.t("settings.extensions.browse.openStore")}
{language.t("settings.extensions.browse.openAgain")}
</Button>
</Show>
<Button
size="small"
variant="loading"
class="settings-feature-card-busy"
aria-busy="true"
aria-label={language.t("settings.extensions.browse.installing")}
>
<span class="settings-feature-card-busy-label">{language.t(pressed().action)}</span>
<Spinner width={16} />
</Button>
</Match>
</Switch>
</div>
</Show>
<Show when={errorOpen() && output()}>
{(text) => (
<div class="settings-feature-card-error" id="settings-browse-error">
<pre class="settings-feature-card-error-output">{text()}</pre>
<Button
size="small"
variant="ghost-muted"
class="settings-feature-card-error-copy"
icon={copied() === text() ? "check" : "copy"}
onClick={() => copy(text())}
>
{language.t(copied() === text() ? "common.copied" : "settings.extensions.browse.copyError")}
</Button>
</div>
)}
</Show>
</div>
</div>
</Show>

View file

@ -2344,6 +2344,54 @@
gap: 8px;
}
/* The busy button keeps the pressed button's label, hidden, so it stays that width with the loader over it. */
.settings-feature-card-busy[data-component="button-v2"] {
position: relative;
}
.settings-feature-card-busy-label {
visibility: hidden;
}
.settings-feature-card-busy [data-slot="spinner-dots"] {
position: absolute;
inset: 0;
margin: auto;
color: var(--v2-icon-icon-muted);
}
/* The installer's output, opened from the failed row, spans the card under the text and actions. */
.settings-feature-card-error {
position: relative;
flex: 1 0 100%;
min-width: 0;
/* 12px from the card's sides and bottom, inside its 16px padding. */
margin: 0 -4px -4px;
border-radius: 4px;
background: var(--v2-background-bg-layer-02);
box-shadow: inset 0 0 0 0.5px var(--v2-border-border-muted);
}
.settings-feature-card-error-output {
max-height: calc(8 * 18px + 24px);
margin: 0;
padding: 12px 72px 12px 12px;
overflow: auto;
font-family: var(--font-family-mono);
font-size: 12px;
line-height: 18px;
color: var(--v2-text-text-base);
white-space: pre-wrap;
overflow-wrap: anywhere;
user-select: text;
}
.settings-feature-card-error-copy[data-component="button-v2"] {
position: absolute;
top: 8px;
inset-inline-end: 8px;
}
/* The name keeps its own width so the source sits right after it, not against the actions. */
.settings-mcp-row .settings-extension-name {
flex: 0 1 auto;

View file

@ -14,7 +14,7 @@ import {
type BrowseInstallCommand,
type BrowseInstallEvent,
} from "./install-machine"
import { installerError } from "./installer-error"
import { installerError, installerOutput } from "./installer-error"
import { browseBinaries, readBrowseSetup } from "./status"
export interface Interface {
@ -98,11 +98,15 @@ const runInstaller = Effect.gen(function* () {
try: async () => {
const response = await net.fetch(windows ? WINDOWS_INSTALLER_URL : INSTALLER_URL)
if (!response.ok) throw new Error(`HTTP ${response.status}`)
if (!response.ok)
throw new Error(`${windows ? WINDOWS_INSTALLER_URL : INSTALLER_URL} answered HTTP ${response.status}`)
return response.text()
},
catch: (): BrowseInstallFailure => ({ reason: "download" }),
catch: (error): BrowseInstallFailure => ({
reason: "download",
...(error instanceof Error ? { output: error.message } : {}),
}),
})
if (!windows) return yield* spawnInstaller("/bin/sh", ["-s", "--", ...SETUP_ARGS], script)
@ -148,13 +152,18 @@ function spawnInstaller(command: string, args: readonly string[], stdin?: string
child.stdout.on("data", collect)
child.stderr.on("data", collect)
child.on("error", (error) => resume(Effect.fail({ reason: "installer", detail: error.message })))
child.on("error", (error) =>
resume(Effect.fail({ reason: "installer", detail: error.message, output: error.message })),
)
child.on("close", (code) => {
if (code === 0) return resume(Effect.void)
const detail = installerError(output)
const printed = installerOutput(output)
resume(Effect.fail(detail ? { reason: "installer", detail } : { reason: "installer" }))
resume(
Effect.fail({ reason: "installer", ...(detail ? { detail } : {}), ...(printed ? { output: printed } : {}) }),
)
})
// The shell can exit before it read the whole script; its `close` reports the failure.
child.stdin.on("error", () => {})

View file

@ -1,5 +1,11 @@
import { expect, test } from "bun:test"
import { installerError } from "./installer-error"
import { installerError, installerOutput } from "./installer-error"
test("the output shown to the user drops color codes and surrounding blank lines", () => {
expect(installerOutput("\n\u001b[0;31mbrowse: unknown channel\u001b[0m\r\n hint\r\n\n")).toBe(
"browse: unknown channel\n hint",
)
})
test.each([
{

View file

@ -1,9 +1,7 @@
/** The installer's last warning (`! …`), else its last line: what to tell the user. */
export function installerError(output: string) {
const lines = output
// eslint-disable-next-line no-control-regex
.replace(/\u001b\[[0-9;]*m/g, "")
.split(/\r?\n/)
const lines = installerOutput(output)
.split("\n")
.map((line) => line.trim())
.filter(Boolean)
@ -11,3 +9,14 @@ export function installerError(output: string) {
return line?.replace(/^!\s*/, "").replace(/^browse:\s*/, "")
}
/** What the installer printed, without color codes or blank lines at either end, for the user to read. */
export function installerOutput(output: string) {
return (
output
// eslint-disable-next-line no-control-regex
.replace(/\u001b\[[0-9;]*m/g, "")
.replace(/\r\n?/g, "\n")
.trim()
)
}

View file

@ -88,6 +88,7 @@ const BrowseStatus = Schema.Struct({
failure: Schema.Struct({
reason: Schema.Literals(["start", "download", "timeout", "installer"]),
detail: Schema.optionalKey(Schema.String),
output: Schema.optionalKey(Schema.String),
}),
}),
]),