Skip to content

Commit 65b18dc

Browse files
Apply PR #15012: refactor(opencode): replace Bun.which with npm which
2 parents 9ecd033 + bf53e1c commit 65b18dc

11 files changed

Lines changed: 186 additions & 77 deletions

File tree

bun.lock

Lines changed: 10 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opencode/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@types/mime-types": "3.0.1",
4444
"@types/turndown": "5.0.5",
4545
"@types/yargs": "17.0.33",
46+
"@types/which": "3.0.4",
4647
"@typescript/native-preview": "catalog:",
4748
"drizzle-kit": "1.0.0-beta.12-a5629fb",
4849
"drizzle-orm": "1.0.0-beta.12-a5629fb",
@@ -127,6 +128,7 @@
127128
"ulid": "catalog:",
128129
"vscode-jsonrpc": "8.2.1",
129130
"web-tree-sitter": "0.25.10",
131+
"which": "6.0.1",
130132
"xdg-basedir": "5.1.0",
131133
"yargs": "18.0.0",
132134
"zod": "catalog:",

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Filesystem } from "../../util/filesystem"
99
import { Process } from "../../util/process"
1010
import { EOL } from "os"
1111
import path from "path"
12+
import { which } from "../../util/which"
1213

1314
function pagerCmd(): string[] {
1415
const lessOptions = ["-R", "-S"]
@@ -17,7 +18,7 @@ function pagerCmd(): string[] {
1718
}
1819

1920
// user could have less installed via other options
20-
const lessOnPath = Bun.which("less")
21+
const lessOnPath = which("less")
2122
if (lessOnPath) {
2223
if (Filesystem.stat(lessOnPath)?.size) return [lessOnPath, ...lessOptions]
2324
}
@@ -27,7 +28,7 @@ function pagerCmd(): string[] {
2728
if (Filesystem.stat(less)?.size) return [less, ...lessOptions]
2829
}
2930

30-
const git = Bun.which("git")
31+
const git = which("git")
3132
if (git) {
3233
const less = path.join(git, "..", "..", "usr", "bin", "less.exe")
3334
if (Filesystem.stat(less)?.size) return [less, ...lessOptions]

packages/opencode/src/cli/cmd/tui/util/clipboard.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { tmpdir } from "os"
66
import path from "path"
77
import { Filesystem } from "../../../../util/filesystem"
88
import { Process } from "../../../../util/process"
9+
import { which } from "../../../../util/which"
910

1011
/**
1112
* Writes text to clipboard via OSC 52 escape sequence.
@@ -76,7 +77,7 @@ export namespace Clipboard {
7677
const getCopyMethod = lazy(() => {
7778
const os = platform()
7879

79-
if (os === "darwin" && Bun.which("osascript")) {
80+
if (os === "darwin" && which("osascript")) {
8081
console.log("clipboard: using osascript")
8182
return async (text: string) => {
8283
const escaped = text.replace(/\\/g, "\\\\").replace(/"/g, '\\"')
@@ -85,7 +86,7 @@ export namespace Clipboard {
8586
}
8687

8788
if (os === "linux") {
88-
if (process.env["WAYLAND_DISPLAY"] && Bun.which("wl-copy")) {
89+
if (process.env["WAYLAND_DISPLAY"] && which("wl-copy")) {
8990
console.log("clipboard: using wl-copy")
9091
return async (text: string) => {
9192
const proc = Process.spawn(["wl-copy"], { stdin: "pipe", stdout: "ignore", stderr: "ignore" })
@@ -95,7 +96,7 @@ export namespace Clipboard {
9596
await proc.exited.catch(() => {})
9697
}
9798
}
98-
if (Bun.which("xclip")) {
99+
if (which("xclip")) {
99100
console.log("clipboard: using xclip")
100101
return async (text: string) => {
101102
const proc = Process.spawn(["xclip", "-selection", "clipboard"], {
@@ -109,7 +110,7 @@ export namespace Clipboard {
109110
await proc.exited.catch(() => {})
110111
}
111112
}
112-
if (Bun.which("xsel")) {
113+
if (which("xsel")) {
113114
console.log("clipboard: using xsel")
114115
return async (text: string) => {
115116
const proc = Process.spawn(["xsel", "--clipboard", "--input"], {

packages/opencode/src/file/ripgrep.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { lazy } from "../util/lazy"
88
import { $ } from "bun"
99
import { Filesystem } from "../util/filesystem"
1010
import { Process } from "../util/process"
11+
import { which } from "../util/which"
1112
import { text } from "node:stream/consumers"
1213

1314
import { ZipReader, BlobReader, BlobWriter } from "@zip.js/zip.js"
@@ -126,7 +127,7 @@ export namespace Ripgrep {
126127
)
127128

128129
const state = lazy(async () => {
129-
const system = Bun.which("rg")
130+
const system = which("rg")
130131
if (system) {
131132
const stat = await fs.stat(system).catch(() => undefined)
132133
if (stat?.isFile()) return { filepath: system }

packages/opencode/src/format/formatter.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { BunProc } from "../bun"
33
import { Instance } from "../project/instance"
44
import { Filesystem } from "../util/filesystem"
55
import { Process } from "../util/process"
6+
import { which } from "../util/which"
67
import { Flag } from "@/flag/flag"
78

89
export interface Info {
@@ -18,7 +19,7 @@ export const gofmt: Info = {
1819
command: ["gofmt", "-w", "$FILE"],
1920
extensions: [".go"],
2021
async enabled() {
21-
return Bun.which("gofmt") !== null
22+
return which("gofmt") !== null
2223
},
2324
}
2425

@@ -27,7 +28,7 @@ export const mix: Info = {
2728
command: ["mix", "format", "$FILE"],
2829
extensions: [".ex", ".exs", ".eex", ".heex", ".leex", ".neex", ".sface"],
2930
async enabled() {
30-
return Bun.which("mix") !== null
31+
return which("mix") !== null
3132
},
3233
}
3334

@@ -152,7 +153,7 @@ export const zig: Info = {
152153
command: ["zig", "fmt", "$FILE"],
153154
extensions: [".zig", ".zon"],
154155
async enabled() {
155-
return Bun.which("zig") !== null
156+
return which("zig") !== null
156157
},
157158
}
158159

@@ -171,7 +172,7 @@ export const ktlint: Info = {
171172
command: ["ktlint", "-F", "$FILE"],
172173
extensions: [".kt", ".kts"],
173174
async enabled() {
174-
return Bun.which("ktlint") !== null
175+
return which("ktlint") !== null
175176
},
176177
}
177178

@@ -180,7 +181,7 @@ export const ruff: Info = {
180181
command: ["ruff", "format", "$FILE"],
181182
extensions: [".py", ".pyi"],
182183
async enabled() {
183-
if (!Bun.which("ruff")) return false
184+
if (!which("ruff")) return false
184185
const configs = ["pyproject.toml", "ruff.toml", ".ruff.toml"]
185186
for (const config of configs) {
186187
const found = await Filesystem.findUp(config, Instance.directory, Instance.worktree)
@@ -210,7 +211,7 @@ export const rlang: Info = {
210211
command: ["air", "format", "$FILE"],
211212
extensions: [".R"],
212213
async enabled() {
213-
const airPath = Bun.which("air")
214+
const airPath = which("air")
214215
if (airPath == null) return false
215216

216217
try {
@@ -239,7 +240,7 @@ export const uvformat: Info = {
239240
extensions: [".py", ".pyi"],
240241
async enabled() {
241242
if (await ruff.enabled()) return false
242-
if (Bun.which("uv") !== null) {
243+
if (which("uv") !== null) {
243244
const proc = Process.spawn(["uv", "format", "--help"], { stderr: "pipe", stdout: "pipe" })
244245
const code = await proc.exited
245246
return code === 0
@@ -253,7 +254,7 @@ export const rubocop: Info = {
253254
command: ["rubocop", "--autocorrect", "$FILE"],
254255
extensions: [".rb", ".rake", ".gemspec", ".ru"],
255256
async enabled() {
256-
return Bun.which("rubocop") !== null
257+
return which("rubocop") !== null
257258
},
258259
}
259260

@@ -262,7 +263,7 @@ export const standardrb: Info = {
262263
command: ["standardrb", "--fix", "$FILE"],
263264
extensions: [".rb", ".rake", ".gemspec", ".ru"],
264265
async enabled() {
265-
return Bun.which("standardrb") !== null
266+
return which("standardrb") !== null
266267
},
267268
}
268269

@@ -271,7 +272,7 @@ export const htmlbeautifier: Info = {
271272
command: ["htmlbeautifier", "$FILE"],
272273
extensions: [".erb", ".html.erb"],
273274
async enabled() {
274-
return Bun.which("htmlbeautifier") !== null
275+
return which("htmlbeautifier") !== null
275276
},
276277
}
277278

@@ -280,7 +281,7 @@ export const dart: Info = {
280281
command: ["dart", "format", "$FILE"],
281282
extensions: [".dart"],
282283
async enabled() {
283-
return Bun.which("dart") !== null
284+
return which("dart") !== null
284285
},
285286
}
286287

@@ -289,7 +290,7 @@ export const ocamlformat: Info = {
289290
command: ["ocamlformat", "-i", "$FILE"],
290291
extensions: [".ml", ".mli"],
291292
async enabled() {
292-
if (!Bun.which("ocamlformat")) return false
293+
if (!which("ocamlformat")) return false
293294
const items = await Filesystem.findUp(".ocamlformat", Instance.directory, Instance.worktree)
294295
return items.length > 0
295296
},
@@ -300,7 +301,7 @@ export const terraform: Info = {
300301
command: ["terraform", "fmt", "$FILE"],
301302
extensions: [".tf", ".tfvars"],
302303
async enabled() {
303-
return Bun.which("terraform") !== null
304+
return which("terraform") !== null
304305
},
305306
}
306307

@@ -309,7 +310,7 @@ export const latexindent: Info = {
309310
command: ["latexindent", "-w", "-s", "$FILE"],
310311
extensions: [".tex"],
311312
async enabled() {
312-
return Bun.which("latexindent") !== null
313+
return which("latexindent") !== null
313314
},
314315
}
315316

@@ -318,7 +319,7 @@ export const gleam: Info = {
318319
command: ["gleam", "format", "$FILE"],
319320
extensions: [".gleam"],
320321
async enabled() {
321-
return Bun.which("gleam") !== null
322+
return which("gleam") !== null
322323
},
323324
}
324325

@@ -327,7 +328,7 @@ export const shfmt: Info = {
327328
command: ["shfmt", "-w", "$FILE"],
328329
extensions: [".sh", ".bash"],
329330
async enabled() {
330-
return Bun.which("shfmt") !== null
331+
return which("shfmt") !== null
331332
},
332333
}
333334

@@ -336,7 +337,7 @@ export const nixfmt: Info = {
336337
command: ["nixfmt", "$FILE"],
337338
extensions: [".nix"],
338339
async enabled() {
339-
return Bun.which("nixfmt") !== null
340+
return which("nixfmt") !== null
340341
},
341342
}
342343

@@ -345,7 +346,7 @@ export const rustfmt: Info = {
345346
command: ["rustfmt", "$FILE"],
346347
extensions: [".rs"],
347348
async enabled() {
348-
return Bun.which("rustfmt") !== null
349+
return which("rustfmt") !== null
349350
},
350351
}
351352

@@ -372,7 +373,7 @@ export const ormolu: Info = {
372373
command: ["ormolu", "-i", "$FILE"],
373374
extensions: [".hs"],
374375
async enabled() {
375-
return Bun.which("ormolu") !== null
376+
return which("ormolu") !== null
376377
},
377378
}
378379

@@ -381,7 +382,7 @@ export const cljfmt: Info = {
381382
command: ["cljfmt", "fix", "--quiet", "$FILE"],
382383
extensions: [".clj", ".cljs", ".cljc", ".edn"],
383384
async enabled() {
384-
return Bun.which("cljfmt") !== null
385+
return which("cljfmt") !== null
385386
},
386387
}
387388

@@ -390,6 +391,6 @@ export const dfmt: Info = {
390391
command: ["dfmt", "-i", "$FILE"],
391392
extensions: [".d"],
392393
async enabled() {
393-
return Bun.which("dfmt") !== null
394+
return which("dfmt") !== null
394395
},
395396
}

0 commit comments

Comments
 (0)