-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-summary.js
76 lines (64 loc) · 2.79 KB
/
generate-summary.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const fs = require('fs-extra');
const path = require('path');
const excludedFolders = [
/* Add the folders you want to exclude. This will exclude all files
and folders in the folder. Only works for folders on same level as
'generate-summary.js'. For nested folders, use 'excludedPaths'
instead.*/
// 'node_modules',
// '.git',
];
const excludedFiles = [
/* Add exact file name if on same level as 'generate-summary.js' */
//'.env',
/* Add exact file path if not on same level as 'generate-summary.js' */
//'./app/Console/Kernel.php',
];
const excludedPaths = [
/* Add the paths you want to exclude. This will exclude all files and
folders in the path */
// 'app/Http/Middleware/',
];
// Output file path
const outputPath = './output.txt';
function shouldBeExcluded(filePath) {
return (
excludedPaths.some(excludedPath => filePath.includes(excludedPath)) ||
excludedFiles.some(excludedFile => {
const relativeFilePath = path.relative('.', excludedFile);
return relativeFilePath === filePath || filePath === excludedFile; // Add exact file match check
})
);
}
function generateFolderTree(folder, parentFolder, level) {
const folderPath = `${parentFolder}/${folder}`;
const files = fs.readdirSync(folderPath).filter(file => {
const filePath = `${folderPath}/${file}`;
const relativeFilePath = filePath.slice(2);
const excludeFolder = excludedFolders.includes(file) && fs.statSync(filePath).isDirectory();
const excludeFile = excludedFiles.includes(file) && !fs.statSync(filePath).isDirectory();
return !excludeFolder && !excludeFile && !shouldBeExcluded(relativeFilePath);
});
const prefix = ' '.repeat(level) + '|_';
files.forEach(file => {
const filePath = `${folderPath}/${file}`;
const isDirectory = fs.statSync(filePath).isDirectory();
if (!shouldBeExcluded(filePath.slice(2))) {
if (!isDirectory) {
const fileContent = fs.readFileSync(filePath, 'utf-8');
const writeStream = fs.createWriteStream(outputPath, { flags: 'a' });
writeStream.setMaxListeners(20); // Increase max listeners
writeStream.write(`${prefix}${file}\n\n${fileContent}\n\n`);
writeStream.close();
} else {
const writeStream = fs.createWriteStream(outputPath, { flags: 'a' });
writeStream.setMaxListeners(20); // Increase max listeners
writeStream.write(`${prefix}${file}\n`);
writeStream.close();
generateFolderTree(file, folderPath, level + 1);
}
}
});
}
fs.writeFileSync(outputPath, ''); // Clear the output file before writing
generateFolderTree('.', '.', 0);