-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (33 loc) · 1.48 KB
/
Copy pathindex.js
File metadata and controls
55 lines (33 loc) · 1.48 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
import fs from "fs";
import path from "path";
const getFolderStructure = (dirPath, indent = "", isRoot = true) => {
const name = path.basename(dirPath);
const stats = fs.statSync(dirPath);
const isDirectory = stats.isDirectory();
let result = isRoot ? name + "\n" : "";
if (isDirectory) {
const items = fs.readdirSync(dirPath);
const ignoreFile = items.find(name => name === "kyp.ignore");
let filestoexclude = [];
if (ignoreFile) {
const ignorePath = path.join(dirPath, ignoreFile);
filestoexclude = fs.readFileSync(ignorePath, 'utf-8').split(/\r?\n/).map(line => line.trim().replace(/\/$/, '')).filter(line => line.length > 0);
}
const filteredItems = items.filter(item =>
item !== ignoreFile && !filestoexclude.includes(item)
);
filteredItems.forEach((item, index) => {
const isLastIndex = index === filteredItems.length - 1;
const itemPath = path.join(dirPath, item);
const isChildDirectory = fs.statSync(itemPath).isDirectory();
const connector = isLastIndex ? "└── " : "├── ";
result += `${indent}${connector}${item}\n`;
if (isChildDirectory) {
const newIndent = indent + (isLastIndex ? " " : "│ ");
result += getFolderStructure(itemPath, newIndent, false);
}
});
}
return result;
};
export default getFolderStructure;