@magic/fs 0.0.29 → 0.0.30

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 CHANGED
@@ -301,7 +301,13 @@ update dependencies
301
301
  - update dependencies
302
302
  - getFiles and getDirectories now accept maxDepth and minDepth config args
303
303
 
304
- ##### 0.0.30 - unreleased
304
+ ##### 0.0.30
305
+
306
+ - update dependencies
307
+ - remove deprecated getFiles and getDirectories api
308
+ - add minDepth option
309
+
310
+ ##### 0.0.31 - unreleased
305
311
 
306
312
  ...
307
313
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magic/fs",
3
- "version": "0.0.29",
3
+ "version": "0.0.30",
4
4
  "author": "Wizards & Witches",
5
5
  "description": "nodejs fs promises + goodies",
6
6
  "license": "AGPL-3.0",
@@ -37,8 +37,8 @@
37
37
  "@magic-modules/no-spy": "0.0.9",
38
38
  "@magic-modules/pre": "0.0.12",
39
39
  "@magic-themes/docs": "0.0.15",
40
- "@magic/core": "0.0.155",
41
- "@magic/format": "0.0.52",
40
+ "@magic/core": "0.0.156",
41
+ "@magic/format": "0.0.54",
42
42
  "@magic/test": "0.2.18"
43
43
  },
44
44
  "dependencies": {
@@ -11,39 +11,29 @@ import { getFilePath } from './getFilePath.mjs'
11
11
 
12
12
  const libName = '@magic/fs.getDirectories'
13
13
 
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
14
+ export const getDirectories = async (dir, args = {}) => {
15
+ if (is.number(args)) {
16
+ args = {
17
+ maxDepth: args,
18
+ }
19
+ } else if (args === false) {
20
+ args = {
21
+ maxDepth: 1,
22
+ }
21
23
  }
22
24
 
23
- let minDepth = 0
24
- let maxDepth = 200
25
+ let { minDepth, maxDepth = false, depth = false, root, noRoot = false } = args
25
26
 
26
- let noRoot = false
27
- if (depth === false) {
28
- maxDepth = 1
29
- } else if (is.number(depth)) {
27
+ if (is.number(depth) && !is.number(maxDepth)) {
30
28
  maxDepth = depth
31
- } else if (!is.empty(depth) && is.objectNative(depth)) {
32
- root = root || depth?.root
33
- noRoot = depth?.noRoot
34
-
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
- }
44
29
  }
45
30
 
46
- maxDepth = parseInt(maxDepth)
31
+ if (!is.number(maxDepth)) {
32
+ maxDepth = 200_000
33
+ }
34
+ if (!is.number(minDepth)) {
35
+ minDepth = 0
36
+ }
47
37
 
48
38
  if (!is.array(dir) && !is.string(dir)) {
49
39
  throw error(`${libName}: need an array or a string as first argument`, 'E_ARG_TYPE')
@@ -70,8 +60,12 @@ export const getDirectories = async (dir, depth = {}, root = 'deprecated') => {
70
60
  return deep.flatten(...dirs).filter(a => a)
71
61
  }
72
62
 
73
- const currentDepth = dir.replace(root, '').split(path.sep).length
74
- if (is.number(maxDepth) && currentDepth - 1 > maxDepth) {
63
+ const currentDepth = dir
64
+ .replace(root, '')
65
+ .split(path.sep)
66
+ .filter(a => a).length
67
+
68
+ if (currentDepth > maxDepth) {
75
69
  return []
76
70
  }
77
71
 
@@ -110,24 +104,25 @@ export const getDirectories = async (dir, depth = {}, root = 'deprecated') => {
110
104
  }),
111
105
  )
112
106
 
113
- let finalDirs = dirs.filter(a => a)
107
+ let finalDirs = deep.flatten(dirs).filter(a => a)
114
108
  if (!noRoot) {
115
109
  finalDirs = [dir, ...finalDirs]
116
110
  }
117
111
 
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
112
+ const finalized = finalDirs
113
+ .filter(a => a)
114
+ .filter(dir => {
115
+ if (is.number(minDepth)) {
116
+ const currentDepth = dir
117
+ .replace(root, '')
118
+ .split(path.sep)
119
+ .filter(a => a).length
125
120
 
126
- return currentDepth > minDepth
127
- }
121
+ return currentDepth >= minDepth
122
+ }
128
123
 
129
- return true
130
- })
124
+ return false
125
+ })
131
126
 
132
127
  const unique = Array.from(new Set(finalized))
133
128
 
@@ -2,30 +2,12 @@ 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'
6
5
 
7
6
  import { fs } from './fs.mjs'
