Skip to content

Commit 68e1b3c

Browse files
thdxropencode
andcommitted
Fix TypeScript compilation errors and consolidate version handling
🤖 Generated with [opencode](https://opencode.ai) Co-Authored-By: opencode <[email protected]>
1 parent 2d68814 commit 68e1b3c

File tree

15 files changed

+123
-181
lines changed

15 files changed

+123
-181
lines changed

packages/opencode/src/app/app.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Global } from "../global"
55
import path from "path"
66
import os from "os"
77
import { z } from "zod"
8+
import { Installation } from "../installation"
89

910
export namespace App {
1011
const log = Log.create({ service: "app" })
@@ -32,7 +33,7 @@ export namespace App {
3233

3334
const APP_JSON = "app.json"
3435

35-
async function create(input: { cwd: string; version: string }) {
36+
async function create(input: { cwd: string }) {
3637
log.info("creating", {
3738
cwd: input.cwd,
3839
})
@@ -51,7 +52,7 @@ export namespace App {
5152
initialized: number
5253
version: string
5354
}
54-
state.version = input.version
55+
state.version = Installation.VERSION
5556
await stateFile.write(JSON.stringify(state))
5657

5758
const services = new Map<
@@ -76,7 +77,6 @@ export namespace App {
7677
},
7778
}
7879
const result = {
79-
version: input.version,
8080
services,
8181
info,
8282
}
@@ -108,7 +108,7 @@ export namespace App {
108108
}
109109

110110
export async function provide<T>(
111-
input: { cwd: string; version: string },
111+
input: { cwd: string },
112112
cb: (app: Info) => Promise<T>,
113113
) {
114114
const app = await create(input)
@@ -124,12 +124,12 @@ export namespace App {
124124
}
125125

126126
export async function initialize() {
127-
const { info, version } = ctx.use()
127+
const { info } = ctx.use()
128128
info.time.initialized = Date.now()
129129
await Bun.write(
130130
path.join(info.path.data, APP_JSON),
131131
JSON.stringify({
132-
version,
132+
version: Installation.VERSION,
133133
initialized: Date.now(),
134134
}),
135135
)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Server } from "../../server/server"
22
import fs from "fs/promises"
33
import path from "path"
44
import type { CommandModule } from "yargs"
5-
import { Config } from "../../config/config"
65

76
export const GenerateCommand = {
87
command: "generate",

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { Session } from "../../session"
66
import { Share } from "../../share/share"
77
import { Message } from "../../session/message"
88
import { UI } from "../ui"
9-
import { VERSION } from "../version"
109
import { cmd } from "./cmd"
1110
import { GlobalConfig } from "../../global/config"
1211
import { Flag } from "../../flag/flag"
@@ -48,7 +47,6 @@ export const RunCommand = cmd({
4847
await App.provide(
4948
{
5049
cwd: process.cwd(),
51-
version: VERSION,
5250
},
5351
async () => {
5452
await Share.init()
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { App } from "../../app/app"
22
import { LSP } from "../../lsp"
3-
import { VERSION } from "../version"
43
import { cmd } from "./cmd"
54

65
export const ScrapCommand = cmd({
76
command: "scrap <file>",
87
builder: (yargs) =>
98
yargs.positional("file", { type: "string", demandOption: true }),
109
async handler(args) {
11-
await App.provide({ cwd: process.cwd(), version: VERSION }, async (app) => {
12-
await LSP.touchFile(args.file, true)
13-
console.log(await LSP.diagnostics())
14-
})
10+
await App.provide(
11+
{ cwd: process.cwd() },
12+
async () => {
13+
await LSP.touchFile(args.file, true)
14+
console.log(await LSP.diagnostics())
15+
},
16+
)
1517
},
1618
})
Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Argv } from "yargs"
22
import { UI } from "../ui"
3-
import { VERSION } from "../version"
43
import * as prompts from "@clack/prompts"
54
import { Installation } from "../../installation"
65

@@ -27,7 +26,7 @@ export const UpgradeCommand = {
2726
return
2827
}
2928
const target = args.target ?? (await Installation.latest())
30-
prompts.log.info(`From ${VERSION}${target}`)
29+
prompts.log.info(`From ${Installation.VERSION}${target}`)
3130
const spinner = prompts.spinner()
3231
spinner.start("Upgrading...")
3332
const err = await Installation.upgrade(method, target).catch((err) => err)
@@ -41,69 +40,5 @@ export const UpgradeCommand = {
4140
}
4241
spinner.stop("Upgrade complete")
4342
prompts.outro("Done")
44-
return
45-
46-
/*
47-
if (!process.execPath.includes(path.join(".opencode", "bin")) && false) {
48-
return
49-
}
50-
51-
const release = args.target
52-
? await specific(args.target).catch(() => {})
53-
: await latest().catch(() => {})
54-
if (!release) {
55-
prompts.log.error("Failed to fetch release information")
56-
prompts.outro("Done")
57-
return
58-
}
59-
60-
const target = release.tag_name
61-
62-
if (VERSION !== "dev" && compare(VERSION, target) >= 0) {
63-
prompts.log.success(`Already up to date`)
64-
prompts.outro("Done")
65-
return
66-
}
67-
68-
prompts.log.info(`From ${VERSION} → ${target}`)
69-
70-
const name = asset()
71-
const found = release.assets.find((a) => a.name === name)
72-
73-
if (!found) {
74-
prompts.log.error(`No binary found for platform: ${name}`)
75-
prompts.outro("Done")
76-
return
77-
}
78-
79-
const spinner = prompts.spinner()
80-
spinner.start("Downloading update...")
81-
82-
const downloadPath = await download(found.browser_download_url).catch(
83-
() => {},
84-
)
85-
if (!downloadPath) {
86-
spinner.stop("Download failed")
87-
prompts.log.error("Download failed")
88-
prompts.outro("Done")
89-
return
90-
}
91-
92-
spinner.stop("Download complete")
93-
94-
const renamed = await fs
95-
.rename(downloadPath, process.execPath)
96-
.catch(() => {})
97-
98-
if (renamed === undefined) {
99-
prompts.log.error("Install failed")
100-
await fs.unlink(downloadPath).catch(() => {})
101-
prompts.outro("Done")
102-
return
103-
}
104-
105-
prompts.log.success(`Successfully upgraded to ${target}`)
106-
prompts.outro("Done")
107-
*/
10843
},
10944
}

packages/opencode/src/cli/version.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/opencode/src/external/ripgrep.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { App } from "../app/app"
21
import path from "path"
32
import { Global } from "../global"
43
import fs from "fs/promises"

packages/opencode/src/file/index.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

packages/opencode/src/index.ts

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import yargs from "yargs"
99
import { hideBin } from "yargs/helpers"
1010
import { RunCommand } from "./cli/cmd/run"
1111
import { GenerateCommand } from "./cli/cmd/generate"
12-
import { VERSION } from "./cli/version"
1312
import { ScrapCommand } from "./cli/cmd/scrap"
1413
import { Log } from "./util/log"
1514
import { AuthCommand, AuthLoginCommand } from "./cli/cmd/auth"
@@ -18,29 +17,19 @@ import { Provider } from "./provider/provider"
1817
import { UI } from "./cli/ui"
1918
import { GlobalConfig } from "./global/config"
2019
import { Installation } from "./installation"
21-
;(async () => {
22-
if (Installation.VERSION === "dev") return
23-
if (Installation.isSnapshot()) return
24-
const config = await GlobalConfig.get()
25-
if (config.autoupdate === false) return
26-
const latest = await Installation.latest()
27-
if (Installation.VERSION === latest) return
28-
const method = await Installation.method()
29-
if (method === "unknown") return
30-
await Installation.upgrade(method, latest).catch(() => {})
31-
})()
20+
import { Bus } from "./bus"
3221

3322
const cli = yargs(hideBin(process.argv))
3423
.scriptName("opencode")
35-
.version(VERSION)
24+
.version(Installation.VERSION)
3625
.option("print-logs", {
3726
describe: "Print logs to stderr",
3827
type: "boolean",
3928
})
4029
.middleware(async () => {
4130
await Log.init({ print: process.argv.includes("--print-logs") })
4231
Log.Default.info("opencode", {
43-
version: VERSION,
32+
version: Installation.VERSION,
4433
args: process.argv.slice(2),
4534
})
4635
})
@@ -57,52 +46,62 @@ const cli = yargs(hideBin(process.argv))
5746
while (true) {
5847
const cwd = args.project ? path.resolve(args.project) : process.cwd()
5948
process.chdir(cwd)
60-
const result = await App.provide(
61-
{ cwd, version: VERSION },
62-
async (app) => {
63-
const providers = await Provider.list()
64-
if (Object.keys(providers).length === 0) {
65-
return "needs_provider"
66-
}
49+
const result = await App.provide({ cwd }, async (app) => {
50+
const providers = await Provider.list()
51+
if (Object.keys(providers).length === 0) {
52+
return "needs_provider"
53+
}
6754

68-
await Share.init()
69-
const server = Server.listen()
55+
await Share.init()
56+
const server = Server.listen()
7057

71-
let cmd = ["go", "run", "./main.go"]
72-
let cwd = new URL("../../tui/cmd/opencode", import.meta.url)
73-
.pathname
74-
if (Bun.embeddedFiles.length > 0) {
75-
const blob = Bun.embeddedFiles[0] as File
76-
const binary = path.join(Global.Path.cache, "tui", blob.name)
77-
const file = Bun.file(binary)
78-
if (!(await file.exists())) {
79-
await Bun.write(file, blob, { mode: 0o755 })
80-
await fs.chmod(binary, 0o755)
81-
}
82-
cwd = process.cwd()
83-
cmd = [binary]
58+
let cmd = ["go", "run", "./main.go"]
59+
let cwd = new URL("../../tui/cmd/opencode", import.meta.url).pathname
60+
if (Bun.embeddedFiles.length > 0) {
61+
const blob = Bun.embeddedFiles[0] as File
62+
const binary = path.join(Global.Path.cache, "tui", blob.name)
63+
const file = Bun.file(binary)
64+
if (!(await file.exists())) {
65+
await Bun.write(file, blob, { mode: 0o755 })
66+
await fs.chmod(binary, 0o755)
8467
}
85-
const proc = Bun.spawn({
86-
cmd: [...cmd, ...process.argv.slice(2)],
87-
cwd,
88-
stdout: "inherit",
89-
stderr: "inherit",
90-
stdin: "inherit",
91-
env: {
92-
...process.env,
93-
OPENCODE_SERVER: server.url.toString(),
94-
OPENCODE_APP_INFO: JSON.stringify(app),
95-
},
96-
onExit: () => {
97-
server.stop()
98-
},
99-
})
100-
await proc.exited
101-
await server.stop()
68+
cwd = process.cwd()
69+
cmd = [binary]
70+
}
71+
const proc = Bun.spawn({
72+
cmd: [...cmd, ...process.argv.slice(2)],
73+
cwd,
74+
stdout: "inherit",
75+
stderr: "inherit",
76+
stdin: "inherit",
77+
env: {
78+
...process.env,
79+
OPENCODE_SERVER: server.url.toString(),
80+
OPENCODE_APP_INFO: JSON.stringify(app),
81+
},
82+
onExit: () => {
83+
server.stop()
84+
},
85+
})
86+
87+
;(async () => {
88+
if (Installation.VERSION === "dev") return
89+
if (Installation.isSnapshot()) return
90+
const config = await GlobalConfig.get()
91+
if (config.autoupdate === false) return
92+
const latest = await Installation.latest()
93+
if (Installation.VERSION === latest) return
94+
const method = await Installation.method()
95+
if (method === "unknown") return
96+
await Installation.upgrade(method, latest).catch(() => {})
97+
Bus.publish(Installation.Event.Updated, { version: latest })
98+
})()
99+
100+
await proc.exited
101+
await server.stop()
102102

103-
return "done"
104-
},
105-
)
103+
return "done"
104+
})
106105
if (result === "done") break
107106
if (result === "needs_provider") {
108107
UI.empty()

packages/opencode/src/installation/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,24 @@ import path from "path"
22
import { $ } from "bun"
33
import { z } from "zod"
44
import { NamedError } from "../util/error"
5+
import { Bus } from "../bus"
6+
7+
declare global {
8+
const OPENCODE_VERSION: string
9+
}
510

611
export namespace Installation {
712
export type Method = Awaited<ReturnType<typeof method>>
813

14+
export const Event = {
15+
Updated: Bus.event(
16+
"installation.updated",
17+
z.object({
18+
version: z.string(),
19+
}),
20+
),
21+
}
22+
923
export const Info = z
1024
.object({
1125
version: z.string(),

0 commit comments

Comments
 (0)