readDirStructureSync【获取所有子文件,递归】
描述
根据路径 获取其下所有子文件包括文件夹路径,返回平面list 和 树状tree 🎉🎉🎉
# 1.示例
const { readDirStructureSync } = require('sf-utils2/lib/nodejs')
const fs = require('fs')
fs.writeFileSync(readDirStructureSync(path.join(__dirname, '../public/icons/font-awesome-4.7.0')
// 返回分别两个字段
// list 平面结构
// tree 树状结构
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
源码,点开查看 👈
{
"list": [
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/css",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"ext": "",
"type": "dir",
"relativePath": "css",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
},
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/css/font-awesome.min.css",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/css",
"ext": ".css",
"type": "file",
"relativePath": "css/font-awesome.min.css",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
},
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"ext": "",
"type": "dir",
"relativePath": "fonts",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
},
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts/FontAwesome.otf",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts",
"ext": ".otf",
"type": "file",
"relativePath": "fonts/FontAwesome.otf",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
},
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts/fontawesome-webfont.eot",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts",
"ext": ".eot",
"type": "file",
"relativePath": "fonts/fontawesome-webfont.eot",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
},
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts/fontawesome-webfont.svg",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts",
"ext": ".svg",
"type": "file",
"relativePath": "fonts/fontawesome-webfont.svg",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
},
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts/fontawesome-webfont.ttf",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts",
"ext": ".ttf",
"type": "file",
"relativePath": "fonts/fontawesome-webfont.ttf",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
},
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts/fontawesome-webfont.woff",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts",
"ext": ".woff",
"type": "file",
"relativePath": "fonts/fontawesome-webfont.woff",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
},
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts/fontawesome-webfont.woff2",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts",
"ext": ".woff2",
"type": "file",
"relativePath": "fonts/fontawesome-webfont.woff2",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
},
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/index.json",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"ext": ".json",
"type": "file",
"relativePath": "index.json",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
}
],
"tree": [
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/css",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"ext": "",
"type": "dir",
"relativePath": "css",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false,
"children": [
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/css/font-awesome.min.css",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/css",
"ext": ".css",
"type": "file",
"relativePath": "css/font-awesome.min.css",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
}
]
},
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"ext": "",
"type": "dir",
"relativePath": "fonts",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false,
"children": [
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts/FontAwesome.otf",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts",
"ext": ".otf",
"type": "file",
"relativePath": "fonts/FontAwesome.otf",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
},
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts/fontawesome-webfont.eot",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts",
"ext": ".eot",
"type": "file",
"relativePath": "fonts/fontawesome-webfont.eot",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
},
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts/fontawesome-webfont.svg",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts",
"ext": ".svg",
"type": "file",
"relativePath": "fonts/fontawesome-webfont.svg",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
},
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts/fontawesome-webfont.ttf",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts",
"ext": ".ttf",
"type": "file",
"relativePath": "fonts/fontawesome-webfont.ttf",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
},
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts/fontawesome-webfont.woff",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts",
"ext": ".woff",
"type": "file",
"relativePath": "fonts/fontawesome-webfont.woff",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
},
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts/fontawesome-webfont.woff2",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/fonts",
"ext": ".woff2",
"type": "file",
"relativePath": "fonts/fontawesome-webfont.woff2",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
}
]
},
{
"path": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0/index.json",
"parentPath": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"ext": ".json",
"type": "file",
"relativePath": "index.json",
"rootDir": "/Users/bianpengfei/卞鹏飞/工作空间/中电金信软件公司/webvue/jeplus/jeplus-hub/public/icons/font-awesome-4.7.0",
"isRoot": false
}
]
}
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# 2.入参说明
| 参数 | 说明 | 类型 | 是否必填 | 默认值 |
|---|---|---|---|---|
| dir | 目录地址 | String | 是 | ------ |
# 返回对象中字段说明
| 参数 | 说明 | 类型 | 是否必填 | 默认值 |
|---|---|---|---|---|
| path | 绝对路径 | String | ------ | |
| parentPath | 父目录 绝对路径 | String | ------ | |
| ext | 拓展名 | String | ------ | |
| dir | 目录地址 | String | ------ | |
| type | 类型 file 或 dir | String | ------ | |
| relativePath | 相对路径 | String | ------ | |
| rootDir | 根路径 | String | ------ | |
| isRoot | 是否根路径文件夹 | Boolean | ------ |
# 3.源码
const path2 = require('path')
const fs = require('fs')
const { listToTree, deepClone } = require('sf-utils2')
/**
* 根据路径 获取其下所有子文件包括文件夹路径,返回平面list 和 树状tree
* @param dir
* @returns {{tree: *, list: {path: *, relativePath: *, id: *, type: *, parentId: string}[]}}
*/
function readDirStructureSync(dir = '') {
const itors = (dirName, result = []) => {
const dirStat = fs.existsSync(dirName) && fs.statSync(dirName)
if (dirStat) {
if (dirStat?.isDirectory?.()) {
fs.readdirSync(dirName).forEach(v => {
const absPath = path2.join(dirName, v)
const stat = fs.statSync(path2.join(absPath))
if (stat.isDirectory()) {
// 是目录,即有children
result.push({ path: absPath, type: 'dir', rootDir: dir })
itors(absPath, result)
} else if (stat.isFile()) {
// 是文件
result.push({ path: absPath, type: 'file', rootDir: dir })
} else {
result.push({ path: absPath, type: 'unknown', rootDir: dir })
}
})
} else if (dirStat.isFile?.()) {
result.push({ path: dirName, type: 'file', rootDir: dir })
}
}
return result
}
const list = itors(dir).map(v => {
const parentPath = path2.join('/', v.path.split('/').slice(0, -1).join('/'))
return {
path: v.path, // 绝对路径
parentPath, // 父目录 绝对路径
ext: path2.extname(v.path), // 拓展名
type: v.type, // 类型 file 或 dir
relativePath: v.path.replace(dir, '').split('/').filter(Boolean).join('/'), // 相对路径
rootDir: dir, // 根路径
isRoot: v.path === dir // 是否根路径文件夹
}
})
const tree = listToTree({
list: deepClone(list),
root: dir,
props: { id: 'path', parentId: 'parentPath', children: 'children' }
})
return {
list,
tree
}
}
const ROOT_PATH = path2.resolve(__dirname, '../../')
module.exports = readDirStructureSync
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
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
上次更新: 2025/07/01, 14:52:29