@magic/fs 0.0.27 → 0.0.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -13
- package/package.json +13 -13
- package/src/getDirectories.mjs +40 -14
- package/src/getFilePath.mjs +7 -3
- package/src/getFiles.mjs +44 -12
package/README.md
CHANGED
|
@@ -12,12 +12,15 @@ exports all fs.promises + exists + mkdirp + rmrf + getFiles + getDirs functions.
|
|
|
12
12
|
[html-docs](https://magic.github.io/fs)
|
|
13
13
|
|
|
14
14
|
### installation
|
|
15
|
+
|
|
15
16
|
be in a nodejs project
|
|
17
|
+
|
|
16
18
|
```bash
|
|
17
19
|
npm install @magic/fs
|
|
18
20
|
```
|
|
19
21
|
|
|
20
22
|
### import
|
|
23
|
+
|
|
21
24
|
```javascript
|
|
22
25
|
import fs from '@magic/fs'
|
|
23
26
|
|
|
@@ -31,7 +34,9 @@ run()
|
|
|
31
34
|
```
|
|
32
35
|
|
|
33
36
|
### promises
|
|
37
|
+
|
|
34
38
|
promises from fs:
|
|
39
|
+
|
|
35
40
|
```
|
|
36
41
|
access
|
|
37
42
|
copyFile
|
|
@@ -65,6 +70,7 @@ rmDir
|
|
|
65
70
|
```
|
|
66
71
|
|
|
67
72
|
### export overloads:
|
|
73
|
+
|
|
68
74
|
```javascript
|
|
69
75
|
rmdir, rmDir
|
|
70
76
|
readfile, readFile
|
|
@@ -74,6 +80,7 @@ readdir, readDir
|
|
|
74
80
|
### Additional functions:
|
|
75
81
|
|
|
76
82
|
#### mkdirp
|
|
83
|
+
|
|
77
84
|
same as mkdir -p on unix
|
|
78
85
|
|
|
79
86
|
```javascript
|
|
@@ -81,17 +88,21 @@ await fs.mkdirp('./path/to/dir')
|
|
|
81
88
|
```
|
|
82
89
|
|
|
83
90
|
#### rmrf
|
|
91
|
+
|
|
84
92
|
same as rm -rf on unix.
|
|
85
93
|
|
|
86
94
|
**will not work outside process.cwd()**
|
|
95
|
+
|
|
87
96
|
```javascript
|
|
88
97
|
await fs.rmrf('./path/to/dir')
|
|
89
98
|
```
|
|
90
99
|
|
|
91
100
|
#### exists
|
|
101
|
+
|
|
92
102
|
same as fs.exists, but promisified and ready for esmodules.
|
|
93
103
|
|
|
94
104
|
#### getDirectories
|
|
105
|
+
|
|
95
106
|
get a list of directories in a directory,
|
|
96
107
|
recursively.
|
|
97
108
|
|
|
@@ -122,6 +133,7 @@ run()
|
|
|
122
133
|
```
|
|
123
134
|
|
|
124
135
|
#### getFiles
|
|
136
|
+
|
|
125
137
|
get a list of files in a directory,
|
|
126
138
|
recursively.
|
|
127
139
|
|
|
@@ -145,6 +157,7 @@ run()
|
|
|
145
157
|
```
|
|
146
158
|
|
|
147
159
|
#### getFileType
|
|
160
|
+
|
|
148
161
|
get the file type of a file,
|
|
149
162
|
based on extension,
|
|
150
163
|
and defaulting to "txt"
|
|
@@ -157,39 +170,47 @@ console.log(fileType, fileType === 'html')
|
|
|
157
170
|
|
|
158
171
|
const nonFileType = fs.getFileType()
|
|
159
172
|
console.log(nonFileType, nonFileType === 'txt')
|
|
160
|
-
|
|
161
173
|
```
|
|
162
174
|
|
|
163
175
|
### changelog
|
|
164
176
|
|
|
165
177
|
#### 0.0.1
|
|
178
|
+
|
|
166
179
|
first release
|
|
167
180
|
|
|
168
181
|
#### 0.0.2
|
|
169
|
-
|
|
170
|
-
|
|
182
|
+
|
|
183
|
+
- bump required node version
|
|
184
|
+
- update dependencies
|
|
171
185
|
|
|
172
186
|
#### 0.0.3
|
|
187
|
+
|
|
173
188
|
better error messages
|
|
174
189
|
|
|
175
190
|
#### 0.0.4
|
|
191
|
+
|
|
176
192
|
rmrf returns true if directory does not exist.
|
|
177
193
|
|
|
178
194
|
#### 0.0.5
|
|
195
|
+
|
|
179
196
|
use @magic/mime-types to export contentTypes
|
|
180
197
|
|
|
181
198
|
#### 0.0.6
|
|
199
|
+
|
|
182
200
|
bump required node version to 14.2.0
|
|
183
201
|
|
|
184
202
|
#### 0.0.7
|
|
203
|
+
|
|
185
204
|
rmrf: add dryRun option
|
|
186
205
|
|
|
187
206
|
#### 0.0.8
|
|
207
|
+
|
|
188
208
|
update dependencies
|
|
189
209
|
|
|
190
210
|
#### 0.0.9
|
|
191
|
-
|
|
192
|
-
|
|
211
|
+
|
|
212
|
+
- remove unused imports from getDirectories
|
|
213
|
+
- getDirectories and getFiles now accept a number as second argument.
|
|
193
214
|
|
|
194
215
|
```
|
|
195
216
|
// if a number is given instead of true/false, then this is the depth of recursion.
|
|
@@ -197,65 +218,92 @@ getFiles(directory, 2) // two levels down
|
|
|
197
218
|
```
|
|
198
219
|
|
|
199
220
|
#### 0.0.10
|
|
200
|
-
|
|
201
|
-
|
|
221
|
+
|
|
222
|
+
- bump required node version to 14.15.4
|
|
223
|
+
- update dependencies
|
|
202
224
|
|
|
203
225
|
##### 0.0.11
|
|
226
|
+
|
|
204
227
|
update dependencies (@magic/mime-types)
|
|
205
228
|
|
|
206
229
|
##### 0.0.12
|
|
230
|
+
|
|
207
231
|
export all functions from native fs
|
|
208
232
|
|
|
209
233
|
##### 0.0.13
|
|
210
|
-
|
|
211
|
-
|
|
234
|
+
|
|
235
|
+
- getDirectories, getFiles: default root to process.cwd() if first argument is an array and root is not passed
|
|
236
|
+
- update dependencies
|
|
212
237
|
|
|
213
238
|
##### 0.0.14
|
|
239
|
+
|
|
214
240
|
update dependencies (@magic/mime-types)
|
|
215
241
|
|
|
216
242
|
##### 0.0.15
|
|
243
|
+
|
|
217
244
|
update @magic/types to avoid circular dependency
|
|
218
245
|
|
|
219
246
|
##### 0.0.16
|
|
247
|
+
|
|
220
248
|
add deprecation warning for calls to fs.getDirectories, fs.getFilePath and fs.getFiles that do not use an options object
|
|
221
249
|
|
|
222
250
|
##### 0.0.17
|
|
251
|
+
|
|
223
252
|
update dependencies
|
|
224
253
|
|
|
225
254
|
##### 0.0.18
|
|
255
|
+
|
|
226
256
|
update mime-types
|
|
227
257
|
|
|
228
258
|
##### 0.0.19
|
|
259
|
+
|
|
229
260
|
update dependencies
|
|
230
261
|
|
|
231
262
|
##### 0.0.20
|
|
263
|
+
|
|
232
264
|
update dependencies
|
|
233
265
|
|
|
234
266
|
##### 0.0.21
|
|
235
|
-
|
|
236
|
-
|
|
267
|
+
|
|
268
|
+
- add missing @magic/fs dependency
|
|
269
|
+
- update dependencies
|
|
237
270
|
|
|
238
271
|
##### 0.0.22
|
|
272
|
+
|
|
239
273
|
update dependencies
|
|
240
274
|
|
|
241
275
|
##### 0.0.23
|
|
276
|
+
|
|
242
277
|
update dependencies
|
|
243
278
|
|
|
244
279
|
##### 0.0.24
|
|
280
|
+
|
|
245
281
|
getFiles: allow files to be filtered by extension, using the .extension key of the second parameter
|
|
246
282
|
|
|
247
283
|
##### 0.0.25
|
|
284
|
+
|
|
248
285
|
update dependencies
|
|
249
286
|
|
|
250
287
|
##### 0.0.26
|
|
288
|
+
|
|
251
289
|
update dependencies
|
|
252
290
|
|
|
253
291
|
##### 0.0.27
|
|
292
|
+
|
|
254
293
|
update dependencies
|
|
255
294
|
|
|
256
|
-
##### 0.0.28
|
|
257
|
-
...
|
|
295
|
+
##### 0.0.28
|
|
258
296
|
|
|
297
|
+
update dependencies
|
|
298
|
+
|
|
299
|
+
##### 0.0.29
|
|
300
|
+
|
|
301
|
+
- update dependencies
|
|
302
|
+
- getFiles and getDirectories now accept maxDepth and minDepth config args
|
|
303
|
+
|
|
304
|
+
##### 0.0.30 - unreleased
|
|
305
|
+
|
|
306
|
+
...
|
|
259
307
|
|
|
260
308
|
[npm-image]: https://img.shields.io/npm/v/@magic/fs.svg
|
|
261
309
|
[npm-url]: https://www.npmjs.com/package/@magic/fs
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magic/fs",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.29",
|
|
4
4
|
"author": "Wizards & Witches",
|
|
5
5
|
"description": "nodejs fs promises + goodies",
|
|
6
6
|
"license": "AGPL-3.0",
|
|
@@ -33,20 +33,20 @@
|
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@magic-modules/git-badges": "0.0.12",
|
|
36
|
-
"@magic-modules/light-switch": "0.0.
|
|
37
|
-
"@magic-modules/no-spy": "0.0.
|
|
38
|
-
"@magic-modules/pre": "0.0.
|
|
39
|
-
"@magic-themes/docs": "0.0.
|
|
40
|
-
"@magic/core": "0.0.
|
|
41
|
-
"@magic/format": "0.0.
|
|
42
|
-
"@magic/test": "0.2.
|
|
36
|
+
"@magic-modules/light-switch": "0.0.12",
|
|
37
|
+
"@magic-modules/no-spy": "0.0.9",
|
|
38
|
+
"@magic-modules/pre": "0.0.12",
|
|
39
|
+
"@magic-themes/docs": "0.0.15",
|
|
40
|
+
"@magic/core": "0.0.155",
|
|
41
|
+
"@magic/format": "0.0.52",
|
|
42
|
+
"@magic/test": "0.2.18"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@magic/deep": "0.1.
|
|
46
|
-
"@magic/error": "0.0.
|
|
47
|
-
"@magic/log": "0.1.
|
|
48
|
-
"@magic/mime-types": "0.0.
|
|
49
|
-
"@magic/types": "0.1.
|
|
45
|
+
"@magic/deep": "0.1.16",
|
|
46
|
+
"@magic/error": "0.0.17",
|
|
47
|
+
"@magic/log": "0.1.18",
|
|
48
|
+
"@magic/mime-types": "0.0.18",
|
|
49
|
+
"@magic/types": "0.1.23"
|
|
50
50
|
},
|
|
51
51
|
"files": [
|
|
52
52
|
"src"
|
package/src/getDirectories.mjs
CHANGED
|
@@ -20,17 +20,31 @@ export const getDirectories = async (dir, depth = {}, root = 'deprecated') => {
|
|
|
20
20
|
root = false
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
let minDepth = 0
|
|
24
|
+
let maxDepth = 200
|
|
25
|
+
|
|
23
26
|
let noRoot = false
|
|
24
|
-
if (
|
|
27
|
+
if (depth === false) {
|
|
28
|
+
maxDepth = 1
|
|
29
|
+
} else if (is.number(depth)) {
|
|
30
|
+
maxDepth = depth
|
|
31
|
+
} else if (!is.empty(depth) && is.objectNative(depth)) {
|
|
25
32
|
root = root || depth?.root
|
|
26
33
|
noRoot = depth?.noRoot
|
|
27
|
-
depth = depth?.depth
|
|
28
|
-
}
|
|
29
34
|
|
|
30
|
-
|
|
31
|
-
|
|
35
|
+
if (depth.hasOwnProperty('depth')) {
|
|
36
|
+
maxDepth = depth.depth
|
|
37
|
+
} else if (depth.hasOwnProperty('maxDepth')) {
|
|
38
|
+
maxDepth = depth.maxDepth
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (depth.hasOwnProperty('minDepth')) {
|
|
42
|
+
minDepth = depth.minDepth
|
|
43
|
+
}
|
|
32
44
|
}
|
|
33
45
|
|
|
46
|
+
maxDepth = parseInt(maxDepth)
|
|
47
|
+
|
|
34
48
|
if (!is.array(dir) && !is.string(dir)) {
|
|
35
49
|
throw error(`${libName}: need an array or a string as first argument`, 'E_ARG_TYPE')
|
|
36
50
|
}
|
|
@@ -50,17 +64,15 @@ export const getDirectories = async (dir, depth = {}, root = 'deprecated') => {
|
|
|
50
64
|
try {
|
|
51
65
|
if (is.array(dir)) {
|
|
52
66
|
const dirs = await Promise.all(
|
|
53
|
-
dir.map(async f => await getDirectories(f, {
|
|
67
|
+
dir.map(async f => await getDirectories(f, { maxDepth, minDepth, root, noRoot })),
|
|
54
68
|
)
|
|
55
69
|
|
|
56
70
|
return deep.flatten(...dirs).filter(a => a)
|
|
57
71
|
}
|
|
58
72
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return []
|
|
63
|
-
}
|
|
73
|
+
const currentDepth = dir.replace(root, '').split(path.sep).length
|
|
74
|
+
if (is.number(maxDepth) && currentDepth - 1 > maxDepth) {
|
|
75
|
+
return []
|
|
64
76
|
}
|
|
65
77
|
|
|
66
78
|
const dirContent = await fs.readdir(dir)
|
|
@@ -71,7 +83,7 @@ export const getDirectories = async (dir, depth = {}, root = 'deprecated') => {
|
|
|
71
83
|
throw error(`${libName}: path was not a string: ${file}`, 'E_ARG_TYPE')
|
|
72
84
|
}
|
|
73
85
|
|
|
74
|
-
let filePath = await getFilePath(getDirectories, dir, file, {
|
|
86
|
+
let filePath = await getFilePath(getDirectories, dir, file, { maxDepth, minDepth, root })
|
|
75
87
|
|
|
76
88
|
if (filePath) {
|
|
77
89
|
if (!is.array(filePath)) {
|
|
@@ -103,9 +115,23 @@ export const getDirectories = async (dir, depth = {}, root = 'deprecated') => {
|
|
|
103
115
|
finalDirs = [dir, ...finalDirs]
|
|
104
116
|
}
|
|
105
117
|
|
|
106
|
-
const finalized = deep.flatten(finalDirs).filter(
|
|
118
|
+
const finalized = deep.flatten(finalDirs).filter(dir => {
|
|
119
|
+
if (!dir) {
|
|
120
|
+
return false
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (is.number(minDepth)) {
|
|
124
|
+
const currentDepth = dir.replace(root, '').split(path.sep).length
|
|
125
|
+
|
|
126
|
+
return currentDepth > minDepth
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return true
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
const unique = Array.from(new Set(finalized))
|
|
107
133
|
|
|
108
|
-
return
|
|
134
|
+
return unique
|
|
109
135
|
} catch (e) {
|
|
110
136
|
if (e.code === 'ENOENT') {
|
|
111
137
|
return []
|
package/src/getFilePath.mjs
CHANGED
|
@@ -17,9 +17,13 @@ export const getFilePath = async (fn, dir, file, depth = true, root = 'deprecate
|
|
|
17
17
|
root = false
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
let minDepth = 0
|
|
21
|
+
let maxDepth = 200
|
|
22
|
+
|
|
20
23
|
if (!is.empty(depth) && is.objectNative(depth)) {
|
|
21
24
|
root = depth.root
|
|
22
|
-
|
|
25
|
+
maxDepth = depth?.depth || depth?.maxDepth
|
|
26
|
+
minDepth = depth?.minDepth
|
|
23
27
|
}
|
|
24
28
|
|
|
25
29
|
if (is.empty(fn)) {
|
|
@@ -52,8 +56,8 @@ export const getFilePath = async (fn, dir, file, depth = true, root = 'deprecate
|
|
|
52
56
|
.split(path.sep)
|
|
53
57
|
.filter(a => a).length
|
|
54
58
|
|
|
55
|
-
if (
|
|
56
|
-
return await fn(filePath, {
|
|
59
|
+
if (maxDepth || currentDepth === 1) {
|
|
60
|
+
return await fn(filePath, { maxDepth, minDepth, root })
|
|
57
61
|
}
|
|
58
62
|
} else if (stat.isFile()) {
|
|
59
63
|
return filePath
|
package/src/getFiles.mjs
CHANGED
|
@@ -14,25 +14,35 @@ export const getFiles = async (dir, depth = true, root = 'deprecated') => {
|
|
|
14
14
|
if (root !== 'deprecated') {
|
|
15
15
|
log.warn('E_DEPRECATED', 'you have used fs.getFiles with a third argument.')
|
|
16
16
|
log.info('Please use the new syntax instead:')
|
|
17
|
-
log.info("fs.getFiles(dir, {
|
|
17
|
+
log.info("fs.getFiles(dir, { maxDepth: 200, root: false, extension: 'md', minDepth: 0 })")
|
|
18
18
|
} else {
|
|
19
19
|
root = false
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
let minDepth = 0
|
|
23
|
+
let maxDepth = 200
|
|
24
|
+
|
|
22
25
|
let extension
|
|
23
26
|
|
|
27
|
+
if (depth === false) {
|
|
28
|
+
maxDepth = 1
|
|
29
|
+
} else if (is.number(depth)) {
|
|
30
|
+
maxDepth = depth
|
|
31
|
+
}
|
|
24
32
|
if (!is.empty(depth) && is.objectNative(depth)) {
|
|
25
33
|
root = root || depth?.root
|
|
26
34
|
extension = depth?.extension
|
|
27
35
|
|
|
28
|
-
|
|
29
|
-
|
|
36
|
+
maxDepth = depth?.depth || depth?.maxDepth
|
|
37
|
+
minDepth = depth.minDepth
|
|
30
38
|
}
|
|
31
39
|
|
|
32
|
-
if (
|
|
33
|
-
|
|
40
|
+
if (maxDepth === false) {
|
|
41
|
+
maxDepth = 1
|
|
34
42
|
}
|
|
35
43
|
|
|
44
|
+
maxDepth = parseInt(maxDepth)
|
|
45
|
+
|
|
36
46
|
if (is.empty(dir)) {
|
|
37
47
|
throw error(`${libName}: dir: first argument can not be empty.`, 'E_ARG_EMPTY')
|
|
38
48
|
}
|
|
@@ -41,31 +51,53 @@ export const getFiles = async (dir, depth = true, root = 'deprecated') => {
|
|
|
41
51
|
throw error(`${libName}: dir: first argument must be a string.`, 'E_ARG_TYPE')
|
|
42
52
|
}
|
|
43
53
|
|
|
44
|
-
if (is.empty(root) && is.number(
|
|
54
|
+
if (is.empty(root) && is.number(maxDepth)) {
|
|
45
55
|
root = dir
|
|
46
56
|
}
|
|
47
57
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
58
|
+
const currentDepth = dir.replace(root, '').split(path.sep).length
|
|
59
|
+
|
|
60
|
+
if (is.number(maxDepth) && currentDepth - 1 > maxDepth) {
|
|
61
|
+
return []
|
|
53
62
|
}
|
|
54
63
|
|
|
55
64
|
try {
|
|
56
65
|
const dirContent = await fs.readdir(dir)
|
|
57
66
|
const files = await Promise.all(
|
|
58
|
-
dirContent.map(file => getFilePath(getFiles, dir, file, {
|
|
67
|
+
dirContent.map(file => getFilePath(getFiles, dir, file, { maxDepth, minDepth, root })),
|
|
59
68
|
)
|
|
60
69
|
|
|
61
70
|
return await Promise.all(
|
|
62
71
|
deep
|
|
63
72
|
.flatten(files)
|
|
64
73
|
.filter(a => a)
|
|
74
|
+
/*
|
|
75
|
+
* if an extension parameter has been passed,
|
|
76
|
+
* remove the file if it does not end with extension
|
|
77
|
+
*/
|
|
65
78
|
.filter(a => !extension || a.endsWith(extension))
|
|
79
|
+
/*
|
|
80
|
+
* filter nonfiles
|
|
81
|
+
*/
|
|
66
82
|
.filter(async f => {
|
|
67
83
|
const stat = await fs.stat(f)
|
|
68
84
|
return stat.isFile()
|
|
85
|
+
})
|
|
86
|
+
/*
|
|
87
|
+
* filter files if depth is smaller than minDepth
|
|
88
|
+
*/
|
|
89
|
+
.filter(file => {
|
|
90
|
+
if (!file) {
|
|
91
|
+
return false
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (is.number(minDepth)) {
|
|
95
|
+
const currentDepth = file.replace(root, '').split(path.sep).length
|
|
96
|
+
|
|
97
|
+
return currentDepth > minDepth
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return true
|
|
69
101
|
}),
|
|
70
102
|
)
|
|
71
103
|
} catch (e) {
|