@magic/fs 0.0.28 → 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
@@ -296,7 +296,18 @@ update dependencies
296
296
 
297
297
  update dependencies
298
298
 
299
- ##### 0.0.29 - unreleased
299
+ ##### 0.0.29
300
+
301
+ - update dependencies
302
+ - getFiles and getDirectories now accept maxDepth and minDepth config args
303
+
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
300
311
 
301
312
  ...
302
313
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magic/fs",
3
- "version": "0.0.28",
3
+ "version": "0.0.30",
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.11",
37
- "@magic-modules/no-spy": "0.0.8",
38
- "@magic-modules/pre": "0.0.11",
39
- "@magic-themes/docs": "0.0.14",
40
- "@magic/core": "0.0.149",
41
- "@magic/format": "0.0.50",
42
- "@magic/test": "0.2.15"
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.156",
41
+ "@magic/format": "0.0.54",
42
+ "@magic/test": "0.2.18"
43
43
  },
44
44
  "dependencies": {
45
45
  "@magic/deep": "0.1.16",
@@ -11,24 +11,28 @@ 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 noRoot = false
24
- if (!is.empty(depth) && is.objectNative(depth)) {
25
- root = root || depth?.root
26
- noRoot = depth?.noRoot
27
- depth = depth?.depth
25
+ let { minDepth, maxDepth = false, depth = false, root, noRoot = false } = args
26
+
27
+ if (is.number(depth) && !is.number(maxDepth)) {
28
+ maxDepth = depth
28
29
  }
29
30
 
30
- if (depth === false) {
31
- depth = 1
31
+ if (!is.number(maxDepth)) {
32
+ maxDepth = 200_000
33
+ }
34
+ if (!is.number(minDepth)) {
35
+ minDepth = 0
32
36
  }
33
37
 
34
38
  if (!is.array(dir) && !is.string(dir)) {
@@ -50,17 +54,19 @@ export const getDirectories = async (dir, depth = {}, root = 'deprecated') => {
50
54
  try {
51
55
  if (is.array(dir)) {
52
56
  const dirs = await Promise.all(
53
- dir.map(async f => await getDirectories(f, { depth, root, noRoot })),
57
+ dir.map(async f => await getDirectories(f, { maxDepth, minDepth, root, noRoot })),
54
58
  )
55
59
 
56
60
  return deep.flatten(...dirs).filter(a => a)
57
61
  }
58
62
 
59
- if (is.number(depth)) {
60
- const currentDepth = dir.replace(root, '').split(path.sep).length
61
- if (currentDepth - 1 > depth) {
62
- return []
63
- }
63
+ const currentDepth = dir
64
+ .replace(root, '')
65
+ .split(path.sep)
66
+ .filter(a => a).length
67
+
68
+ if (currentDepth > maxDepth) {
69
+ return []
64
70
  }
65
71
 
66
72
  const dirContent = await fs.readdir(dir)
@@ -71,7 +77,7 @@ export const getDirectories = async (dir, depth = {}, root = 'deprecated') => {
71
77
  throw error(`${libName}: path was not a string: ${file}`, 'E_ARG_TYPE')
72
78
  }
73
79
 
74
- let filePath = await getFilePath(getDirectories, dir, file, { depth, root })
80
+ let filePath = await getFilePath(getDirectories, dir, file, { maxDepth, minDepth, root })
75
81
 
76
82
  if (filePath) {
77
83
  if (!is.array(filePath)) {
@@ -98,14 +104,29 @@ export const getDirectories = async (dir, depth = {}, root = 'deprecated') => {
98
104
  }),
99
105
  )
100
106
 
101
- let finalDirs = dirs.filter(a => a)
107
+ let finalDirs = deep.flatten(dirs).filter(a => a)
102
108
  if (!noRoot) {
103
109
  finalDirs = [dir, ...finalDirs]
104
110
  }
105
111
 
106
- const finalized = deep.flatten(finalDirs).filter(a => a)
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
120
+
121
+ return currentDepth >= minDepth
122
+ }
123
+
124
+ return false
125
+ })
126
+
127
+ const unique = Array.from(new Set(finalized))
107
128
 
108
- return Array.from(new Set(finalized))
129
+ return unique
109
130
  } catch (e) {
110
131
  if (e.code === 'ENOENT') {
111
132
  return []
@@ -2,26 +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
- if (!is.empty(depth) && is.objectNative(depth)) {
21
- root = depth.root
22
- depth = depth.depth
23
- }
24
-
10
+ export const getFilePath = async (fn, dir, file, args = {}) => {
25
11
  if (is.empty(fn)) {
26
12
  throw error(`${libName}: fn: first argument can not be empty`, 'E_ARG_1_EMPTY')
27
13
  }
@@ -47,14 +33,7 @@ export const getFilePath = async (fn, dir, file, depth = true, root = 'deprecate
47
33
 
48
34
  const stat = await fs.stat(filePath)
49
35
  if (stat.isDirectory(filePath)) {
50
- const currentDepth = filePath
51
- .replace(root, '')
52
- .split(path.sep)
53
- .filter(a => a).length
54
-
55
- if (depth || currentDepth === 1) {
56
- return await fn(filePath, { depth, root })
57
- }
36
+ return await fn(filePath, args)
58
37
  } else if (stat.isFile()) {
59
38
  return filePath
60
39
  }
package/src/getFiles.mjs CHANGED
@@ -3,34 +3,34 @@ 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, { depth: true, root: false, extension: 'md' })")
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 extension
19
+ let { minDepth = 0, maxDepth = false, depth = false, extension = false, ext = false, root } = args
23
20
 
24
- if (!is.empty(depth) && is.objectNative(depth)) {
25
- root = root || depth?.root
26
- extension = depth?.extension
21
+ if (ext && !extension) {
22
+ extension = ext
23
+ }
27
24
 
28
- /* make sure this is the last action in this if */
29
- depth = depth?.depth
25
+ if (is.number(depth) && !is.number(maxDepth)) {
26
+ maxDepth = depth
30
27
  }
31
28
 
32
- if (depth === false) {
33
- depth = 1
29
+ if (!is.number(maxDepth)) {
30
+ maxDepth = 200_000
31
+ }
32
+ if (!is.number(minDepth)) {
33
+ minDepth = 0
34
34
  }
35
35
 
36
36
  if (is.empty(dir)) {
@@ -41,31 +41,60 @@ export const getFiles = async (dir, depth = true, root = 'deprecated') => {
41
41
  throw error(`${libName}: dir: first argument must be a string.`, 'E_ARG_TYPE')
42
42
  }
43
43
 
44
- if (is.empty(root) && is.number(depth)) {
44
+ if (is.empty(root)) {
45
45
  root = dir
46
46
  }
47
47
 
48
- if (is.number(depth)) {
49
- const currentDepth = dir.replace(root, '').split(path.sep).length
50
- if (currentDepth - 1 > depth) {
51
- return []
52
- }
48
+ const currentDepth = dir
49
+ .replace(root, '')
50
+ .split(path.sep)
51
+ .filter(a => a).length
52
+
53
+ if (currentDepth > maxDepth) {
54
+ return []
53
55
  }
54
56
 
55
57
  try {
56
58
  const dirContent = await fs.readdir(dir)
57
59
  const files = await Promise.all(
58
- dirContent.map(file => getFilePath(getFiles, dir, file, { depth, root })),
60
+ dirContent.map(file => getFilePath(getFiles, dir, file, { maxDepth, minDepth, root })),
59
61
  )
60
62
 
61
63
  return await Promise.all(
62
64
  deep
63
65
  .flatten(files)
64
66
  .filter(a => a)
67
+ /*
68
+ * if an extension parameter has been passed,
69
+ * remove the file if it does not end with extension
70
+ */
65
71
  .filter(a => !extension || a.endsWith(extension))
72
+ /*
73
+ * filter nonfiles
74
+ */
66
75
  .filter(async f => {
67
76
  const stat = await fs.stat(f)
68
77
  return stat.isFile()
78
+ })
79
+ /*
80
+ * filter files if depth is smaller than minDepth
81
+ */
82
+ .filter(file => {
83
+ if (!file) {
84
+ return false
85
+ }
86
+
87
+ if (is.number(minDepth)) {
88
+ const currentDepth =
89
+ file
90
+ .replace(root, '')
91
+ .split(path.sep)
92
+ .filter(a => a).length - 1
93
+
94
+ return currentDepth >= minDepth
95
+ }
96
+
97
+ return true
69
98
  }),
70
99
  )
71
100
  } catch (e) {