(file: string)
| 232 | } |
| 233 | |
| 234 | export async function read(file: string): Promise<Content> { |
| 235 | using _ = log.time("read", { file }) |
| 236 | const project = Instance.project |
| 237 | const full = path.join(Instance.directory, file) |
| 238 | const bunFile = Bun.file(full) |
| 239 | |
| 240 | if (!(await bunFile.exists())) { |
| 241 | return { type: "text", content: "" } |
| 242 | } |
| 243 | |
| 244 | const encode = await shouldEncode(bunFile) |
| 245 | |
| 246 | if (encode) { |
| 247 | const buffer = await bunFile.arrayBuffer().catch(() => new ArrayBuffer(0)) |
| 248 | const content = Buffer.from(buffer).toString("base64") |
| 249 | const mimeType = bunFile.type || "application/octet-stream" |
| 250 | return { type: "text", content, mimeType, encoding: "base64" } |
| 251 | } |
| 252 | |
| 253 | const content = await bunFile |
| 254 | .text() |
| 255 | .catch(() => "") |
| 256 | .then((x) => x.trim()) |
| 257 | |
| 258 | if (project.vcs === "git") { |
| 259 | let diff = await $`git diff ${file}`.cwd(Instance.directory).quiet().nothrow().text() |
| 260 | if (!diff.trim()) diff = await $`git diff --staged ${file}`.cwd(Instance.directory).quiet().nothrow().text() |
| 261 | if (diff.trim()) { |
| 262 | const original = await $`git show HEAD:${file}`.cwd(Instance.directory).quiet().nothrow().text() |
| 263 | const patch = structuredPatch(file, file, original, content, "old", "new", { |
| 264 | context: Infinity, |
| 265 | ignoreWhitespace: true, |
| 266 | }) |
| 267 | const diff = formatPatch(patch) |
| 268 | return { type: "text", content, patch, diff } |
| 269 | } |
| 270 | } |
| 271 | return { type: "text", content } |
| 272 | } |
| 273 | |
| 274 | export async function list(dir?: string) { |
| 275 | const exclude = [".git", ".DS_Store"] |
nothing calls this directly
no test coverage detected