@magic/fs 0.0.15 → 0.0.19
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 +31 -14
- package/package.json +8 -8
- package/src/getDirectories.mjs +30 -8
- package/src/getFilePath.mjs +19 -4
- package/src/getFiles.mjs +21 -7
package/README.md
CHANGED
|
@@ -100,17 +100,23 @@ import fs from '@magic/fs'
|
|
|
100
100
|
|
|
101
101
|
const run = async () => {
|
|
102
102
|
// first level directories
|
|
103
|
-
const directories = await fs.getDirectories(process.cwd())
|
|
103
|
+
const directories = await fs.getDirectories(process.cwd(), false)
|
|
104
104
|
console.log(directories)
|
|
105
105
|
|
|
106
|
+
// first level again
|
|
107
|
+
const dirs = await fs.getDirectories(process.cwd(), { depth: false })
|
|
108
|
+
|
|
106
109
|
// recursive run
|
|
107
|
-
const deepDirectories = await fs.getDirectories(process.cwd()
|
|
110
|
+
const deepDirectories = await fs.getDirectories(process.cwd())
|
|
108
111
|
console.log(deepDirectories)
|
|
109
112
|
|
|
110
|
-
// recursive run with specified depth
|
|
111
|
-
|
|
112
|
-
const deepDirectoriesDepth2 = await fs.getDirectories(process.cwd(), 2)
|
|
113
|
+
// recursive run with specified depth
|
|
114
|
+
const deepDirectoriesDepth2 = await fs.getDirectories(process.cwd(), { depth: 2 })
|
|
113
115
|
console.log(deepDirectoriesDepth2)
|
|
116
|
+
|
|
117
|
+
// use /some/dir as root instead of process.cwd()
|
|
118
|
+
const deepDirWithRoot = await fs.getDirectories(process.cwd(), { depth: true, root: '/some/dir' })
|
|
119
|
+
console.log(deepDirWithRoot)
|
|
114
120
|
}
|
|
115
121
|
run()
|
|
116
122
|
```
|
|
@@ -128,13 +134,12 @@ const run = async () => {
|
|
|
128
134
|
console.log(files)
|
|
129
135
|
|
|
130
136
|
// recursive run
|
|
131
|
-
const deepFiles = await fs.getFiles(process.cwd(), true)
|
|
137
|
+
const deepFiles = await fs.getFiles(process.cwd(), { depth: true })
|
|
132
138
|
console.log(deepFiles)
|
|
133
139
|
|
|
134
|
-
// recursive run with specified depth,
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
console.log(deepDirectoriesDepth2)
|
|
140
|
+
// recursive run with specified depth,
|
|
141
|
+
const deepFilesDepth2 = await fs.getFiles(process.cwd(), { depth: 2 })
|
|
142
|
+
console.log(deepFilesDepth2)
|
|
138
143
|
}
|
|
139
144
|
run()
|
|
140
145
|
```
|
|
@@ -179,10 +184,10 @@ bump required node version to 14.2.0
|
|
|
179
184
|
#### 0.0.7
|
|
180
185
|
rmrf: add dryRun option
|
|
181
186
|
|
|
182
|
-
#### 0.0.8
|
|
187
|
+
#### 0.0.8
|
|
183
188
|
update dependencies
|
|
184
189
|
|
|
185
|
-
#### 0.0.9
|
|
190
|
+
#### 0.0.9
|
|
186
191
|
* remove unused imports from getDirectories
|
|
187
192
|
* getDirectories and getFiles now accept a number as second argument.
|
|
188
193
|
|
|
@@ -198,7 +203,7 @@ getFiles(directory, 2) // two levels down
|
|
|
198
203
|
##### 0.0.11
|
|
199
204
|
update dependencies (@magic/mime-types)
|
|
200
205
|
|
|
201
|
-
##### 0.0.12
|
|
206
|
+
##### 0.0.12
|
|
202
207
|
export all functions from native fs
|
|
203
208
|
|
|
204
209
|
##### 0.0.13
|
|
@@ -211,7 +216,19 @@ update dependencies (@magic/mime-types)
|
|
|
211
216
|
##### 0.0.15
|
|
212
217
|
update @magic/types to avoid circular dependency
|
|
213
218
|
|
|
214
|
-
##### 0.0.16
|
|
219
|
+
##### 0.0.16
|
|
220
|
+
add deprecation warning for calls to fs.getDirectories, fs.getFilePath and fs.getFiles that do not use an options object
|
|
221
|
+
|
|
222
|
+
##### 0.0.17
|
|
223
|
+
update dependencies
|
|
224
|
+
|
|
225
|
+
##### 0.0.18
|
|
226
|
+
update mime-types
|
|
227
|
+
|
|
228
|
+
##### 0.0.19
|
|
229
|
+
update dependencies
|
|
230
|
+
|
|
231
|
+
##### 0.0.20 - unreleased
|
|
215
232
|
...
|
|
216
233
|
|
|
217
234
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magic/fs",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.19",
|
|
4
4
|
"author": "Wizards & Witches",
|
|
5
5
|
"description": "nodejs fs promises + goodies",
|
|
6
6
|
"license": "AGPL-3.0",
|
|
@@ -35,16 +35,16 @@
|
|
|
35
35
|
"@magic-modules/git-badges": "0.0.11",
|
|
36
36
|
"@magic-modules/light-switch": "0.0.10",
|
|
37
37
|
"@magic-modules/no-spy": "0.0.6",
|
|
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.1.
|
|
38
|
+
"@magic-modules/pre": "0.0.11",
|
|
39
|
+
"@magic-themes/docs": "0.0.14",
|
|
40
|
+
"@magic/core": "0.0.133",
|
|
41
|
+
"@magic/format": "0.0.33",
|
|
42
|
+
"@magic/test": "0.1.77"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@magic/deep": "0.1.
|
|
45
|
+
"@magic/deep": "0.1.8",
|
|
46
46
|
"@magic/error": "0.0.10",
|
|
47
|
-
"@magic/mime-types": "0.0.
|
|
47
|
+
"@magic/mime-types": "0.0.12",
|
|
48
48
|
"@magic/types": "0.1.16"
|
|
49
49
|
},
|
|
50
50
|
"files": [
|
package/src/getDirectories.mjs
CHANGED
|
@@ -11,9 +11,24 @@ import { getFilePath } from './getFilePath.mjs'
|
|
|
11
11
|
|
|
12
12
|
const libName = '@magic/fs.getDirectories'
|
|
13
13
|
|
|
14
|
-
export const getDirectories = async (dir,
|
|
15
|
-
if (
|
|
16
|
-
|
|
14
|
+
export const getDirectories = async (dir, depth = {}, root = 'deprecated') => {
|
|
15
|
+
if (root !== 'deprecated') {
|
|
16
|
+
log.warn('E_DEPRECATED', 'you have used fs.getDirectories with a third argument.')
|
|
17
|
+
log.info('Please use the new syntax instead:')
|
|
18
|
+
log.info('fs.getDirectories(dir, { root: true })')
|
|
19
|
+
} else {
|
|
20
|
+
root = false
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let noRoot = false
|
|
24
|
+
if (!is.empty(depth) && is.objectNative(depth)) {
|
|
25
|
+
root = root || depth?.root
|
|
26
|
+
noRoot = depth?.noRoot
|
|
27
|
+
depth = depth?.depth
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (depth === false) {
|
|
31
|
+
depth = 1
|
|
17
32
|
}
|
|
18
33
|
|
|
19
34
|
if (!is.array(dir) && !is.string(dir)) {
|
|
@@ -34,14 +49,16 @@ export const getDirectories = async (dir, recurse = true, root = false) => {
|
|
|
34
49
|
|
|
35
50
|
try {
|
|
36
51
|
if (is.array(dir)) {
|
|
37
|
-
const dirs = await Promise.all(
|
|
52
|
+
const dirs = await Promise.all(
|
|
53
|
+
dir.map(async f => await getDirectories(f, { depth, root, noRoot })),
|
|
54
|
+
)
|
|
38
55
|
|
|
39
56
|
return deep.flatten(...dirs).filter(a => a)
|
|
40
57
|
}
|
|
41
58
|
|
|
42
|
-
if (is.number(
|
|
59
|
+
if (is.number(depth)) {
|
|
43
60
|
const currentDepth = dir.replace(root, '').split(path.sep).length
|
|
44
|
-
if (currentDepth - 1 >
|
|
61
|
+
if (currentDepth - 1 > depth) {
|
|
45
62
|
return []
|
|
46
63
|
}
|
|
47
64
|
}
|
|
@@ -54,7 +71,7 @@ export const getDirectories = async (dir, recurse = true, root = false) => {
|
|
|
54
71
|
throw error(`${libName}: path was not a string: ${file}`, 'E_ARG_TYPE')
|
|
55
72
|
}
|
|
56
73
|
|
|
57
|
-
let filePath = await getFilePath(getDirectories, dir, file,
|
|
74
|
+
let filePath = await getFilePath(getDirectories, dir, file, { depth, root })
|
|
58
75
|
|
|
59
76
|
if (filePath) {
|
|
60
77
|
if (!is.array(filePath)) {
|
|
@@ -81,7 +98,12 @@ export const getDirectories = async (dir, recurse = true, root = false) => {
|
|
|
81
98
|
}),
|
|
82
99
|
)
|
|
83
100
|
|
|
84
|
-
|
|
101
|
+
let finalDirs = dirs.filter(a => a)
|
|
102
|
+
if (!noRoot) {
|
|
103
|
+
finalDirs = [dir, ...finalDirs]
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const finalized = deep.flatten(finalDirs).filter(a => a)
|
|
85
107
|
|
|
86
108
|
return Array.from(new Set(finalized))
|
|
87
109
|
} catch (e) {
|
package/src/getFilePath.mjs
CHANGED
|
@@ -2,12 +2,26 @@ import path from 'path'
|
|
|
2
2
|
|
|
3
3
|
import is from '@magic/types'
|
|
4
4
|
import error from '@magic/error'
|
|
5
|
+
import log from '@magic/log'
|
|
5
6
|
|
|
6
7
|
import { fs } from './fs.mjs'
|
|
7
8
|
|
|
8
9
|
const libName = '@magic/fs.getFilePath'
|
|
9
10
|
|
|
10
|
-
export const getFilePath = async (fn, dir, file,
|
|
11
|
+
export const getFilePath = async (fn, dir, file, depth = true, root = 'deprecated') => {
|
|
12
|
+
if (root !== 'deprecated') {
|
|
13
|
+
log.warn('E_DEPRECATED', 'you have used fs.getFilePath with a fifth argument.')
|
|
14
|
+
log.info('Please use the new syntax instead:')
|
|
15
|
+
log.info("fs.getFilePath(fn, dir, file, { depth: true || 22, root: false || '/some/dir/' })")
|
|
16
|
+
} else {
|
|
17
|
+
root = false
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (!is.empty(depth) && is.objectNative(depth)) {
|
|
21
|
+
root = depth.root
|
|
22
|
+
depth = depth.depth
|
|
23
|
+
}
|
|
24
|
+
|
|
11
25
|
if (is.empty(fn)) {
|
|
12
26
|
throw error(`${libName}: fn: first argument can not be empty`, 'E_ARG_1_EMPTY')
|
|
13
27
|
}
|
|
@@ -33,12 +47,13 @@ export const getFilePath = async (fn, dir, file, recurse = true, root = false) =
|
|
|
33
47
|
|
|
34
48
|
const stat = await fs.stat(filePath)
|
|
35
49
|
if (stat.isDirectory(filePath)) {
|
|
36
|
-
const
|
|
50
|
+
const currentDepth = filePath
|
|
37
51
|
.replace(root, '')
|
|
38
52
|
.split(path.sep)
|
|
39
53
|
.filter(a => a).length
|
|
40
|
-
|
|
41
|
-
|
|
54
|
+
|
|
55
|
+
if (depth || currentDepth === 1) {
|
|
56
|
+
return await fn(filePath, { depth, root })
|
|
42
57
|
}
|
|
43
58
|
} else if (stat.isFile()) {
|
|
44
59
|
return filePath
|
package/src/getFiles.mjs
CHANGED
|
@@ -3,15 +3,29 @@ import path from 'path'
|
|
|
3
3
|
import deep from '@magic/deep'
|
|
4
4
|
import is from '@magic/types'
|
|
5
5
|
import error from '@magic/error'
|
|
6
|
+
import log from '@magic/log'
|
|
6
7
|
|
|
7
8
|
import { getFilePath } from './getFilePath.mjs'
|
|
8
9
|
import { fs } from './fs.mjs'
|
|
9
10
|
|
|
10
11
|
const libName = '@magic/fs.getFiles'
|
|
11
12
|
|
|
12
|
-
export const getFiles = async (dir,
|
|
13
|
-
if (
|
|
14
|
-
|
|
13
|
+
export const getFiles = async (dir, depth = true, root = 'deprecated') => {
|
|
14
|
+
if (root !== 'deprecated') {
|
|
15
|
+
log.warn('E_DEPRECATED', 'you have used fs.getFiles with a third argument.')
|
|
16
|
+
log.info('Please use the new syntax instead:')
|
|
17
|
+
log.info('fs.getFiles(dir, { depth: true, root: false })')
|
|
18
|
+
} else {
|
|
19
|
+
root = false
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (!is.empty(depth) && is.objectNative(depth)) {
|
|
23
|
+
root = root || depth?.root
|
|
24
|
+
depth = depth?.depth
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (depth === false) {
|
|
28
|
+
depth = 1
|
|
15
29
|
}
|
|
16
30
|
|
|
17
31
|
if (is.empty(dir)) {
|
|
@@ -22,13 +36,13 @@ export const getFiles = async (dir, recurse = true, root = false) => {
|
|
|
22
36
|
throw error(`${libName}: dir: first argument must be a string.`, 'E_ARG_TYPE')
|
|
23
37
|
}
|
|
24
38
|
|
|
25
|
-
if (is.empty(root) && is.number(
|
|
39
|
+
if (is.empty(root) && is.number(depth)) {
|
|
26
40
|
root = dir
|
|
27
41
|
}
|
|
28
42
|
|
|
29
|
-
if (is.number(
|
|
43
|
+
if (is.number(depth)) {
|
|
30
44
|
const currentDepth = dir.replace(root, '').split(path.sep).length
|
|
31
|
-
if (currentDepth - 1 >
|
|
45
|
+
if (currentDepth - 1 > depth) {
|
|
32
46
|
return []
|
|
33
47
|
}
|
|
34
48
|
}
|
|
@@ -36,7 +50,7 @@ export const getFiles = async (dir, recurse = true, root = false) => {
|
|
|
36
50
|
try {
|
|
37
51
|
const dirContent = await fs.readdir(dir)
|
|
38
52
|
const files = await Promise.all(
|
|
39
|
-
dirContent.map(file => getFilePath(getFiles, dir, file,
|
|
53
|
+
dirContent.map(file => getFilePath(getFiles, dir, file, { depth, root })),
|
|
40
54
|
)
|
|
41
55
|
|
|
42
56
|
return await Promise.all(
|