@fullstackunicorn/filesystem 1.0.9 → 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.
- package/modules/fsa.js +104 -78
- package/modules/fss.js +100 -75
- 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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
62
|
-
const
|
|
63
|
-
const stat = await fsa.stat(
|
|
64
|
-
if (stat.isDirectory()) await
|
|
65
|
-
else
|
|
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
104
|
}
|
|
83
|
-
|
|
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
59
|
-
const
|
|
60
|
-
const stat = fss.stat(
|
|
61
|
-
if (stat.isDirectory())
|
|
62
|
-
else
|
|
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