-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-summary.js
57 lines (50 loc) · 1.49 KB
/
gen-summary.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
let fs = require("fs")
let path = require("path")
let finalContent = `# Summary\n- [index](README.md)\n\n` //最终输出到文件的内容
let baseDir = "." //遍历的起始文件夹
let exceptDir = ['_book', '.git', 'node_modules', 'images'] //排除的文件
let targetFile = './SUMMARY.md' //目标文件
//start
let start = () => {
scan(baseDir, 0)
fs.writeFileSync(targetFile, finalContent);
}
//递归遍历
let scan = (dir, num) => {
fs.readdirSync(dir).forEach((it) => {
if (exceptDir.includes(it)) return
let isFile = fs.statSync(path.join(dir, it)).isFile()
if (isFile) {
if (dir == '.') return //首层的文件排除
generateLine(num, dir, it)
} else {
generateLine(num, it, null)
scan(`${dir}/${it}`, num + 1)
}
})
}
//生成一行数据
let generateLine = (num, dir, file) => {
let blank = getBlank(num)
if (file == null) {
appendLine(`${blank}* ${dir}\n`)
} else {
let fileName = file.substring(0, file.lastIndexOf("."))
appendLine(`${blank}* [${fileName}](${dir}/${file})\n`)
}
}
//获取空格
let getBlank = (num) => {
let result = ""
let i = 0
while (i++ < num) {
result += " "
}
return result
}
//追加一列,line需自带换行
let appendLine = (line) => {
finalContent += line
}
start()
console.log("成功生成目录");