@fullstackunicorn/filesystem 1.0.8 → 1.0.10

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.
Files changed (3) hide show
  1. package/modules/fsa.js +105 -79
  2. package/modules/fss.js +100 -75
  3. package/package.json +1 -1
package/modules/fsa.js CHANGED
@@ -1,83 +1,109 @@
1
1
  import { fsAsync, constants, path } from '#@/index.dep.js'
2
2
 
3
- export const fsa = {
4
- chmod: fsAsync.chmod,
5
- exists: async (path) => {
6
- try { await fsAsync.access(path, constants.F_OK); return true }
7
- catch { return false }
8
- },
9
- readFile: async (path) => await fsa.exists(path) ? await fsAsync.readFile(path) : null,
10
- readText: async (path) => await fsa.exists(path) ? await fsAsync.readFile(path, 'utf8') : null,
11
- readJSON: async (path) => {
12
- try { return await fsa.exists(path) ? JSON.parse(await fsAsync.readFile(path, 'utf8')) : {} }
13
- catch { return {} }
14
- },
15
- writeJSON: async (path, json) => await fsAsync.writeFile(path, JSON.stringify(json, null, 4)),
16
- readDir: async (path, opts) => await fsa.exists(path) ? await fsAsync.readdir(path, opts) : [],
17
- readDirVisible: async (path) => (await fsa.readDir(path, { withFileTypes: true }))
18
- .filter(e => e.isDirectory() && !e.name.startsWith('.')).map(e => e.name),
19
- isDir: async (path) => {
20
- try { return (await fsAsync.stat(path)).isDirectory() }
21
- catch { return false }
22
- },
23
- ensureDir: async (path) => { if (!await fsa.exists(path)) await fsAsync.mkdir(path, { recursive: true }) },
24
- ensureFileDir: async (filePath) => {
25
- const dirPath = path.dirname(filePath)
26
- if (!await fsa.exists(dirPath)) await fsAsync.mkdir(dirPath, { recursive: true })
27
- },
28
- writeText: async (path, text) => await fsAsync.writeFile(path, text),
29
- listFiles: async (path, ext = null) => (await fsa.readDir(path)).filter(f => !ext || f.endsWith(ext)),
30
- safeUnlink: async (path) => { if (await fsa.exists(path)) await fsAsync.unlink(path) },
31
- safeRemove: async (path) => { if (await fsa.exists(path)) await fsAsync.rm(path, { recursive: true, force: true }) },
32
- append: async (path, buffer) => await fsAsync.appendFile(path, buffer),
33
- copyFile: fsAsync.copyFile,
34
- remove: fsAsync.rm,
35
- writeDir: fsAsync.mkdir,
36
- unlink: fsAsync.unlink,
37
- stat: fsAsync.stat,
38
- writeFile: fsAsync.writeFile,
39
- writeBuffer: async (filePath, file) => {
40
- let buffer
41
- if (Buffer.isBuffer(file?.buffer)) buffer = file?.buffer
42
- else if (file?.buffer instanceof ArrayBuffer) buffer = Buffer.from(file.buffer)
43
- else throw new Error('buffer-error')
44
- await fsAsync.writeFile(filePath, buffer)
45
- },
46
- isJunk: name => {
47
- if (!name) return true
48
- const lower = name.toLowerCase()
49
- if (lower.endsWith('~') || lower.endsWith('.tmp') || lower.endsWith('.temp')) return true
50
- return [
51
- '.ds_store', '._', '.spotlight-v100', '.trashes', '.fseventsd', '.temporaryitems', '.apdisk', '.volumeicon.icns',
52
- 'thumbs.db', 'ehthumbs.db', 'desktop.ini', '$recycle.bin', 'system volume information',
53
- 'lost+found', '__macosx', '.swp', '.swo', '.#', '.cache'
54
- ].some(bad => lower === bad || lower.startsWith(bad))
55
- },
56
- copyFile: fsAsync.copyFile,
57
- copyDir: async (src, dest) => {
58
- await fsa.ensureDir(dest)
59
- const entries = await fsa.readDir(src)
3
+ const fsa = {}
4
+
5
+ fsa.chmod = fsAsync.chmod
6
+
7
+ fsa.exists = async (path) => {
8
+ try { await fsAsync.access(path, constants.F_OK); return true }
9
+ catch { return false }
10
+ }
11
+
12
+ fsa.readFile = async (path) => await fsa.exists(path) ? await fsAsync.readFile(path) : null
13
+
14
+ fsa.readText = async (path) => await fsa.exists(path) ? await fsAsync.readFile(path, 'utf8') : null
15
+
16
+ fsa.readJSON = async (path) => {
17
+ try { return await fsa.exists(path) ? JSON.parse(await fsAsync.readFile(path, 'utf8')) : {} }
18
+ catch { return {} }
19
+ }
20
+
21
+ fsa.writeJSON = async (path, json) => await fsAsync.writeFile(path, JSON.stringify(json, null, 4))
22
+
23
+ fsa.readDir = async (path, opts) => await fsa.exists(path) ? await fsAsync.readdir(path, opts) : []
24
+
25
+ fsa.readDirVisible = async (path) => (await fsa.readDir(path, { withFileTypes: true }))
26
+ .filter(e => e.isDirectory() && !e.name.startsWith('.')).map(e => e.name)
27
+
28
+ fsa.isDir = async (path) => {
29
+ try { return (await fsAsync.stat(path)).isDirectory() }
30
+ catch { return false }
31
+ }
32
+
33
+ fsa.ensureDir = async (path) => { if (!await fsa.exists(path)) await fsAsync.mkdir(path, { recursive: true }) }
34
+
35
+ fsa.ensureFileDir = async (filePath) => {
36
+ const dirPath = path.dirname(filePath)
37
+ if (!await fsa.exists(dirPath)) await fsAsync.mkdir(dirPath, { recursive: true })
38
+ }
39
+
40
+ fsa.writeText = async (path, text) => await fsAsync.writeFile(path, text)
41
+
42
+ fsa.listFiles = async (path, ext = null) => (await fsa.readDir(path)).filter(f => !ext || f.endsWith(ext))
43
+
44
+ fsa.safeUnlink = async (path) => { if (await fsa.exists(path)) await fsAsync.unlink(path) }
45
+
46
+ fsa.safeRemove = async (path) => { if (await fsa.exists(path)) await fsAsync.rm(path, { recursive: true, force: true }) }
47
+
48
+ fsa.append = async (path, buffer) => await fsAsync.appendFile(path, buffer)
49
+
50
+ fsa.copyFile = fsAsync.copyFile
51
+
52
+ fsa.remove = fsAsync.rm
53
+
54
+ fsa.writeDir = fsAsync.mkdir
55
+
56
+ fsa.unlink = fsAsync.unlink
57
+
58
+ fsa.stat = fsAsync.stat
59
+
60
+ fsa.writeFile = fsAsync.writeFile
61
+
62
+ fsa.writeBuffer = async (filePath, file) => {
63
+ let buffer
64
+ if (Buffer.isBuffer(file?.buffer)) buffer = file?.buffer
65
+ else if (file?.buffer instanceof ArrayBuffer) buffer = Buffer.from(file.buffer)
66
+ else throw new Error('buffer-error')
67
+ await fsAsync.writeFile(filePath, buffer)
68
+ }
69
+
70
+ fsa.isJunk = name => {
71
+ if (!name) return true
72
+ const lower = name.toLowerCase()
73
+ if (lower.endsWith('~') || lower.endsWith('.tmp') || lower.endsWith('.temp')) return true
74
+ return [
75
+ '.ds_store', '._', '.spotlight-v100', '.trashes', '.fseventsd', '.temporaryitems', '.apdisk', '.volumeicon.icns',
76
+ 'thumbs.db', 'ehthumbs.db', 'desktop.ini', '$recycle.bin', 'system volume information',
77
+ 'lost+found', '__macosx', '.swp', '.swo', '.#', '.cache'
78
+ ].some(bad => lower === bad || lower.startsWith(bad))
79
+ }
80
+
81
+ fsa.copyDir = async (src, dest) => {
82
+ await fsa.ensureDir(dest)
83
+ const entries = await fsa.readDir(src)
84
+ for (const entry of entries) {
85
+ const srcPath = `${src}/${entry}`
86
+ const destPath = `${dest}/${entry}`
87
+ const stat = await fsa.stat(srcPath)
88
+ if (stat.isDirectory()) await fsa.copyDir(srcPath, destPath)
89
+ else await fsAsync.copyFile(srcPath, destPath)
90
+ }
91
+ }
92
+
93
+ fsa.getDirPaths = async (basePath) => {
94
+ const files = []
95
+ const scan = async dir => {
96
+ const entries = await fsa.readDir(dir)
60
97
  for (const entry of entries) {
61
- const srcPath = `${src}/${entry}`
62
- const destPath = `${dest}/${entry}`
63
- const stat = await fsa.stat(srcPath)
64
- if (stat.isDirectory()) await fsa.copyDir(srcPath, destPath)
65
- else await fsAsync.copyFile(srcPath, destPath)
66
- }
67
- },
68
- getDirPaths: async (basePath) => {
69
- const files = []
70
- const scan = async dir => {
71
- const entries = await fsa.readDir(dir)
72
- for (const entry of entries) {
73
- if (fsa.isJunk(entry)) continue
74
- const fullPath = path.join(dir, entry)
75
- const stat = await fsa.stat(fullPath)
76
- if (stat.isDirectory()) await scan(fullPath)
77
- else files.push(path.relative(basePath, fullPath))
78
- }
98
+ if (fsa.isJunk(entry)) continue
99
+ const fullPath = path.join(dir, entry)
100
+ const stat = await fsa.stat(fullPath)
101
+ if (stat.isDirectory()) await scan(fullPath)
102
+ else files.push(path.relative(basePath, fullPath))
79
103
  }
80
- await scan(basePath)
81
- return files
82
- },
83
- }
104
+ }
105
+ await scan(basePath)
106
+ return files
107
+ }
108
+
109
+ export { fsa }
package/modules/fss.js CHANGED
@@ -1,80 +1,105 @@
1
1
  import { fsSync, path } from '#@/index.dep.js'
