Skip to content

Commit e37a908

Browse files
authored
refactor: migrate src/cli/cmd/session.ts from Bun.file() to statSync (#14144)
1 parent a2469d9 commit e37a908

3 files changed

Lines changed: 14 additions & 11 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { bootstrap } from "../bootstrap"
55
import { UI } from "../ui"
66
import { Locale } from "../../util/locale"
77
import { Flag } from "../../flag/flag"
8+
import { Filesystem } from "../../util/filesystem"
89
import { EOL } from "os"
910
import path from "path"
1011

@@ -17,18 +18,18 @@ function pagerCmd(): string[] {
1718
// user could have less installed via other options
1819
const lessOnPath = Bun.which("less")
1920
if (lessOnPath) {
20-
if (Bun.file(lessOnPath).size) return [lessOnPath, ...lessOptions]
21+
if (Filesystem.stat(lessOnPath)?.size) return [lessOnPath, ...lessOptions]
2122
}
2223

2324
if (Flag.OPENCODE_GIT_BASH_PATH) {
2425
const less = path.join(Flag.OPENCODE_GIT_BASH_PATH, "..", "..", "usr", "bin", "less.exe")
25-
if (Bun.file(less).size) return [less, ...lessOptions]
26+
if (Filesystem.stat(less)?.size) return [less, ...lessOptions]
2627
}
2728

2829
const git = Bun.which("git")
2930
if (git) {
3031
const less = path.join(git, "..", "..", "usr", "bin", "less.exe")
31-
if (Bun.file(less).size) return [less, ...lessOptions]
32+
if (Filesystem.stat(less)?.size) return [less, ...lessOptions]
3233
}
3334

3435
// Fall back to Windows built-in more (via cmd.exe)

packages/opencode/src/file/time.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Instance } from "../project/instance"
22
import { Log } from "../util/log"
33
import { Flag } from "../flag/flag"
4+
import { Filesystem } from "../util/filesystem"
45

56
export namespace FileTime {
67
const log = Log.create({ service: "file.time" })
@@ -59,10 +60,10 @@ export namespace FileTime {
5960

6061
const time = get(sessionID, filepath)
6162
if (!time) throw new Error(`You must read file ${filepath} before overwriting it. Use the Read tool first`)
62-
const stats = await Bun.file(filepath).stat()
63-
if (stats.mtime.getTime() > time.getTime()) {
63+
const mtime = Filesystem.stat(filepath)?.mtime
64+
if (mtime && mtime.getTime() > time.getTime()) {
6465
throw new Error(
65-
`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.`,
66+
`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.`,
6667
)
6768
}
6869
}

packages/opencode/src/util/filesystem.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ export namespace Filesystem {
1818
}
1919
}
2020

21+
export function stat(p: string): ReturnType<typeof statSync> | undefined {
22+
return statSync(p, { throwIfNoEntry: false }) ?? undefined
23+
}
24+
2125
export async function size(p: string): Promise<number> {
22-
try {
23-
return statSync(p).size
24-
} catch {
25-
return 0
26-
}
26+
const s = stat(p)?.size ?? 0
27+
return typeof s === "bigint" ? Number(s) : s
2728
}
2829

2930
export async function readText(p: string): Promise<string> {

0 commit comments

Comments
 (0)