|
| 1 | +import { readFile, writeFile } from "node:fs/promises"; |
| 2 | +import { glob } from "glob"; |
| 3 | +import type { Diff, FileDiff } from "../components/types"; |
| 4 | +import { Git } from "./git"; |
| 5 | + |
| 6 | +export class FileEditor { |
| 7 | + private git = new Git(); |
| 8 | + // Text file extension patterns |
| 9 | + // TODO: enhance these patterns |
| 10 | + private readonly textFilePatterns = [ |
| 11 | + "**/*.txt", |
| 12 | + "**/*.md", |
| 13 | + "**/*.js", |
| 14 | + "**/*.jsx", |
| 15 | + "**/*.ts", |
| 16 | + "**/*.tsx", |
| 17 | + "**/*.json", |
| 18 | + "**/*.css", |
| 19 | + "**/*.html", |
| 20 | + "**/*.xml", |
| 21 | + "**/*.yml", |
| 22 | + "**/*.yaml", |
| 23 | + ]; |
| 24 | + |
| 25 | + // Cat word candidates |
| 26 | + private readonly catWords = [ |
| 27 | + "ニャー", |
| 28 | + "ミャー", |
| 29 | + "ニャン", |
| 30 | + "ミャン", |
| 31 | + "ニャッ", |
| 32 | + "ミャッ", |
| 33 | + "ニャオ~ン", |
| 34 | + "ミャオ~", |
| 35 | + "ゴロゴロ", |
| 36 | + ]; |
| 37 | + |
| 38 | + /** |
| 39 | + * Get files under git management (when git is initialized) |
| 40 | + */ |
| 41 | + private async getGitTrackedFiles(): Promise<string[]> { |
| 42 | + const gitFiles = await this.git.lsFiles(); |
| 43 | + return gitFiles.filter((file) => this.isTextFile(file)); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Search for text files (fallback when git is not initialized) |
| 48 | + */ |
| 49 | + private async getTextFiles(): Promise<string[]> { |
| 50 | + return await glob(this.textFilePatterns); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Determine if a file is a text file |
| 55 | + */ |
| 56 | + private isTextFile(filePath: string): boolean { |
| 57 | + const extension = filePath.split(".").pop()?.toLowerCase(); |
| 58 | + const textExtensions = [ |
| 59 | + "txt", |
| 60 | + "md", |
| 61 | + "js", |
| 62 | + "ts", |
| 63 | + "tsx", |
| 64 | + "json", |
| 65 | + "css", |
| 66 | + "html", |
| 67 | + "xml", |
| 68 | + "yml", |
| 69 | + "yaml", |
| 70 | + ]; |
| 71 | + return textExtensions.includes(extension || ""); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Select a file randomly |
| 76 | + */ |
| 77 | + async selectRandomFile(): Promise<string | null> { |
| 78 | + let files: string[]; |
| 79 | + |
| 80 | + // Check if git is initialized |
| 81 | + if (await this.git.isInited()) { |
| 82 | + files = await this.getGitTrackedFiles(); |
| 83 | + } else { |
| 84 | + files = await this.getTextFiles(); |
| 85 | + } |
| 86 | + |
| 87 | + if (files.length === 0) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + const randomIndex = Math.floor(Math.random() * files.length); |
| 92 | + return files[randomIndex] as string; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Edit file content in a cat-like manner |
| 97 | + */ |
| 98 | + private editFileContent(content: string): string { |
| 99 | + const words = content.match(/\w+/g); |
| 100 | + if (!words || words.length === 0) return content; |
| 101 | + |
| 102 | + const targetWord = words[ |
| 103 | + Math.floor(Math.random() * words.length) |
| 104 | + ] as string; |
| 105 | + const catWord = this.catWords[ |
| 106 | + Math.floor(Math.random() * this.catWords.length) |
| 107 | + ] as string; |
| 108 | + |
| 109 | + // Replace up to 3 locations |
| 110 | + let replacedContent = content; |
| 111 | + let replacementCount = 0; |
| 112 | + const maxReplacements = 3; |
| 113 | + |
| 114 | + replacedContent = replacedContent.replace( |
| 115 | + new RegExp(`\\b${targetWord}\\b`, "g"), |
| 116 | + (match) => { |
| 117 | + if (replacementCount < maxReplacements) { |
| 118 | + replacementCount++; |
| 119 | + return catWord; |
| 120 | + } |
| 121 | + return match; |
| 122 | + }, |
| 123 | + ); |
| 124 | + |
| 125 | + return replacedContent; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Generate structured diff |
| 130 | + */ |
| 131 | + private generateDiffs(original: string, edited: string): Diff[] { |
| 132 | + const originalLines = original.split("\n"); |
| 133 | + const editedLines = edited.split("\n"); |
| 134 | + const diffs: Diff[] = []; |
| 135 | + |
| 136 | + const maxLines = Math.max(originalLines.length, editedLines.length); |
| 137 | + |
| 138 | + for (let i = 0; i < maxLines; i++) { |
| 139 | + const rowNumber = i + 1; |
| 140 | + const originalLine = originalLines[i] || ""; |
| 141 | + const editedLine = editedLines[i] || ""; |
| 142 | + |
| 143 | + if (originalLine !== editedLine) { |
| 144 | + diffs.push({ |
| 145 | + rowNumber, |
| 146 | + a: originalLine, |
| 147 | + b: editedLine, |
| 148 | + }); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return diffs; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Edit file and generate diff |
| 157 | + */ |
| 158 | + async edit(): Promise<FileDiff | null> { |
| 159 | + const filePath = await this.selectRandomFile(); |
| 160 | + if (!filePath) { |
| 161 | + return null; |
| 162 | + } |
| 163 | + |
| 164 | + // Read file content |
| 165 | + const originalContent = await readFile(filePath, "utf-8"); |
| 166 | + |
| 167 | + // Edit |
| 168 | + const editedContent = this.editFileContent(originalContent); |
| 169 | + |
| 170 | + // Write to file |
| 171 | + await writeFile(filePath, editedContent, "utf-8"); |
| 172 | + |
| 173 | + // Generate structured diff |
| 174 | + const diffs = this.generateDiffs(originalContent, editedContent); |
| 175 | + if (diffs.length === 0) { |
| 176 | + return null; |
| 177 | + } |
| 178 | + |
| 179 | + return { |
| 180 | + fileName: filePath, |
| 181 | + diffs, |
| 182 | + }; |
| 183 | + } |
| 184 | +} |
0 commit comments