-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompose.cjs
62 lines (51 loc) · 1.62 KB
/
compose.cjs
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
const fs = require("fs");
const path = require("path");
const rootDir = "./";
const outputFile = "formatted_output.txt";
const foldersToInclude = ["public", "routes"];
const validExtensions = [".js", ".css", ".html"];
function getLanguage(ext) {
switch (ext) {
case ".js":
return "javascript";
case ".css":
return "css";
case ".html":
return "html";
default:
return "";
}
}
function formatFileContent(filePath, content) {
const ext = path.extname(filePath);
const language = getLanguage(ext);
return `${filePath}\n\`\`\`${language}\n${content}\n\`\`\`\n\n`;
}
function processFile(filePath) {
const content = fs.readFileSync(filePath, "utf8");
return formatFileContent(filePath, content);
}
function searchFiles(dir, baseDir = "") {
let result = "";
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
const folderName = path.basename(filePath);
if (baseDir === "" || foldersToInclude.includes(baseDir)) {
result += searchFiles(filePath, baseDir || folderName);
}
} else if (validExtensions.includes(path.extname(file))) {
const relativePath = path.relative(rootDir, filePath);
result += processFile(relativePath);
}
}
return result;
}
function main() {
let formattedContent = `I am working on a simple and quick AI chat web app. Here is the full project:\n\n${searchFiles(rootDir)}`;
fs.writeFileSync(outputFile, formattedContent);
console.log(`Formatted content has been saved to ${outputFile}`);
}
main();