refactor: migrate file/index.ts from Bun.file() to Filesystem module (#14152)

This commit is contained in:
Dax
2026-02-18 12:33:20 -05:00
committed by GitHub
parent 82a323ef70
commit ef155f3766

View File

@@ -1,7 +1,6 @@
import { BusEvent } from "@/bus/bus-event" import { BusEvent } from "@/bus/bus-event"
import z from "zod" import z from "zod"
import { $ } from "bun" import { $ } from "bun"
import type { BunFile } from "bun"
import { formatPatch, structuredPatch } from "diff" import { formatPatch, structuredPatch } from "diff"
import path from "path" import path from "path"
import fs from "fs" import fs from "fs"
@@ -241,8 +240,8 @@ export namespace File {
return mimeType.startsWith("image/") return mimeType.startsWith("image/")
} }
async function shouldEncode(file: BunFile): Promise<boolean> { async function shouldEncode(mimeType: string): Promise<boolean> {
const type = file.type?.toLowerCase() const type = mimeType.toLowerCase()
log.info("shouldEncode", { type }) log.info("shouldEncode", { type })
if (!type) return false if (!type) return false
@@ -385,7 +384,7 @@ export namespace File {
const untrackedFiles = untrackedOutput.trim().split("\n") const untrackedFiles = untrackedOutput.trim().split("\n")
for (const filepath of untrackedFiles) { for (const filepath of untrackedFiles) {
try { try {
const content = await Bun.file(path.join(Instance.directory, filepath)).text() const content = await Filesystem.readText(path.join(Instance.directory, filepath))
const lines = content.split("\n").length const lines = content.split("\n").length
changedFiles.push({ changedFiles.push({
path: filepath, path: filepath,
@@ -437,10 +436,9 @@ export namespace File {
// Fast path: check extension before any filesystem operations // Fast path: check extension before any filesystem operations
if (isImageByExtension(file)) { if (isImageByExtension(file)) {
const bunFile = Bun.file(full) if (await Filesystem.exists(full)) {
if (await bunFile.exists()) { const buffer = await Filesystem.readBytes(full).catch(() => Buffer.from([]))
const buffer = await bunFile.arrayBuffer().catch(() => new ArrayBuffer(0)) const content = buffer.toString("base64")
const content = Buffer.from(buffer).toString("base64")
const mimeType = getImageMimeType(file) const mimeType = getImageMimeType(file)
return { type: "text", content, mimeType, encoding: "base64" } return { type: "text", content, mimeType, encoding: "base64" }
} }
@@ -451,29 +449,24 @@ export namespace File {
return { type: "binary", content: "" } return { type: "binary", content: "" }
} }
const bunFile = Bun.file(full) if (!(await Filesystem.exists(full))) {
if (!(await bunFile.exists())) {
return { type: "text", content: "" } return { type: "text", content: "" }
} }
const encode = await shouldEncode(bunFile) const mimeType = Filesystem.mimeType(full)
const mimeType = bunFile.type || "application/octet-stream" const encode = await shouldEncode(mimeType)
if (encode && !isImage(mimeType)) { if (encode && !isImage(mimeType)) {
return { type: "binary", content: "", mimeType } return { type: "binary", content: "", mimeType }
} }
if (encode) { if (encode) {
const buffer = await bunFile.arrayBuffer().catch(() => new ArrayBuffer(0)) const buffer = await Filesystem.readBytes(full).catch(() => Buffer.from([]))
const content = Buffer.from(buffer).toString("base64") const content = buffer.toString("base64")
return { type: "text", content, mimeType, encoding: "base64" } return { type: "text", content, mimeType, encoding: "base64" }
} }
const content = await bunFile const content = (await Filesystem.readText(full).catch(() => "")).trim()
.text()
.catch(() => "")
.then((x) => x.trim())
if (project.vcs === "git") { if (project.vcs === "git") {
let diff = await $`git diff ${file}`.cwd(Instance.directory).quiet().nothrow().text() let diff = await $`git diff ${file}`.cwd(Instance.directory).quiet().nothrow().text()
@@ -497,13 +490,13 @@ export namespace File {
let ignored = (_: string) => false let ignored = (_: string) => false
if (project.vcs === "git") { if (project.vcs === "git") {
const ig = ignore() const ig = ignore()
const gitignore = Bun.file(path.join(Instance.worktree, ".gitignore")) const gitignorePath = path.join(Instance.worktree, ".gitignore")
if (await gitignore.exists()) { if (await Filesystem.exists(gitignorePath)) {
ig.add(await gitignore.text()) ig.add(await Filesystem.readText(gitignorePath))
} }
const ignoreFile = Bun.file(path.join(Instance.worktree, ".ignore")) const ignorePath = path.join(Instance.worktree, ".ignore")
if (await ignoreFile.exists()) { if (await Filesystem.exists(ignorePath)) {
ig.add(await ignoreFile.text()) ig.add(await Filesystem.readText(ignorePath))
} }
ignored = ig.ignores.bind(ig) ignored = ig.ignores.bind(ig)
} }