Skip to content

Commit 29ccd76

Browse files
committed
refactor: dedupe and harmonize bun tests
1 parent 757865e commit 29ccd76

File tree

2 files changed

+155
-157
lines changed

2 files changed

+155
-157
lines changed

packages/opencode/src/bun/index.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,39 @@ export namespace BunProc {
8686
await writePackageJson(parsed)
8787
}
8888

89+
async function cleanup(provider: string, oldPkg: string) {
90+
const parsed = await readPackageJson()
91+
const providers = parsed.opencode?.providers ?? {}
92+
const used = Object.entries(providers).some(([p, name]) => p !== provider && name === oldPkg)
93+
if (used) return
94+
log.info("removing unused package", { pkg: oldPkg })
95+
await BunProc.run(["remove", "--cwd", Global.Path.cache, oldPkg]).catch(() => {})
96+
}
97+
8998
export async function install(pkg: string, version = "latest", provider?: string) {
9099
using _ = await Lock.write("bun-install")
91100

92101
const mod = path.join(Global.Path.cache, "node_modules", pkg)
93102
const parsed = await readPackageJson()
94103
const oldPkg = provider ? parsed.opencode?.providers?.[provider] : undefined
95104
const switched = oldPkg && oldPkg !== pkg
105+
const dependencies = parsed.dependencies ?? {}
106+
const modExists = await Filesystem.exists(mod)
107+
const cachedVersion = dependencies[pkg]
108+
109+
const earlyReturn = async () => {
110+
if (provider) await track(provider, pkg)
111+
if (switched) await cleanup(provider!, oldPkg!)
112+
return mod
113+
}
96114

97115
// Skip install if exact version already cached (always reinstall with "latest")
98116
const installed = parsed.dependencies?.[pkg]
99117
if (installed && version !== "latest" && installed === version && (await Filesystem.exists(mod))) {
100-
if (provider) await track(provider, pkg)
101-
if (switched) {
102-
const providers = parsed.opencode?.providers ?? {}
103-
const used = Object.entries(providers).some(([p, name]) => p !== provider && name === oldPkg)
104-
if (!used) {
105-
log.info("removing unused package", { pkg: oldPkg })
106-
await BunProc.run(["remove", "--cwd", Global.Path.cache, oldPkg]).catch(() => {})
107-
}
108-
}
109-
return mod
118+
return earlyReturn()
110119
} else if (version === "latest") {
111120
const isOutdated = await PackageRegistry.isOutdated(pkg, cachedVersion, Global.Path.cache)
112-
if (!isOutdated) return mod
121+
if (!isOutdated) return earlyReturn()
113122
log.info("Cached version is outdated, proceeding with install", { pkg, cachedVersion })
114123
}
115124

@@ -131,15 +140,7 @@ export namespace BunProc {
131140
})
132141

133142
if (provider) await track(provider, pkg)
134-
if (switched) {
135-
const current = await readPackageJson()
136-
const providers = current.opencode?.providers ?? {}
137-
const used = Object.entries(providers).some(([p, name]) => p !== provider && name === oldPkg)
138-
if (!used) {
139-
log.info("removing unused package", { pkg: oldPkg })
140-
await BunProc.run(["remove", "--cwd", Global.Path.cache, oldPkg!]).catch(() => {})
141-
}
142-
}
143+
if (switched) await cleanup(provider!, oldPkg!)
143144
return mod
144145
}
145146
}

0 commit comments

Comments
 (0)