forked from qhx0807/file-tree-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (53 loc) · 1.35 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
#!/usr/bin/env node
const fs = require('fs')
const { resolve } = require('path')
const charSet = {
node: '├──',
pipe: '│ ',
last: '└──'
}
const fileMap = (dir, deep) => {
if (dir.indexOf('node_modules') > -1 || dir.indexOf('.git') > -1) return false
let files = fs.readdirSync(dir)
let maps = []
files.forEach(file => {
const filepath = resolve(dir, file)
const isdir = fs.statSync(filepath).isDirectory()
const obj = {
name: file,
isdir: isdir,
deep: deep,
children: []
}
if (isdir) {
let d = deep + 1
obj.children = fileMap(filepath, d)
}
maps.push(obj)
})
return maps
}
const filestack = fileMap(process.cwd(), 0)
const fileOutPutLog = (arr) => {
let str = ''
arr.forEach((item, index) => {
let i = 0
while (i < item.deep) {
str += charSet.pipe
i++
}
str += (index === arr.length - 1 && item.children.length === 0) ? charSet.last : charSet.node
str += item.isdir ? ` /${item.name}/\r\n` : ` ${item.name}\r\n`
if (item.children.length > 0) {
str += fileOutPutLog(item.children)
}
})
return str
}
const contents = fileOutPutLog(filestack)
fs.writeFile(process.cwd() + '/filetree.txt', contents, function (err) {
if (err) {
return console.log(err)
}
console.log('-------目录树文件filetree.txt已创建-------')
})