/
index.js
188 lines (144 loc) · 4.52 KB
/
index.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* Created by Moyu on 16/9/11.
*/
const fs = require('fs')
const path = require('path')
const colors = require('colors/safe')
const NL = "\n"
let SP = ""
let fileNum = 0, dirNum = 0
function generateInfoStr(fileNum, dirNum) {
return `${dirNum} directories, ${fileNum} files.`
}
function isIgnoredFile(filename, ignoresFile) {
return ignoresFile.some(m => {
if(m == '')
return false;
return new RegExp(m).test(filename)
})
}
function isIgnoredDir(dirname, ignoresDir) {
return ignoresDir.some(m => {
if(m == '')
return false;
return new RegExp(m).test(dirname)
})
}
function isIgnored(stat, ignoresDir, ignoresFile) {
let f = stat.name
return /^\./.test(f)
|| stat.isDirectory() && isIgnoredDir(f, ignoresDir)
|| stat.isFile() && isIgnoredFile(f, ignoresFile)
}
function newLine(string) {
return string + NL;
}
/*
bootstrap/
├── css/
│ ├── bootstrap.css
│ ├── bootstrap.min.css
│ ├── bootstrap-theme.css
│ └── bootstrap-theme.min.css
├── js/
│ ├── bootstrap.js
│ └── bootstrap.min.js
├── fonts/
├── glyphicons-halflings-regular.eot
├── glyphicons-halflings-regular.svg
├── glyphicons-halflings-regular.ttf
└── glyphicons-halflings-regular.woff
bootstrap/
├── less/
├── js/
├── fonts/
├── dist/
│ ├── css/
│ ├── js/
│ └── fonts/
├── docs/
└── examples/
*/
function generateDirectoryStr(dirname, theme, maxLev, ignoresDir=[], ignoresFile=[],
isLine, sort, level=1, depth=[], SP='') {
function makeFileLineStr(depth, isLast, stat) {
return SP + depth.join('')
+ (isLast?theme.last:theme.node)
+ stat.name + (stat.isFile()?"":theme.dir_suffix);
}
if(maxLev && level > maxLev) {
return '';
}
let filenames = fs.readdirSync(dirname)
let stats = filenames.reduce((c, f) => {
let stat = fs.lstatSync(path.join(dirname, f))
stat.name = f
// remove ignored files
if(isIgnored(stat, ignoresDir, ignoresFile)) {
return c;
}
return c.concat(stat)
}, []).sort((a,b) => {
// default sort by filename
if(sort=='dir' || sort=='file') {
if(a.isDirectory()) {
if(b.isDirectory())
return a.name.localeCompare(b.name)
return sort=='dir' ? -1 : 1
} else {
if(b.isDirectory())
return sort=='dir' ? 1 : -1
return a.name.localeCompare(b.name)
}
} else {
return a.name.localeCompare(b.name)
}
})
let outString = level==1 ? SP + path.basename(path.resolve(dirname)) + theme.dir_suffix : ''
if(outString!='') {
isLine && console.log(colors.blue(outString))
outString = newLine(outString)
}
var tree = stats.reduce((prev, next, index, all) => {
let newDepthArr = level==1 ? [] : depth.slice(0)
let isLast = index == stats.length-1
let str = makeFileLineStr(newDepthArr, isLast, next)
isLine && console.log(colors.blue(str))
prev += newLine(str)
if(next.isDirectory()) {
let childpath = path.join(dirname, next.name)
if(isLast) {
newDepthArr.push(theme.indent)
} else {
newDepthArr.push(theme.pipe)
}
prev += generateDirectoryStr(childpath, theme,
maxLev, ignoresDir, ignoresFile, isLine, sort, level+1, newDepthArr, SP)
dirNum++
} else {
fileNum++
}
return prev
}, outString)
return level === 1 ? tree.replace(new RegExp( NL+"$"), '') : tree;
}
module.exports = function (options) {
dirNum = fileNum = 0
return {
treeString: generateDirectoryStr(
options.dir,
options.theme || JSON.parse(fs.readFileSync(path.resolve(__dirname, "./themes/default.json")).toString()),
options.maxLev || null,
options.ignoresDir || [],
options.ignoresFile || [],
options.isLine,
options.sort,
1,
[],
options.prefix || SP
),
infoString: generateInfoStr(fileNum, dirNum),
fileNum: fileNum,
DirNum: dirNum
}
}