2
2
 
3
- export const fss = {
4
- exists: (...args) => fsSync.existsSync(...args),
5
- readFile: (...args) => fss.exists(...args) ? fsSync.readFileSync(...args) : null,
6
- readText: (path) => fss.exists(path) ? fsSync.readFileSync(path, 'utf8') : null,
7
- readJSON: (path) => {
8
- try { return fss.exists(path) ? JSON.parse(fsSync.readFileSync(path, 'utf8')) : {} }
9
- catch { return {} }
10
- },
11
- writeJSON: (path, json) => fsSync.writeFileSync(path, JSON.stringify(json, null, 4)),
12
- readDir: (...args) => fss.exists(...args) ? fsSync.readdirSync(...args) : [],
13
- readDirVisible: (path) => (
14
- fss.readDir(path, { withFileTypes: true }) || []
15
- ).filter(e => e.isDirectory() && !e.name.startsWith('.')).map(e => e.name),
16
- isDir: (path) => {
17
- try { return fsSync.statSync(path).isDirectory() }
18
- catch { return false }
19
- },
20
- ensureDir: (path) => { if (!fss.exists(path)) fsSync.mkdirSync(path, { recursive: true }) },
21
- ensureFileDir: (filePath) => {
22
- const dirPath = path.dirname(filePath)
23
- if (!fss.exists(dirPath)) fsSync.mkdirSync(dirPath, { recursive: true })
24
- },
25
- writeText: (path, text) => fsSync.writeFileSync(path, text),
26
- listFiles: (path, ext = null) => fss.readDir(path).filter(f => !ext || f.endsWith(ext)),
27
- safeUnlink: (path) => { if (fss.exists(path)) fsSync.unlinkSync(path) },
28
- safeRemove: (path) => { if (fss.exists(path)) fsSync.rmSync(path, { recursive: true, force: true }) },
29
- copyFile: fsSync.copyFileSync,
30
- remove: fsSync.rmSync,
31
- writeDir: fsSync.mkdirSync,
32
- unlink: fsSync.unlinkSync,
33
- stat: fsSync.statSync,
34
- writeFile: fsSync.writeFileSync,
35
- writeStream: (...args) => fsSync.createWriteStream(...args),
36
- writeBuffer: (filePath, file) => {
37
- let buffer
38
- if (Buffer.isBuffer(file?.buffer)) buffer = file?.buffer
39
- else if (file?.buffer instanceof ArrayBuffer) buffer = Buffer.from(file.buffer)
40
- else throw new Error('buffer-error')
41
- fsSync.writeFileSync(filePath, buffer)
42
- },
43
- isJunk: name => {
44
- if (!name) return true
45
- const lower = name.toLowerCase()
46
- if (lower.endsWith('~') || lower.endsWith('.tmp') || lower.endsWith('.temp')) return true
47
- return [
48
- '.ds_store', '._', '.spotlight-v100', '.trashes', '.fseventsd', '.temporaryitems', '.apdisk', '.volumeicon.icns',
49
- 'thumbs.db', 'ehthumbs.db', 'desktop.ini', '$recycle.bin', 'system volume information',
50
- 'lost+found', '__macosx', '.swp', '.swo', '.#', '.cache'
51
- ].some(bad => lower === bad || lower.startsWith(bad))
52
- },
53
- copyFile: fsSync.copyFileSync,
54
- copyDir: (src, dest) => {
55
- fss.ensureDir(dest)
56
- const entries = fss.readDir(src)
3
+ const fss = {}
4
+
5
+ fss.exists = (...args) => fsSync.existsSync(...args)
6
+
7
+ fss.readFile = (...args) => fss.exists(...args) ? fsSync.readFileSync(...args) : null
8
+
9
+ fss.readText = (path) => fss.exists(path) ? fsSync.readFileSync(path, 'utf8') : null
10
+
11
+ fss.readJSON = (path) => {
12
+ try { return fss.exists(path) ? JSON.parse(fsSync.readFileSync(path, 'utf8')) : {} }
13
+ catch { return {} }
14
+ }
15
+
16
+ fss.writeJSON = (path, json) => fsSync.writeFileSync(path, JSON.stringify(json, null, 4))
17
+
18
+ fss.readDir = (...args) => fss.exists(...args) ? fsSync.readdirSync(...args) : []
19
+
20
+ fss.readDirVisible = (path) => (
21
+ fss.readDir(path, { withFileTypes: true }) || []
22
+ ).filter(e => e.isDirectory() && !e.name.startsWith('.')).map(e => e.name)
23
+
24
+ fss.isDir = (path) => {
25
+ try { return fsSync.statSync(path).isDirectory() }
26
+ catch { return false }
27
+ }
28
+
29
+ fss.ensureDir = (path) => { if (!fss.exists(path)) fsSync.mkdirSync(path, { recursive: true }) }
30
+
31
+ fss.ensureFileDir = (filePath) => {
32
+ const dirPath = path.dirname(filePath)
33
+ if (!fss.exists(dirPath)) fsSync.mkdirSync(dirPath, { recursive: true })
34
+ }
35
+
36
+ fss.writeText = (path, text) => fsSync.writeFileSync(path, text)
37
+
38
+ fss.listFiles = (path, ext = null) => fss.readDir(path).filter(f => !ext || f.endsWith(ext))
39
+
40
+ fss.safeUnlink = (path) => { if (fss.exists(path)) fsSync.unlinkSync(path) }
41
+
42
+ fss.safeRemove = (path) => { if (fss.exists(path)) fsSync.rmSync(path, { recursive: true, force: true }) }
43
+
44
+ fss.copyFile = fsSync.copyFileSync
45
+
46
+ fss.remove = fsSync.rmSync
47
+
48
+ fss.writeDir = fsSync.mkdirSync
49
+
50
+ fss.unlink = fsSync.unlinkSync
51
+
52
+ fss.stat = fsSync.statSync
53
+
54
+ fss.writeFile = fsSync.writeFileSync
55
+
56
+ fss.writeStream = (...args) => fsSync.createWriteStream(...args)
57
+
58
+ fss.writeBuffer = (filePath, file) => {
59
+ let buffer
60
+ if (Buffer.isBuffer(file?.buffer)) buffer = file?.buffer
61
+ else if (file?.buffer instanceof ArrayBuffer) buffer = Buffer.from(file.buffer)
62
+ else throw new Error('buffer-error')
63
+ fsSync.writeFileSync(filePath, buffer)
64
+ }
65
+
66
+ fss.isJunk = name => {
67
+ if (!name) return true
68
+ const lower = name.toLowerCase()
69
+ if (lower.endsWith('~') || lower.endsWith('.tmp') || lower.endsWith('.temp')) return true
70
+ return [
71
+ '.ds_store', '._', '.spotlight-v100', '.trashes', '.fseventsd', '.temporaryitems', '.apdisk', '.volumeicon.icns',
72
+ 'thumbs.db', 'ehthumbs.db', 'desktop.ini', '$recycle.bin', 'system volume information',
73
+ 'lost+found', '__macosx', '.swp', '.swo', '.#', '.cache'
74
+ ].some(bad => lower === bad || lower.startsWith(bad))
75
+ }
76
+
77
+ fss.copyDir = (src, dest) => {
78
+ fss.ensureDir(dest)
79
+ const entries = fss.readDir(src)
80
+ for (const entry of entries) {
81
+ const srcPath = `${src}/${entry}`
82
+ const destPath = `${dest}/${entry}`
83
+ const stat = fss.stat(srcPath)
84
+ if (stat.isDirectory()) fss.copyDir(srcPath, destPath)
85
+ else fsSync.copyFileSync(srcPath, destPath)
86
+ }
87
+ }
88
+
89
+ fss.getDirPaths = (basePath) => {
90
+ const files = []
91
+ const scan = dir => {
92
+ const entries = fss.readDir(dir)
57
93
  for (const entry of entries) {
58
- const srcPath = `${src}/${entry}`
59
- const destPath = `${dest}/${entry}`
60
- const stat = fss.stat(srcPath)
61
- if (stat.isDirectory()) fss.copyDir(srcPath, destPath)
62
- else fsSync.copyFileSync(srcPath, destPath)
63
- }
64
- },
65
- getDirPaths: (basePath) => {
66
- const files = []
67
- const scan = dir => {
68
- const entries = fss.readDir(dir)
69
- for (const entry of entries) {
70
- if (fss.isJunk(entry)) continue
71
- const fullPath = path.join(dir, entry)
72
- const stat = fss.stat(fullPath)
73
- if (stat.isDirectory()) scan(fullPath)
74
- else files.push(path.relative(basePath, fullPath))
75
- }
94
+ if (fss.isJunk(entry)) continue
95
+ const fullPath = path.join(dir, entry)
96
+ const stat = fss.stat(fullPath)
97
+ if (stat.isDirectory()) scan(fullPath)
98
+ else files.push(path.relative(basePath, fullPath))
76
99
  }
77
- scan(basePath)
78
- return files
79
100
  }
80
- }
101
+ scan(basePath)
102
+ return files
103
+ }
104
+
105
+ export { fss }
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.8",
2
+ "version": "1.0.10",
3
3
  "name": "@fullstackunicorn/filesystem",
4
4
  "author": "lucanigido (https://fullstackunicorn.dev/author/lucanigido)",
5
5
  "description": "A simple sync/async file system wrapper for Node.js",