-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerateFileTree.js
More file actions
88 lines (73 loc) · 2.57 KB
/
generateFileTree.js
File metadata and controls
88 lines (73 loc) · 2.57 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
const fs = require("fs");
const path = require("path");
/**
* Recursively traverse the directory structure and build a file tree object.
* @param {string} dir - The directory to start from.
* @param {Set<string>} ignoreDirs - The set of directory names to ignore.
* @returns {Object} - The file tree object.
*/
function buildFileTree(dir, ignoreDirs) {
const fileTree = {};
const files = fs.readdirSync(dir);
files.forEach((file) => {
if (ignoreDirs.has(file)) return;
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
fileTree[file] = buildFileTree(filePath, ignoreDirs);
} else {
fileTree[file] = "file";
}
});
return fileTree;
}
/**
* Generate a file tree for a given repository.
* @param {string} repoPath - The path to the repository.
* @param {Set<string>} ignoreDirs - The set of directory names to ignore.
* @returns {Object} - The generated file tree.
*/
function generateFileTree(repoPath, ignoreDirs) {
return buildFileTree(repoPath, ignoreDirs);
}
/**
* Format a file tree object into a tree-like string.
* @param {Object} tree - The file tree object.
* @param {string} prefix - The prefix for the current level.
* @returns {string} - The formatted tree string.
*/
function formatFileTree(tree, prefix = "") {
let treeString = "";
const keys = Object.keys(tree);
keys.forEach((key, index) => {
const isLast = index === keys.length - 1;
const newPrefix = prefix + (isLast ? "└── " : "├── ");
treeString += `${newPrefix}${key}`;
if (tree[key] === "file") {
treeString += "\n";
} else {
treeString +=
"/\n" +
formatFileTree(tree[key], prefix + (isLast ? " " : "│ "));
}
});
return treeString;
}
// Example usage:
// Replace 'c:/Users/charlie.palmer/petooly-mobile-application' with the actual path to the repository
const repoPath = "c:/Users/charlie.palmer/petooly-mobile-application"; // Use forward slashes
// Set of directories to ignore
const ignoreDirs = new Set([
"node_modules",
".git",
".husky",
"android",
"ios",
]);
const fileTree = generateFileTree(repoPath, ignoreDirs);
const formattedTree = formatFileTree(fileTree);
console.log(formattedTree);
// Optionally, write the formatted tree to a text file
const outputFilePath = path.join(repoPath, "fileTree.txt");
fs.writeFileSync(outputFilePath, formattedTree);
console.log(`File tree written to ${outputFilePath}`);