index.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { z } from "zod"
  2. import { Bus } from "../bus"
  3. import { $ } from "bun"
  4. import { createPatch } from "diff"
  5. import path from "path"
  6. import * as git from "isomorphic-git"
  7. import { App } from "../app/app"
  8. import fs from "fs"
  9. import { Log } from "../util/log"
  10. export namespace File {
  11. const log = Log.create({ service: "file" })
  12. export const Event = {
  13. Edited: Bus.event(
  14. "file.edited",
  15. z.object({
  16. file: z.string(),
  17. }),
  18. ),
  19. }
  20. export async function status() {
  21. const app = App.info()
  22. if (!app.git) return []
  23. const diffOutput = await $`git diff --numstat HEAD`
  24. .cwd(app.path.cwd)
  25. .quiet()
  26. .nothrow()
  27. .text()
  28. const changedFiles = []
  29. if (diffOutput.trim()) {
  30. const lines = diffOutput.trim().split("\n")
  31. for (const line of lines) {
  32. const [added, removed, filepath] = line.split("\t")
  33. changedFiles.push({
  34. file: filepath,
  35. added: added === "-" ? 0 : parseInt(added, 10),
  36. removed: removed === "-" ? 0 : parseInt(removed, 10),
  37. status: "modified",
  38. })
  39. }
  40. }
  41. const untrackedOutput = await $`git ls-files --others --exclude-standard`
  42. .cwd(app.path.cwd)
  43. .quiet()
  44. .nothrow()
  45. .text()
  46. if (untrackedOutput.trim()) {
  47. const untrackedFiles = untrackedOutput.trim().split("\n")
  48. for (const filepath of untrackedFiles) {
  49. try {
  50. const content = await Bun.file(
  51. path.join(app.path.root, filepath),
  52. ).text()
  53. const lines = content.split("\n").length
  54. changedFiles.push({
  55. file: filepath,
  56. added: lines,
  57. removed: 0,
  58. status: "added",
  59. })
  60. } catch {
  61. continue
  62. }
  63. }
  64. }
  65. // Get deleted files
  66. const deletedOutput = await $`git diff --name-only --diff-filter=D HEAD`
  67. .cwd(app.path.cwd)
  68. .quiet()
  69. .nothrow()
  70. .text()
  71. if (deletedOutput.trim()) {
  72. const deletedFiles = deletedOutput.trim().split("\n")
  73. for (const filepath of deletedFiles) {
  74. changedFiles.push({
  75. file: filepath,
  76. added: 0,
  77. removed: 0, // Could get original line count but would require another git command
  78. status: "deleted",
  79. })
  80. }
  81. }
  82. return changedFiles.map((x) => ({
  83. ...x,
  84. file: path.relative(app.path.cwd, path.join(app.path.root, x.file)),
  85. }))
  86. }
  87. export async function read(file: string) {
  88. using _ = log.time("read", { file })
  89. const app = App.info()
  90. const full = path.join(app.path.cwd, file)
  91. const content = await Bun.file(full)
  92. .text()
  93. .catch(() => "")
  94. .then((x) => x.trim())
  95. if (app.git) {
  96. const rel = path.relative(app.path.root, full)
  97. const diff = await git.status({
  98. fs,
  99. dir: app.path.root,
  100. filepath: rel,
  101. })
  102. if (diff !== "unmodified") {
  103. const original = await $`git show HEAD:${rel}`
  104. .cwd(app.path.root)
  105. .quiet()
  106. .nothrow()
  107. .text()
  108. const patch = createPatch(file, original, content, "old", "new", {
  109. context: Infinity,
  110. })
  111. return { type: "patch", content: patch }
  112. }
  113. }
  114. return { type: "raw", content }
  115. }
  116. }