-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextractAndSaveTree.js
44 lines (35 loc) · 1.39 KB
/
extractAndSaveTree.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
const fs = require('fs');
const path = require('path');
// Function to get directory structure
const getDirectoryStructure = (dir, prefix = '', exclude = []) => {
let result = `${prefix}${path.basename(dir)}\n`;
const items = fs.readdirSync(dir);
items.forEach((item, index) => {
const itemPath = path.join(dir, item);
const isLastItem = index === items.length - 1;
const newPrefix = isLastItem ? `${prefix}└── ` : `${prefix}├── `;
const subPrefix = isLastItem ? `${prefix} ` : `${prefix}│ `;
if (exclude.includes(item)) {
return;
}
result += `${newPrefix}${item}\n`;
if (fs.statSync(itemPath).isDirectory()) {
result += getDirectoryStructure(itemPath, subPrefix, exclude);
}
});
return result;
};
// Main function to save directory structure to a text file
const main = () => {
const dirPath = __dirname; // Root directory of your project
const outputFile = path.join(__dirname, 'directory_structure.txt');
const exclude = ['.git', 'node_modules', 'dist', 'build']; // Directories to exclude
try {
const structure = getDirectoryStructure(dirPath, '', exclude);
fs.writeFileSync(outputFile, structure);
console.log(`Directory structure saved to ${outputFile}`);
} catch (error) {
console.error('Error:', error);
}
};
main();