The open source coding agent.
TypeScript
refactor: migrate src/cli/cmd/session.ts from Bun.file() to statSync (#14144)
Daxe37a9081a673
diff --git a/packages/opencode/src/cli/cmd/session.ts b/packages/opencode/src/cli/cmd/session.tsindex 8239b0bcb38..4aa702359d1 100644--- a/packages/opencode/src/cli/cmd/session.ts+++ b/packages/opencode/src/cli/cmd/session.ts@@ -5,6 +5,7 @@ import { bootstrap } from "../bootstrap"import { UI } from "../ui"import { Locale } from "../../util/locale"import { Flag } from "../../flag/flag"+import { Filesystem } from "../../util/filesystem"import { EOL } from "os"import path from "path"@@ -17,18 +18,18 @@ function pagerCmd(): string[] {// user could have less installed via other optionsconst lessOnPath = Bun.which("less")if (lessOnPath) {- if (Bun.file(lessOnPath).size) return [lessOnPath, ...lessOptions]+ if (Filesystem.stat(lessOnPath)?.size) return [lessOnPath, ...lessOptions]}if (Flag.OPENCODE_GIT_BASH_PATH) {const less = path.join(Flag.OPENCODE_GIT_BASH_PATH, "..", "..", "usr", "bin", "less.exe")- if (Bun.file(less).size) return [less, ...lessOptions]+ if (Filesystem.stat(less)?.size) return [less, ...lessOptions]}const git = Bun.which("git")if (git) {const less = path.join(git, "..", "..", "usr", "bin", "less.exe")- if (Bun.file(less).size) return [less, ...lessOptions]+ if (Filesystem.stat(less)?.size) return [less, ...lessOptions]}// Fall back to Windows built-in more (via cmd.exe)diff --git a/packages/opencode/src/file/time.ts b/packages/opencode/src/file/time.tsindex 35c780fbdd5..c85781eb411 100644--- a/packages/opencode/src/file/time.ts+++ b/packages/opencode/src/file/time.ts@@ -1,6 +1,7 @@import { Instance } from "../project/instance"import { Log } from "../util/log"import { Flag } from "../flag/flag"+import { Filesystem } from "../util/filesystem"export namespace FileTime {const log = Log.create({ service: "file.time" })@@ -59,10 +60,10 @@ export namespace FileTime {const time = get(sessionID, filepath)if (!time) throw new Error(`You must read file ${filepath} before overwriting it. Use the Read tool first`)- const stats = await Bun.file(filepath).stat()- if (stats.mtime.getTime() > time.getTime()) {+ const mtime = Filesystem.stat(filepath)?.mtime+ if (mtime && mtime.getTime() > time.getTime()) {throw new Error(- `File ${filepath} has been modified since it was last read.\nLast modification: ${stats.mtime.toISOString()}\nLast read: ${time.toISOString()}\n\nPlease read the file again before modifying it.`,+ `File ${filepath} has been modified since it was last read.\nLast modification: ${mtime.toISOString()}\nLast read: ${time.toISOString()}\n\nPlease read the file again before modifying it.`,)}}diff --git a/packages/opencode/src/util/filesystem.ts b/packages/opencode/src/util/filesystem.tsindex d2f22be5206..7b196eb84b8 100644--- a/packages/opencode/src/util/filesystem.ts+++ b/packages/opencode/src/util/filesystem.ts@@ -18,12 +18,13 @@ export namespace Filesystem {}}+ export function stat(p: string): ReturnType<typeof statSync> | undefined {+ return statSync(p, { throwIfNoEntry: false }) ?? undefined+ }+export async function size(p: string): Promise<number> {- try {- return statSync(p).size- } catch {- return 0- }+ const s = stat(p)?.size ?? 0+ return typeof s === "bigint" ? Number(s) : s}export async function readText(p: string): Promise<string> {