-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistFolder.js
More file actions
100 lines (87 loc) · 2.77 KB
/
listFolder.js
File metadata and controls
100 lines (87 loc) · 2.77 KB
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
/*
* Author: Kinice
* Github: https://github.com/Kinice/PlyThings/tree/master/listFolder
*/
const fs = require('fs')
const path = require('path')
const baseDir = process.cwd()
const helpTips = '\n用法: node listFolder.js <paths> \n\n<paths>是目录路径,可以写多个,用空格隔开。'
let dirArr = []
let LISTFUNCTION = (dir,dirCount) => {
let finalObj = {}
let layer = 0
let finalStr = ''
try{
fs.readdirSync(dir)
}catch(e){
return console.error('Error: ',dir,'不是一个有效的路径',helpTips)
}
let listFolder = (dir, layer) => {
let parentObj = {}
let count = 0
let files = fs.readdirSync(dir)
for(let i = 0; i < files.length; i++){
let tempObj = {
name: '',
isFile: '',
subDir: {},
layer: layer
}
let stat = fs.statSync(path.join(dir,files[i]))
if(files[i].substr(0,1)=='.') continue
tempObj.name = files[i]
if(stat.isDirectory()){
tempObj.isFile = false
tempObj.subDir = listFolder(path.join(dir,files[i]),layer+1)
parentObj[count++] = tempObj
}else{
tempObj.isFile = true
tempObj.subDir = null
parentObj[count++] = tempObj
}
}
return parentObj
}
let generateStr = (obj, str) => {
for(let i in obj){
let tempStr = ''
for(let j = 0; j < obj[i].layer; j++){
tempStr += ' '
}
if(obj[i].isFile){
str += tempStr + '|-- ' + obj[i].name + '\n'
}else{
str += tempStr + '|-- ' + obj[i].name + '/\n'
}
if(obj[i].subDir){
str += generateStr(obj[i].subDir, '')
}
}
return str
}
console.log('正在执行第',dirCount+1,'个任务:')
console.log('正在生成目录树。。。')
finalObj = listFolder(dir, layer)
console.log('正在渲染目录列表。。。')
finalStr = generateStr(finalObj, finalStr)
console.log('正在写入文件:',path.basename(dir),'.txt 。。。')
fs.writeFile(path.basename(dir)+'.txt', finalStr, (err) => {
if(err) throw err
console.log('写入'+path.basename(dir)+'.txt'+'成功')
})
}
if(process.argv.length == 2){
console.log('无路径,默认输出当前目录')
LISTFUNCTION(baseDir,0)
}else{
if(process.argv[2] == '-h'){
console.log(helpTips)
}else{
for(let i = 2; i < process.argv.length; i++){
dirArr.push(process.argv[i])
}
}
}
for(let i = 0; i < dirArr.length; i++){
LISTFUNCTION(dirArr[i],i)
}