Skip to content

Commit 03ce2e5

Browse files
authored
refactor(installation): drop facade runtime wrappers (#21984)
1 parent 87e23ab commit 03ce2e5

5 files changed

Lines changed: 50 additions & 40 deletions

File tree

packages/opencode/src/cli/cmd/uninstall.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Argv } from "yargs"
22
import { UI } from "../ui"
33
import * as prompts from "@clack/prompts"
4+
import { AppRuntime } from "@/effect/app-runtime"
45
import { Installation } from "../../installation"
56
import { Global } from "../../global"
67
import fs from "fs/promises"
@@ -57,7 +58,7 @@ export const UninstallCommand = {
5758
UI.empty()
5859
prompts.intro("Uninstall OpenCode")
5960

60-
const method = await Installation.method()
61+
const method = await AppRuntime.runPromise(Installation.Service.use((svc) => svc.method()))
6162
prompts.log.info(`Installation method: ${method}`)
6263

6364
const targets = await collectRemovalTargets(args, method)

packages/opencode/src/cli/cmd/upgrade.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Argv } from "yargs"
22
import { UI } from "../ui"
33
import * as prompts from "@clack/prompts"
4+
import { AppRuntime } from "@/effect/app-runtime"
45
import { Installation } from "../../installation"
56

67
export const UpgradeCommand = {
@@ -24,7 +25,7 @@ export const UpgradeCommand = {
2425
UI.println(UI.logo(" "))
2526
UI.empty()
2627
prompts.intro("Upgrade")
27-
const detectedMethod = await Installation.method()
28+
const detectedMethod = await AppRuntime.runPromise(Installation.Service.use((svc) => svc.method()))
2829
const method = (args.method as Installation.Method) ?? detectedMethod
2930
if (method === "unknown") {
3031
prompts.log.error(`opencode is installed to ${process.execPath} and may be managed by a package manager`)
@@ -42,7 +43,9 @@ export const UpgradeCommand = {
4243
}
4344
}
4445
prompts.log.info("Using method: " + method)
45-
const target = args.target ? args.target.replace(/^v/, "") : await Installation.latest()
46+
const target = args.target
47+
? args.target.replace(/^v/, "")
48+
: await AppRuntime.runPromise(Installation.Service.use((svc) => svc.latest()))
4649

4750
if (Installation.VERSION === target) {
4851
prompts.log.warn(`opencode upgrade skipped: ${target} is already installed`)
@@ -53,7 +56,9 @@ export const UpgradeCommand = {
5356
prompts.log.info(`From ${Installation.VERSION}${target}`)
5457
const spinner = prompts.spinner()
5558
spinner.start("Upgrading...")
56-
const err = await Installation.upgrade(method, target).catch((err) => err)
59+
const err = await AppRuntime.runPromise(Installation.Service.use((svc) => svc.upgrade(method, target))).catch(
60+
(err) => err,
61+
)
5762
if (err) {
5863
spinner.stop("Upgrade failed", 1)
5964
if (err instanceof Installation.UpgradeFailedError) {

packages/opencode/src/cli/upgrade.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Bus } from "@/bus"
22
import { Config } from "@/config/config"
3+
import { AppRuntime } from "@/effect/app-runtime"
34
import { Flag } from "@/flag/flag"
45
import { Installation } from "@/installation"
56

67
export async function upgrade() {
78
const config = await Config.getGlobal()
8-
const method = await Installation.method()
9-
const latest = await Installation.latest(method).catch(() => {})
9+
const method = await AppRuntime.runPromise(Installation.Service.use((svc) => svc.method()))
10+
const latest = await AppRuntime.runPromise(Installation.Service.use((svc) => svc.latest(method))).catch(() => {})
1011
if (!latest) return
1112

1213
if (Flag.OPENCODE_ALWAYS_NOTIFY_UPDATE) {
@@ -25,7 +26,7 @@ export async function upgrade() {
2526
}
2627

2728
if (method === "unknown") return
28-
await Installation.upgrade(method, latest)
29+
await AppRuntime.runPromise(Installation.Service.use((svc) => svc.upgrade(method, latest)))
2930
.then(() => Bus.publish(Installation.Event.Updated, { version: latest }))
3031
.catch(() => {})
3132
}

packages/opencode/src/installation/index.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Effect, Layer, Schema, Context, Stream } from "effect"
22
import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http"
33
import * as CrossSpawnSpawner from "@/effect/cross-spawn-spawner"
4-
import { makeRuntime } from "@/effect/run-service"
54
import { withTransientReadRetry } from "@/util/effect-http-client"
65
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
76
import path from "path"
@@ -338,18 +337,4 @@ export namespace Installation {
338337
Layer.provide(FetchHttpClient.layer),
339338
Layer.provide(CrossSpawnSpawner.defaultLayer),
340339
)
341-
342-
const { runPromise } = makeRuntime(Service, defaultLayer)
343-
344-
export async function method(): Promise<Method> {
345-
return runPromise((svc) => svc.method())
346-
}
347-
348-
export async function latest(installMethod?: Method): Promise<string> {
349-
return runPromise((svc) => svc.latest(installMethod))
350-
}
351-
352-
export async function upgrade(m: Method, target: string): Promise<void> {
353-
return runPromise((svc) => svc.upgrade(m, target))
354-
}
355340
}

packages/opencode/src/server/routes/global.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Hono, type Context } from "hono"
22
import { describeRoute, resolver, validator } from "hono-openapi"
33
import { streamSSE } from "hono/streaming"
4+
import { Effect } from "effect"
45
import z from "zod"
56
import { BusEvent } from "@/bus/bus-event"
67
import { SyncEvent } from "@/sync"
78
import { GlobalBus } from "@/bus/global"
9+
import { AppRuntime } from "@/effect/app-runtime"
810
import { AsyncQueue } from "@/util/queue"
911
import { Instance } from "../../project/instance"
1012
import { Installation } from "@/installation"
@@ -290,25 +292,41 @@ export const GlobalRoutes = lazy(() =>
290292
}),
291293
),
292294
async (c) => {
293-
const method = await Installation.method()
294-
if (method === "unknown") {
295-
return c.json({ success: false, error: "Unknown installation method" }, 400)
296-
}
297-
const target = c.req.valid("json").target || (await Installation.latest(method))
298-
const result = await Installation.upgrade(method, target)
299-
.then(() => ({ success: true as const, version: target }))
300-
.catch((e) => ({ success: false as const, error: e instanceof Error ? e.message : String(e) }))
301-
if (result.success) {
302-
GlobalBus.emit("event", {
303-
directory: "global",
304-
payload: {
305-
type: Installation.Event.Updated.type,
306-
properties: { version: target },
307-
},
308-
})
309-
return c.json(result)
295+
const result = await AppRuntime.runPromise(
296+
Installation.Service.use((svc) =>
297+
Effect.gen(function* () {
298+
const method = yield* svc.method()
299+
if (method === "unknown") {
300+
return { success: false as const, status: 400 as const, error: "Unknown installation method" }
301+
}
302+
303+
const target = c.req.valid("json").target || (yield* svc.latest(method))
304+
const result = yield* Effect.catch(
305+
svc.upgrade(method, target).pipe(Effect.as({ success: true as const, version: target })),
306+
(err) =>
307+
Effect.succeed({
308+
success: false as const,
309+
status: 500 as const,
310+
error: err instanceof Error ? err.message : String(err),
311+
}),
312+
)
313+
if (!result.success) return result
314+
return { ...result, status: 200 as const }
315+
}),
316+
),
317+
)
318+
if (!result.success) {
319+
return c.json({ success: false, error: result.error }, result.status)
310320
}
311-
return c.json(result, 500)
321+
const target = result.version
322+
GlobalBus.emit("event", {
323+
directory: "global",
324+
payload: {
325+
type: Installation.Event.Updated.type,
326+
properties: { version: target },
327+
},
328+
})
329+
return c.json({ success: true, version: target })
312330
},
313331
),
314332
)

0 commit comments

Comments
 (0)