@magic/fs 0.0.28 → 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 +6 -1
- package/package.json +8 -8
- package/src/getDirectories.mjs +40 -14
- package/src/getFilePath.mjs +7 -3
- package/src/getFiles.mjs +44 -12
package/README.md
CHANGED
|
@@ -296,7 +296,12 @@ update dependencies
|
|
|
296
296
|
|
|
297
297
|
update dependencies
|
|
298
298
|
|
|
299
|
-
##### 0.0.29
|
|
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
|
|
300
305
|
|
|
301
306
|
...
|
|
302
307
|
|
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,13 +33,13 @@
|
|
|
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
45
|
"@magic/deep": "0.1.16",
|
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) {
|