forked from dunwu/java-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadFileList.js
More file actions
49 lines (44 loc) · 1.45 KB
/
Copy pathreadFileList.js
File metadata and controls
49 lines (44 loc) · 1.45 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
/**
* 读取所有md文件数据
*/
const fs = require('fs') // 文件模块
const path = require('path') // 路径模块
const docsRoot = path.join(__dirname, '..', '..', 'docs') // docs文件路径
function readFileList(dir = docsRoot, filesList = []) {
const files = fs.readdirSync(dir)
files.forEach((item, index) => {
let filePath = path.join(dir, item)
const stat = fs.statSync(filePath)
if (stat.isDirectory() && item !== '.vuepress') {
readFileList(path.join(dir, item), filesList) //递归读取文件
} else {
if (path.basename(dir) !== 'docs') {
// 过滤docs目录级下的文件
const filename = path.basename(filePath)
const fileNameArr = filename.split('.')
const firstDotIndex = filename.indexOf('.')
const lastDotIndex = filename.lastIndexOf('.')
let name = null,
type = null
if (fileNameArr.length === 2) {
// 没有序号的文件
name = fileNameArr[0]
type = fileNameArr[1]
} else if (fileNameArr.length >= 3) {
// 有序号的文件(或文件名中间有'.')
name = filename.substring(firstDotIndex + 1, lastDotIndex)
type = filename.substring(lastDotIndex + 1)
}
if (type === 'md') {
// 过滤非md文件
filesList.push({
name,
filePath
})
}
}
}
})
return filesList
}
module.exports = readFileList