refactor: migrate src/cli/cmd/session.ts from Bun.file() to statSync (#14144)
This commit is contained in:
@@ -5,6 +5,7 @@ import { bootstrap } from "../bootstrap"
|
|||||||
import { UI } from "../ui"
|
import { UI } from "../ui"
|
||||||
import { Locale } from "../../util/locale"
|
import { Locale } from "../../util/locale"
|
||||||
import { Flag } from "../../flag/flag"
|
import { Flag } from "../../flag/flag"
|
||||||
|
import { Filesystem } from "../../util/filesystem"
|
||||||
import { EOL } from "os"
|
import { EOL } from "os"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
|
|
||||||
@@ -17,18 +18,18 @@ function pagerCmd(): string[] {
|
|||||||
// user could have less installed via other options
|
// user could have less installed via other options
|
||||||
const lessOnPath = Bun.which("less")
|
const lessOnPath = Bun.which("less")
|
||||||
if (lessOnPath) {
|
if (lessOnPath) {
|
||||||
if (Bun.file(lessOnPath).size) return [lessOnPath, ...lessOptions]
|
if (Filesystem.stat(lessOnPath)?.size) return [lessOnPath, ...lessOptions]
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Flag.OPENCODE_GIT_BASH_PATH) {
|
if (Flag.OPENCODE_GIT_BASH_PATH) {
|
||||||
const less = path.join(Flag.OPENCODE_GIT_BASH_PATH, "..", "..", "usr", "bin", "less.exe")
|
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")
|
const git = Bun.which("git")
|
||||||
if (git) {
|
if (git) {
|
||||||
const less = path.join(git, "..", "..", "usr", "bin", "less.exe")
|
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)
|
// Fall back to Windows built-in more (via cmd.exe)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Instance } from "../project/instance"
|
import { Instance } from "../project/instance"
|
||||||
import { Log } from "../util/log"
|
import { Log } from "../util/log"
|
||||||
import { Flag } from "../flag/flag"
|
import { Flag } from "../flag/flag"
|
||||||
|
import { Filesystem } from "../util/filesystem"
|
||||||
|
|
||||||
export namespace FileTime {
|
export namespace FileTime {
|
||||||
const log = Log.create({ service: "file.time" })
|
const log = Log.create({ service: "file.time" })
|
||||||
@@ -59,10 +60,10 @@ export namespace FileTime {
|
|||||||
|
|
||||||
const time = get(sessionID, filepath)
|
const time = get(sessionID, filepath)
|
||||||
if (!time) throw new Error(`You must read file ${filepath} before overwriting it. Use the Read tool first`)
|
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()
|
const mtime = Filesystem.stat(filepath)?.mtime
|
||||||
if (stats.mtime.getTime() > time.getTime()) {
|
if (mtime && mtime.getTime() > time.getTime()) {
|
||||||
throw new Error(
|
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.`,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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> {
|
export async function size(p: string): Promise<number> {
|
||||||
try {
|
const s = stat(p)?.size ?? 0
|
||||||
return statSync(p).size
|
return typeof s === "bigint" ? Number(s) : s
|
||||||
} catch {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function readText(p: string): Promise<string> {
|
export async function readText(p: string): Promise<string> {
|
||||||
|
|||||||
Reference in New Issue
Block a user