-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
50 lines (44 loc) · 1.22 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
import * as fs from 'fs/promises';
async function init() {
let result = []
try {
let res = await readFolder('src', result)
console.log('res', res)
} catch(e) {
console.log(e)
}
}
init()
async function readFolder(curPath, result) {
return new Promise(async (resove, reject) => {
if (curPath.includes('node_modules')) {
return resove(result)
}
try {
let files = await fs.readdir(curPath, {
withFileTypes: true // 不返回文件数组,返回文件 <fs.Dirent> 对象
})
console.log(files);
if (files && Array.isArray(files)) {
files.forEach(async item => {
console.log(item.name,item.isDirectory())
// 隐藏文件不处理
if (item.name.startsWith('.') || item.name.startsWith('@')) {
return
}
if (!result[curPath]) {
result[curPath] = []
}
if (item.isDirectory()) {
result[curPath].push(item.name)
console.log('result', result)
return await readFolder(`${curPath}/${item.name}`, result)
}
})
}
} catch (error) {
console.error('there was an error:', error.message);
reject(error)
}
})
}