-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree.js
56 lines (49 loc) · 1.55 KB
/
tree.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
const fs = require("fs");
const path = require("path");
const ignore = ['node_modules'];
const treePath = function(dir) {
const treeArr = [{
name: path.basename(dir),
str: path.basename(dir)
}];
const render = function(name,isLast,deep){
const line = deep.map(el=>`${el?'│':' '} `).join('');
const text = `${line}${isLast?'└─':'├─'} ${name}`;
return {
name: name,
str: text
};
}
const tree = function(target,deep=[]){
const child = fs.readdirSync(target).filter(el=>!el.startsWith('.'));
const direct = [];
const file = [];
child.forEach(function(el){
const dir = path.join(target,el);
const stat = fs.statSync(dir);
const flag = ignore.every(function(reg){
return !dir.includes(reg);
})
if(flag){
if(stat.isFile()){
file.push(el);
}else{
direct.push(el);
}
}
})
direct.forEach(function(el,i){
const dir = path.join(target,el);
const isLast = (i === direct.length -1) && (file.length === 0);
treeArr.push(render(el,isLast,deep));
tree(dir,[...deep,!isLast]);
})
file.forEach(function(el,i){
const isLast = i === file.length -1;
treeArr.push(render(el,isLast,deep));
})
}
tree(dir);
return treeArr;
}
module.exports = treePath;