-
Notifications
You must be signed in to change notification settings - Fork 0
/
treer.js
130 lines (113 loc) · 3.19 KB
/
treer.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
#!/usr/bin/env node
const program = require('commander')
const package = require('./package.json')
const fs = require('fs')
program
.version(package.version)
.option('-d, --directory [dir]', 'Please specify a directory to generate structure tree', process.cwd())
.option('-i, --ignore [ig]', 'You can ignore specific directory name')
.option('-e, --export [epath]', 'export into file')
.parse(process.argv);
let ignoreRegex = null
if (program.ignore) {
//trim program.ignore
program.ignore = program.ignore.replace(/^\s*|\s*$/g, '')
if (/^\/.+\/$/.test(program.ignore)) {
program.ignore = program.ignore.replace(/(^\/)|(\/$)/g, '')
ignoreRegex = new RegExp(program.ignore, "")
} else {
//escape special character
program.ignore = program.ignore.replace(/[-[\]{}*+?,\\^$|#\s]/g, '|');
ignoreRegex = new RegExp("^" + program.ignore + "$", "")
}
}
const dirToJson = (path) => {
if (ignoreRegex.test(path)) {
return;
}
let stats = fs.lstatSync(path),
structure = {}
if (stats.isDirectory()) {
let dir = fs.readdirSync(path)
if (ignoreRegex) {
dir = dir.filter((val) => {
return !ignoreRegex.test(val)
})
}
dir = dir.map((child) => {
let childStats = fs.lstatSync(path + '/' + child)
return childStats.isDirectory() ? dirToJson(path + '/' + child) : child
})
let dirName = path.replace(/.*\/(?!$)/g, '')
structure[dirName] = sortDir(dir)
} else {
let fileName = path.replace(/.*\/(?!$)/g, '')
return fileName
}
return structure
}
const result = dirToJson(program.directory)
const characters = {
border: '|',
contain: '├',
line: '─',
last: '└'
}
let outputString = ''
const drawDirTree = (data, placeholder) => {
let {
border,
contain,
line,
last
} = characters
for (let i in data) {
if (typeof data[i] === 'string') {
// console.log(placeholder + data[i])
outputString += '\n' + placeholder + data[i]
} else if (Array.isArray(data[i])) {
// console.log(placeholder + i)
outputString += '\n' + placeholder + i
placeholder = placeholder.replace(new RegExp(`${contain}`, "g"), border)
placeholder = placeholder.replace(new RegExp(`${line}`, "g"), " ")
placeholder = placeholder + Array(Math.ceil(i.length / 2)).join(" ") + contain + line
placeholder = placeholder.replace(new RegExp("^ +", 'g'), "")
data[i].forEach((val, idx, arr) => {
let pl = placeholder
//if the idx is the last one, change the character
if (idx === (arr.length - 1)) {
let regex = new RegExp(`${contain}${line}$`, "g")
pl = placeholder.replace(regex, last)
}
if (typeof val === 'string') {
// console.log(pl + val)
outputString += '\n' + pl + val
} else {
let pl = placeholder
drawDirTree(val, pl)
}
})
}
}
}
drawDirTree(result, "")
outputString = outputString.replace(/^\n/,'')
// console.log(outputString)
// 写出文件到制定位置
if (program.export) {
fs.writeFile(program.export, outputString, (err) => {
if (err) throw err;
console.log('\n\n'+ 'The result has been saved into ' + program.export);
});
}
function sortDir(arr) {
let i = arr.length - 1
while (i >= 0) {
if (typeof arr[i] === 'object') {
let obj = arr.splice(i, 1)
arr.push(obj[0])
}
i--
}
return arr
}