-
Notifications
You must be signed in to change notification settings - Fork 5
/
update-files.ts
65 lines (51 loc) · 1.8 KB
/
update-files.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// update-files.ts
import * as fs from "fs";
import * as path from "path";
interface CodeSnippet {
filepath: string;
code: string;
}
function insertLineAtBeginning(filePath: string, lineToInsert: string): void {
const content = fs.readFileSync(filePath, "utf-8");
if (!content.startsWith(lineToInsert)) {
const newContent = `${lineToInsert}\n${content}`;
fs.writeFileSync(filePath, newContent);
}
}
function extractFileSnippets(text: string): CodeSnippet[] {
const pattern = /```(?:tsx?|typescript|javascript|js)\s*\n\/\/\s*([^\n]+)\s*\n([\s\S]+?)```/g;
const snippets: CodeSnippet[] = [];
let match;
while ((match = pattern.exec(text)) !== null) {
let filepath = match[1].trim();
if (filepath.startsWith("src/app/")) {
filepath = filepath.replace("src/app/", "app/");
}
if (filepath.includes("layout.tsx")) {
filepath = filepath.replace("layout.tsx", "root.tsx");
}
const code =
filepath.endsWith(".js") || filepath.endsWith(".cjs")
? match[2].trim() // Don't add file header for js/cjs files
: `// ${filepath}\n\n${match[2].trim()}`;
snippets.push({ filepath, code });
}
return snippets;
}
async function writeFiles(snippets: CodeSnippet[], baseDir: string = "."): Promise<string[]> {
const updatedFiles: string[] = [];
for (const { filepath, code } of snippets) {
const fullPath = path.join(baseDir, filepath);
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
fs.writeFileSync(fullPath, code);
updatedFiles.push(filepath);
}
return updatedFiles;
}
async function main(): Promise<void> {
const text = fs.readFileSync("claude-answer.txt", "utf-8");
const snippets = extractFileSnippets(text);
await writeFiles(snippets);
insertLineAtBeginning("claude-answer.txt", "// claude-answer.txt");
}
main();