-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileProcessing.js
53 lines (49 loc) · 1.75 KB
/
fileProcessing.js
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
const fs = require('fs');
const path = require('path');
function getLanguage(extension) {
const languages = {
'.js': 'javascript',
'.jsx': 'javascript',
'.json': 'json',
'.md': 'markdown',
'.sh': 'shell',
'.ts': 'typescript',
'.tsx': 'typescript',
'.yaml': 'yaml',
'.yml': 'yaml',
'Dockerfile': 'docker'
// Add more extensions and their corresponding languages as needed
};
return languages[extension] || '';
}
function appendFileContents(dir, fileStructure, outputStream, baseDir, progress, maxFileSize) {
Object.keys(fileStructure).forEach((key) => {
const filePath = path.join(dir, key);
const relativePath = path.relative(baseDir, filePath);
if (fileStructure[key]) {
appendFileContents(filePath, fileStructure[key], outputStream, baseDir, progress, maxFileSize);
} else {
try {
if (fs.existsSync(filePath)) {
const stats = fs.statSync(filePath);
if (stats.size > maxFileSize * 1024) { // Convert KB to Bytes
console.log(`Skipping large file: ${relativePath}`);
return;
}
const content = fs.readFileSync(filePath, 'utf8');
const extension = path.extname(filePath);
const language = getLanguage(extension);
const mdContent = `\n\n## ${relativePath}\n\n\`\`\`${language}\n${content}\n\`\`\`\nEnd of file: ${relativePath}\n`;
outputStream.write(mdContent);
console.log(`Content appended for: ${relativePath}`);
} else {
console.log(`File does not exist: ${relativePath}`);
}
} catch (err) {
console.error(`Error reading file ${filePath}: ${err.message}`);
}
}
progress.increment();
});
}
module.exports = { appendFileContents };