8
7
 
9
8
  const libName = '@magic/fs.getFilePath'
10
9
 
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
- let minDepth = 0
21
- let maxDepth = 200
22
-
23
- if (!is.empty(depth) && is.objectNative(depth)) {
24
- root = depth.root
25
- maxDepth = depth?.depth || depth?.maxDepth
26
- minDepth = depth?.minDepth
27
- }
28
-
10
+ export const getFilePath = async (fn, dir, file, args = {}) => {
29
11
  if (is.empty(fn)) {
30
12
  throw error(`${libName}: fn: first argument can not be empty`, 'E_ARG_1_EMPTY')
31
13
  }
@@ -51,14 +33,7 @@ export const getFilePath = async (fn, dir, file, depth = true, root = 'deprecate
51
33
 
52
34
  const stat = await fs.stat(filePath)
53
35
  if (stat.isDirectory(filePath)) {
54
- const currentDepth = filePath
55
- .replace(root, '')
56
- .split(path.sep)
57
- .filter(a => a).length
58
-
59
- if (maxDepth || currentDepth === 1) {
60
- return await fn(filePath, { maxDepth, minDepth, root })
61
- }
36
+ return await fn(filePath, args)
62
37
  } else if (stat.isFile()) {
63
38
  return filePath
64
39
  }
package/src/getFiles.mjs CHANGED
@@ -3,46 +3,36 @@ 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'
7
6
 
8
7
  import { getFilePath } from './getFilePath.mjs'
9
8
  import { fs } from './fs.mjs'
10
9
 
11
10
  const libName = '@magic/fs.getFiles'
12
11
 
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, { maxDepth: 200, root: false, extension: 'md', minDepth: 0 })")
18
- } else {
19
- root = false
12
+ export const getFiles = async (dir, args = {}) => {
13
+ if (is.number(args)) {
14
+ args = {
15
+ maxDepth: args,
16
+ }
20
17
  }
21
18
 
22
- let minDepth = 0
23
- let maxDepth = 200
19
+ let { minDepth = 0, maxDepth = false, depth = false, extension = false, ext = false, root } = args
24
20
 
25
- let extension
21
+ if (ext && !extension) {
22
+ extension = ext
23
+ }
26
24
 
27
- if (depth === false) {
28
- maxDepth = 1
29
- } else if (is.number(depth)) {
25
+ if (is.number(depth) && !is.number(maxDepth)) {
30
26
  maxDepth = depth
31
27
  }
32
- if (!is.empty(depth) && is.objectNative(depth)) {
33
- root = root || depth?.root
34
- extension = depth?.extension
35
28
 
36
- maxDepth = depth?.depth || depth?.maxDepth
37
- minDepth = depth.minDepth
29
+ if (!is.number(maxDepth)) {
30
+ maxDepth = 200_000
38
31
  }
39
-
40
- if (maxDepth === false) {
41
- maxDepth = 1
32
+ if (!is.number(minDepth)) {
33
+ minDepth = 0
42
34
  }
43
35
 
44
- maxDepth = parseInt(maxDepth)
45
-
46
36
  if (is.empty(dir)) {
47
37
  throw error(`${libName}: dir: first argument can not be empty.`, 'E_ARG_EMPTY')
48
38
  }
@@ -51,13 +41,16 @@ export const getFiles = async (dir, depth = true, root = 'deprecated') => {
51
41
  throw error(`${libName}: dir: first argument must be a string.`, 'E_ARG_TYPE')
52
42
  }
53
43
 
54
- if (is.empty(root) && is.number(maxDepth)) {
44
+ if (is.empty(root)) {
55
45
  root = dir
56
46
  }
57
47
 
58
- const currentDepth = dir.replace(root, '').split(path.sep).length
48
+ const currentDepth = dir
49
+ .replace(root, '')
50
+ .split(path.sep)
51
+ .filter(a => a).length
59
52
 
60
- if (is.number(maxDepth) && currentDepth - 1 > maxDepth) {
53
+ if (currentDepth > maxDepth) {
61
54
  return []
62
55
  }
63
56
 
@@ -92,9 +85,13 @@ export const getFiles = async (dir, depth = true, root = 'deprecated') => {
92
85
  }
93
86
 
94
87
  if (is.number(minDepth)) {
95
- const currentDepth = file.replace(root, '').split(path.sep).length
88
+ const currentDepth =
89
+ file
90
+ .replace(root, '')
91
+ .split(path.sep)
92
+ .filter(a => a).length - 1
96
93
 
97
- return currentDepth > minDepth
94
+ return currentDepth >= minDepth
98
95
  }
99
96
 
100
97
  return true