GeezerVison is showing github.com — Remove use of Bun.file (#14215) · nathanhinish/opencode@02a9495
File treeExpand file treeCollapse file tree Open diff view settings .opencode/skill/bun-file-io
Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .opencode/skill/bun-file-io/SKILL.md -42Lines changed: 0 additions & 42 deletions Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/tui/component/prompt/frecency.tsx +7-6Lines changed: 7 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import path from "path" | 2 | 2 | import { Global } from "@/global" | | 3 | +import { Filesystem } from "@/util/filesystem" | 3 | 4 | import { onMount } from "solid-js" | 4 | 5 | import { createStore } from "solid-js/store" | 5 | 6 | import { createSimpleContext } from "../../context/helper" | 6 | | -import { appendFile } from "fs/promises" | | 7 | +import { appendFile, writeFile } from "fs/promises" | 7 | 8 |
| 8 | 9 | function calculateFrecency(entry?: { frequency: number; lastOpen: number }): number { | 9 | 10 | if (!entry) return 0 | @@ -17,9 +18,9 @@ const MAX_FRECENCY_ENTRIES = 1000 | 17 | 18 | export const { use: useFrecency, provider: FrecencyProvider } = createSimpleContext({ | 18 | 19 | name: "Frecency", | 19 | 20 | init: () => { | 20 | | - const frecencyFile = Bun.file(path.join(Global.Path.state, "frecency.jsonl")) | | 21 | + const frecencyPath = path.join(Global.Path.state, "frecency.jsonl") | 21 | 22 | onMount(async () => { | 22 | | - const text = await frecencyFile.text().catch(() => "") | | 23 | + const text = await Filesystem.readText(frecencyPath).catch(() => "") | 23 | 24 | const lines = text | 24 | 25 | .split("\n") | 25 | 26 | .filter(Boolean) | @@ -53,7 +54,7 @@ export const { use: useFrecency, provider: FrecencyProvider } = createSimpleCont | 53 | 54 |
| 54 | 55 | if (sorted.length > 0) { | 55 | 56 | const content = sorted.map((entry) => JSON.stringify(entry)).join("\n") + "\n" | 56 | | - Bun.write(frecencyFile, content).catch(() => {}) | | 57 | + writeFile(frecencyPath, content).catch(() => {}) | 57 | 58 | } | 58 | 59 | }) | 59 | 60 |
| @@ -68,15 +69,15 @@ export const { use: useFrecency, provider: FrecencyProvider } = createSimpleCont | 68 | 69 | lastOpen: Date.now(), | 69 | 70 | } | 70 | 71 | setStore("data", absolutePath, newEntry) | 71 | | - appendFile(frecencyFile.name!, JSON.stringify({ path: absolutePath, ...newEntry }) + "\n").catch(() => {}) | | 72 | + appendFile(frecencyPath, JSON.stringify({ path: absolutePath, ...newEntry }) + "\n").catch(() => {}) | 72 | 73 |
| 73 | 74 | if (Object.keys(store.data).length > MAX_FRECENCY_ENTRIES) { | 74 | 75 | const sorted = Object.entries(store.data) | 75 | 76 | .sort(([, a], [, b]) => b.lastOpen - a.lastOpen) | 76 | 77 | .slice(0, MAX_FRECENCY_ENTRIES) | 77 | 78 | setStore("data", Object.fromEntries(sorted)) | 78 | 79 | const content = sorted.map(([path, entry]) => JSON.stringify({ path, ...entry })).join("\n") + "\n" | 79 | | - Bun.write(frecencyFile, content).catch(() => {}) | | 80 | + writeFile(frecencyPath, content).catch(() => {}) | 80 | 81 | } | 81 | 82 | } | 82 | 83 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/tui/component/prompt/history.tsx +6-5Lines changed: 6 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import path from "path" | 2 | 2 | import { Global } from "@/global" | | 3 | +import { Filesystem } from "@/util/filesystem" | 3 | 4 | import { onMount } from "solid-js" | 4 | 5 | import { createStore, produce } from "solid-js/store" | 5 | 6 | import { clone } from "remeda" | @@ -30,9 +31,9 @@ const MAX_HISTORY_ENTRIES = 50 | 30 | 31 | export const { use: usePromptHistory, provider: PromptHistoryProvider } = createSimpleContext({ | 31 | 32 | name: "PromptHistory", | 32 | 33 | init: () => { | 33 | | - const historyFile = Bun.file(path.join(Global.Path.state, "prompt-history.jsonl")) | | 34 | + const historyPath = path.join(Global.Path.state, "prompt-history.jsonl") | 34 | 35 | onMount(async () => { | 35 | | - const text = await historyFile.text().catch(() => "") | | 36 | + const text = await Filesystem.readText(historyPath).catch(() => "") | 36 | 37 | const lines = text | 37 | 38 | .split("\n") | 38 | 39 | .filter(Boolean) | @@ -51,7 +52,7 @@ export const { use: usePromptHistory, provider: PromptHistoryProvider } = create | 51 | 52 | // Rewrite file with only valid entries to self-heal corruption | 52 | 53 | if (lines.length > 0) { | 53 | 54 | const content = lines.map((line) => JSON.stringify(line)).join("\n") + "\n" | 54 | | - writeFile(historyFile.name!, content).catch(() => {}) | | 55 | + writeFile(historyPath, content).catch(() => {}) | 55 | 56 | } | 56 | 57 | }) | 57 | 58 |
| @@ -97,11 +98,11 @@ export const { use: usePromptHistory, provider: PromptHistoryProvider } = create | 97 | 98 |
| 98 | 99 | if (trimmed) { | 99 | 100 | const content = store.history.map((line) => JSON.stringify(line)).join("\n") + "\n" | 100 | | - writeFile(historyFile.name!, content).catch(() => {}) | | 101 | + writeFile(historyPath, content).catch(() => {}) | 101 | 102 | return | 102 | 103 | } | 103 | 104 |
| 104 | | - appendFile(historyFile.name!, JSON.stringify(entry) + "\n").catch(() => {}) | | 105 | + appendFile(historyPath, JSON.stringify(entry) + "\n").catch(() => {}) | 105 | 106 | }, | 106 | 107 | } | 107 | 108 | }, | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx +11-9Lines changed: 11 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { BoxRenderable, TextareaRenderable, MouseEvent, PasteEvent, t, dim, fg } from "@opentui/core" | 2 | 2 | import { createEffect, createMemo, type JSX, onMount, createSignal, onCleanup, on, Show, Switch, Match } from "solid-js" | 3 | 3 | import "opentui-spinner/solid" | | 4 | +import path from "path" | | 5 | +import { Filesystem } from "@/util/filesystem" | 4 | 6 | import { useLocal } from "@tui/context/local" | 5 | 7 | import { useTheme } from "@tui/context/theme" | 6 | 8 | import { EmptyBorder } from "@tui/component/border" | @@ -931,26 +933,26 @@ export function Prompt(props: PromptProps) { | 931 | 933 | const isUrl = /^(https?):\/\//.test(filepath) | 932 | 934 | if (!isUrl) { | 933 | 935 | try { | 934 | | - const file = Bun.file(filepath) | | 936 | + const mime = Filesystem.mimeType(filepath) | | 937 | + const filename = path.basename(filepath) | 935 | 938 | // Handle SVG as raw text content, not as base64 image | 936 | | - if (file.type === "image/svg+xml") { | | 939 | + if (mime === "image/svg+xml") { | 937 | 940 | event.preventDefault() | 938 | | - const content = await file.text().catch(() => {}) | | 941 | + const content = await Filesystem.readText(filepath).catch(() => {}) | 939 | 942 | if (content) { | 940 | | - pasteText(content, `[SVG: ${file.name ?? "image"}]`) | | 943 | + pasteText(content, `[SVG: ${filename ?? "image"}]`) | 941 | 944 | return | 942 | 945 | } | 943 | 946 | } | 944 | | - if (file.type.startsWith("image/")) { | | 947 | + if (mime.startsWith("image/")) { | 945 | 948 | event.preventDefault() | 946 | | - const content = await file | 947 | | - .arrayBuffer() | | 949 | + const content = await Filesystem.readArrayBuffer(filepath) | 948 | 950 | .then((buffer) => Buffer.from(buffer).toString("base64")) | 949 | 951 | .catch(() => {}) | 950 | 952 | if (content) { | 951 | 953 | await pasteImage({ | 952 | | - filename: file.name, | 953 | | - mime: file.type, | | 954 | + filename, | | 955 | + mime, | 954 | 956 | content, | 955 | 957 | }) | 956 | 958 | return | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/tui/component/prompt/stash.tsx +8-7Lines changed: 8 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import path from "path" | 2 | 2 | import { Global } from "@/global" | | 3 | +import { Filesystem } from "@/util/filesystem" | 3 | 4 | import { onMount } from "solid-js" | 4 | 5 | import { createStore, produce } from "solid-js/store" | 5 | 6 | import { clone } from "remeda" | @@ -18,9 +19,9 @@ const MAX_STASH_ENTRIES = 50 | 18 | 19 | export const { use: usePromptStash, provider: PromptStashProvider } = createSimpleContext({ | 19 | 20 | name: "PromptStash", | 20 | 21 | init: () => { | 21 | | - const stashFile = Bun.file(path.join(Global.Path.state, "prompt-stash.jsonl")) | | 22 | + const stashPath = path.join(Global.Path.state, "prompt-stash.jsonl") | 22 | 23 | onMount(async () => { | 23 | | - const text = await stashFile.text().catch(() => "") | | 24 | + const text = await Filesystem.readText(stashPath).catch(() => "") | 24 | 25 | const lines = text | 25 | 26 | .split("\n") | 26 | 27 | .filter(Boolean) | @@ -39,7 +40,7 @@ export const { use: usePromptStash, provider: PromptStashProvider } = createSimp | 39 | 40 | // Rewrite file with only valid entries to self-heal corruption | 40 | 41 | if (lines.length > 0) { | 41 | 42 | const content = lines.map((line) => JSON.stringify(line)).join("\n") + "\n" | 42 | | - writeFile(stashFile.name!, content).catch(() => {}) | | 43 | + writeFile(stashPath, content).catch(() => {}) | 43 | 44 | } | 44 | 45 | }) | 45 | 46 |
| @@ -66,11 +67,11 @@ export const { use: usePromptStash, provider: PromptStashProvider } = createSimp | 66 | 67 |
| 67 | 68 | if (trimmed) { | 68 | 69 | const content = store.entries.map((line) => JSON.stringify(line)).join("\n") + "\n" | 69 | | - writeFile(stashFile.name!, content).catch(() => {}) | | 70 | + writeFile(stashPath, content).catch(() => {}) | 70 | 71 | return | 71 | 72 | } | 72 | 73 |
| 73 | | - appendFile(stashFile.name!, JSON.stringify(stash) + "\n").catch(() => {}) | | 74 | + appendFile(stashPath, JSON.stringify(stash) + "\n").catch(() => {}) | 74 | 75 | }, | 75 | 76 | pop() { | 76 | 77 | if (store.entries.length === 0) return undefined | @@ -82,7 +83,7 @@ export const { use: usePromptStash, provider: PromptStashProvider } = createSimp | 82 | 83 | ) | 83 | 84 | const content = | 84 | 85 | store.entries.length > 0 ? store.entries.map((line) => JSON.stringify(line)).join("\n") + "\n" : "" | 85 | | - writeFile(stashFile.name!, content).catch(() => {}) | | 86 | + writeFile(stashPath, content).catch(() => {}) | 86 | 87 | return entry | 87 | 88 | }, | 88 | 89 | remove(index: number) { | @@ -94,7 +95,7 @@ export const { use: usePromptStash, provider: PromptStashProvider } = createSimp | 94 | 95 | ) | 95 | 96 | const content = | 96 | 97 | store.entries.length > 0 ? store.entries.map((line) => JSON.stringify(line)).join("\n") + "\n" : "" | 97 | | - writeFile(stashFile.name!, content).catch(() => {}) | | 98 | + writeFile(stashPath, content).catch(() => {}) | 98 | 99 | }, | 99 | 100 | } | 100 | 101 | }, | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/tui/context/kv.tsx +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { Global } from "@/global" | | 2 | +import { Filesystem } from "@/util/filesystem" | 2 | 3 | import { createSignal, type Setter } from "solid-js" | 3 | 4 | import { createStore } from "solid-js/store" | 4 | 5 | import { createSimpleContext } from "./helper" | @@ -9,10 +10,9 @@ export const { use: useKV, provider: KVProvider } = createSimpleContext({ | 9 | 10 | init: () => { | 10 | 11 | const [ready, setReady] = createSignal(false) | 11 | 12 | const [store, setStore] = createStore<Record<string, any>>() | 12 | | - const file = Bun.file(path.join(Global.Path.state, "kv.json")) | | 13 | + const filePath = path.join(Global.Path.state, "kv.json") | 13 | 14 |
| 14 | | - file | 15 | | - .json() | | 15 | + Filesystem.readJson(filePath) | 16 | 16 | .then((x) => { | 17 | 17 | setStore(x) | 18 | 18 | }) | @@ -44,7 +44,7 @@ export const { use: useKV, provider: KVProvider } = createSimpleContext({ | 44 | 44 | }, | 45 | 45 | set(key: string, value: any) { | 46 | 46 | setStore(key, value) | 47 | | - Bun.write(file, JSON.stringify(store, null, 2)) | | 47 | + Filesystem.writeJson(filePath, store) | 48 | 48 | }, | 49 | 49 | } | 50 | 50 | return result | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/tui/context/local.tsx +9-12Lines changed: 9 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,7 @@ import { Provider } from "@/provider/provider" | 12 | 12 | import { useArgs } from "./args" | 13 | 13 | import { useSDK } from "./sdk" | 14 | 14 | import { RGBA } from "@opentui/core" | | 15 | +import { Filesystem } from "@/util/filesystem" | 15 | 16 |
| 16 | 17 | export const { use: useLocal, provider: LocalProvider } = createSimpleContext({ | 17 | 18 | name: "Local", | @@ -119,7 +120,7 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({ | 119 | 120 | variant: {}, | 120 | 121 | }) | 121 | 122 |
| 122 | | - const file = Bun.file(path.join(Global.Path.state, "model.json")) | | 123 | + const filePath = path.join(Global.Path.state, "model.json") | 123 | 124 | const state = { | 124 | 125 | pending: false, | 125 | 126 | } | @@ -130,19 +131,15 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({ | 130 | 131 | return | 131 | 132 | } | 132 | 133 | state.pending = false | 133 | | - Bun.write( | 134 | | - file, | 135 | | - JSON.stringify({ | 136 | | - recent: modelStore.recent, | 137 | | - favorite: modelStore.favorite, | 138 | | - variant: modelStore.variant, | 139 | | - }), | 140 | | - ) | | 134 | + Filesystem.writeJson(filePath, { | | 135 | + recent: modelStore.recent, | | 136 | + favorite: modelStore.favorite, | | 137 | + variant: modelStore.variant, | | 138 | + }) | 141 | 139 | } | 142 | 140 |
| 143 | | - file | 144 | | - .json() | 145 | | - .then((x) => { | | 141 | + Filesystem.readJson(filePath) | | 142 | + .then((x: any) => { | 146 | 143 | if (Array.isArray(x.recent)) setModelStore("recent", x.recent) | 147 | 144 | if (Array.isArray(x.favorite)) setModelStore("favorite", x.favorite) | 148 | 145 | if (typeof x.variant === "object" && x.variant !== null) setModelStore("variant", x.variant) | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/tui/context/theme.tsx +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -412,7 +412,7 @@ async function getCustomThemes() { | 412 | 412 | cwd: dir, | 413 | 413 | })) { | 414 | 414 | const name = path.basename(item, ".json") | 415 | | - result[name] = await Bun.file(item).json() | | 415 | + result[name] = await Filesystem.readJson(item) | 416 | 416 | } | 417 | 417 | } | 418 | 418 | return result | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/tui/thread.ts +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,10 +3,12 @@ import { tui } from "./app" | 3 | 3 | import { Rpc } from "@/util/rpc" | 4 | 4 | import { type rpc } from "./worker" | 5 | 5 | import path from "path" | | 6 | +import { fileURLToPath } from "url" | 6 | 7 | import { UI } from "@/cli/ui" | 7 | 8 | import { iife } from "@/util/iife" | 8 | 9 | import { Log } from "@/util/log" | 9 | 10 | import { withNetworkOptions, resolveNetworkOptions } from "@/cli/network" | | 11 | +import { Filesystem } from "@/util/filesystem" | 10 | 12 | import type { Event } from "@opencode-ai/sdk/v2" | 11 | 13 | import type { EventSource } from "./context/sdk" | 12 | 14 | import { win32DisableProcessedInput, win32InstallCtrlCGuard } from "./win32" | @@ -99,7 +101,7 @@ export const TuiThreadCommand = cmd({ | 99 | 101 | const distWorker = new URL("./cli/cmd/tui/worker.js", import.meta.url) | 100 | 102 | const workerPath = await iife(async () => { | 101 | 103 | if (typeof OPENCODE_WORKER_PATH !== "undefined") return OPENCODE_WORKER_PATH | 102 | | - if (await Bun.file(distWorker).exists()) return distWorker | | 104 | + if (await Filesystem.exists(fileURLToPath(distWorker))) return distWorker | 103 | 105 | return localWorker | 104 | 106 | }) | 105 | 107 | try { | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/lsp/client.ts +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -147,8 +147,7 @@ export namespace LSPClient { | 147 | 147 | notify: { | 148 | 148 | async open(input: { path: string }) { | 149 | 149 | input.path = path.isAbsolute(input.path) ? input.path : path.resolve(Instance.directory, input.path) | 150 | | - const file = Bun.file(input.path) | 151 | | - const text = await file.text() | | 150 | + const text = await Filesystem.readText(input.path) | 152 | 151 | const extension = path.extname(input.path) | 153 | 152 | const languageId = LANGUAGE_EXTENSIONS[extension] ?? "plaintext" | 154 | 153 |
| |
|