forked from yangshun/tree-node-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree.js
144 lines (124 loc) · 3 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict';
const fs = require('fs');
const nodePath = require('path');
const DEFAULT_OPTIONS = {
allFiles: false,
dirsFirst: false,
dirsOnly: false,
exclude: [],
maxDepth: Number.POSITIVE_INFINITY,
reverse: false,
trailingSlash: false,
};
const SYMBOLS = {
BRANCH: '├── ',
EMPTY: '',
INDENT: ' ',
LAST_BRANCH: '└── ',
VERTICAL: '│ ',
};
const EXCLUDED_PATTERNS = [/\.DS_Store/];
function isHiddenFile(filename) {
return filename[0] === '.';
}
function print(
filename,
path,
currentDepth,
precedingSymbols,
options,
isLast,
) {
const isFile = fs.lstatSync(path).isFile();
const isDir = !isFile;
const lines = [];
// Do not show these regardless.
for (let i = 0; i < EXCLUDED_PATTERNS.length; i++) {
if (EXCLUDED_PATTERNS[i].test(path)) {
return lines;
}
}
// Handle directories only.
if (isFile && options.dirsOnly) {
return lines;
}
// Handle excluded patterns.
for (let i = 0; i < options.exclude.length; i++) {
if (options.exclude[i].test(path)) {
return lines;
}
}
// Handle showing of all files.
if (!options.allFiles && isHiddenFile(filename)) {
return lines;
}
// Handle max depth.
if (currentDepth > options.maxDepth) {
return lines;
}
// Handle current file.
const line = [precedingSymbols];
if (currentDepth >= 1) {
line.push(isLast ? SYMBOLS.LAST_BRANCH : SYMBOLS.BRANCH);
}
line.push(filename);
if (isDir && options.trailingSlash) {
line.push('/');
}
lines.push(line.join(''));
if (isFile) {
return lines;
}
// Contents of a directory.
let contents = fs.readdirSync(path);
if (options.reverse) {
contents.reverse();
}
if (options.dirsOnly) {
// We have to filter here instead of at the start of the function
// because we need to know how many non-directories there are before
// we even start recursing.
contents = contents.filter(
file => !fs.lstatSync(nodePath.join(path, file)).isFile(),
);
}
// Sort directories first.
if (options.dirsFirst) {
const dirs = contents.filter(
content => !fs.lstatSync(nodePath.join(path, content)).isFile(),
);
const files = contents.filter(content =>
fs.lstatSync(nodePath.join(path, content)).isFile(),
);
contents = [...dirs, ...files];
}
contents.forEach((content, index) => {
const isCurrentLast = index === contents.length - 1;
const linesForFile = print(
content,
nodePath.join(path, content),
currentDepth + 1,
precedingSymbols +
(currentDepth >= 1
? isLast
? SYMBOLS.INDENT
: SYMBOLS.VERTICAL
: SYMBOLS.EMPTY),
options,
isCurrentLast,
);
lines.push(...linesForFile);
});
return lines;
}
function tree(path, options) {
const combinedOptions = { ...DEFAULT_OPTIONS, ...options };
return print(
nodePath.basename(nodePath.join(process.cwd(), path)),
path,
0,
'',
combinedOptions,
).join('\n');
}
module.exports = tree;