@fullstackunicorn/filesystem 1.0.0
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/index.js +65 -0
- package/package.json +21 -0
package/index.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as fsSync from 'fs'
|
|
2
|
+
import * as fsAsync from 'fs/promises'
|
|
3
|
+
import { constants } from 'fs'
|
|
4
|
+
|
|
5
|
+
export const fss = {
|
|
6
|
+
exists: (...args) => fsSync.existsSync(...args),
|
|
7
|
+
readFile: (...args) => fss.exists(...args) ? fsSync.readFileSync(...args) : null,
|
|
8
|
+
readText: (path) => fss.exists(path) ? fsSync.readFileSync(path, 'utf8') : null,
|
|
9
|
+
readJSON: (...args) => {
|
|
10
|
+
try { return fss.exists(...args) ? JSON.parse(fsSync.readFileSync(...args), 'utf8') : {} }
|
|
11
|
+
catch { return {} }
|
|
12
|
+
},
|
|
13
|
+
writeJSON: (path, json) => fsSync.writeFileSync(path, JSON.stringify(json, null, 2)),
|
|
14
|
+
readDir: (...args) => fss.exists(...args) ? fsSync.readdirSync(...args) : [],
|
|
15
|
+
readDirVisible: (path) => (
|
|
16
|
+
fss.readDir(path, { withFileTypes: true }) || []
|
|
17
|
+
).filter(e => e.isDirectory() && !e.name.startsWith('.')).map(e => e.name),
|
|
18
|
+
isDir: (path) => {
|
|
19
|
+
try { return fsSync.statSync(path).isDirectory() }
|
|
20
|
+
catch { return false }
|
|
21
|
+
},
|
|
22
|
+
ensureDir: (path) => { if (!fss.exists(path)) fsSync.mkdirSync(path, { recursive: true }) },
|
|
23
|
+
writeText: (path, text) => fsSync.writeFileSync(path, text),
|
|
24
|
+
listFiles: (path, ext = null) => fss.readDir(path).filter(f => !ext || f.endsWith(ext)),
|
|
25
|
+
safeUnlink: (path) => { if (fss.exists(path)) fsSync.unlinkSync(path) },
|
|
26
|
+
safeRemove: (path) => { if (fss.exists(path)) fsSync.rmSync(path, { recursive: true, force: true }) },
|
|
27
|
+
copyFile: fsSync.copyFileSync,
|
|
28
|
+
remove: fsSync.rmSync,
|
|
29
|
+
writeDir: fsSync.mkdirSync,
|
|
30
|
+
unlink: fsSync.unlinkSync,
|
|
31
|
+
stat: fsSync.statSync,
|
|
32
|
+
writeFile: fsSync.writeFileSync
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const fsa = {
|
|
36
|
+
exists: async (path) => {
|
|
37
|
+
try { await fsAsync.access(path, constants.F_OK); return true }
|
|
38
|
+
catch { return false }
|
|
39
|
+
},
|
|
40
|
+
readFile: async (path) => await fsa.exists(path) ? await fsAsync.readFile(path) : null,
|
|
41
|
+
readText: async (path) => await fsa.exists(path) ? await fsAsync.readFile(path, 'utf8') : null,
|
|
42
|
+
readJSON: async (path) => {
|
|
43
|
+
try { return await fsa.exists(path) ? JSON.parse(await fsAsync.readFile(path, 'utf8')) : {} }
|
|
44
|
+
catch { return {} }
|
|
45
|
+
},
|
|
46
|
+
writeJSON: async (path, json) => await fsAsync.writeFile(path, JSON.stringify(json, null, 2)),
|
|
47
|
+
readDir: async (path, opts) => await fsa.exists(path) ? await fsAsync.readdir(path, opts) : [],
|
|
48
|
+
readDirVisible: async (path) => (await fsa.readDir(path, { withFileTypes: true }))
|
|
49
|
+
.filter(e => e.isDirectory() && !e.name.startsWith('.')).map(e => e.name),
|
|
50
|
+
isDir: async (path) => {
|
|
51
|
+
try { return (await fsAsync.stat(path)).isDirectory() }
|
|
52
|
+
catch { return false }
|
|
53
|
+
},
|
|
54
|
+
ensureDir: async (path) => { if (!await fsa.exists(path)) await fsAsync.mkdir(path, { recursive: true }) },
|
|
55
|
+
writeText: async (path, text) => await fsAsync.writeFile(path, text),
|
|
56
|
+
listFiles: async (path, ext = null) => (await fsa.readDir(path)).filter(f => !ext || f.endsWith(ext)),
|
|
57
|
+
safeUnlink: async (path) => { if (await fsa.exists(path)) await fsAsync.unlink(path) },
|
|
58
|
+
safeRemove: async (path) => { if (await fsa.exists(path)) await fsAsync.rm(path, { recursive: true, force: true }) },
|
|
59
|
+
copyFile: fsAsync.copyFile,
|
|
60
|
+
remove: fsAsync.rm,
|
|
61
|
+
writeDir: fsAsync.mkdir,
|
|
62
|
+
unlink: fsAsync.unlink,
|
|
63
|
+
stat: fsAsync.stat,
|
|
64
|
+
writeFile: fsAsync.writeFile
|
|
65
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fullstackunicorn/filesystem",
|
|
3
|
+
"author": "lucanigido[](https://fullstackunicorn.dev/author/lucanigido)",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"private": false,
|
|
9
|
+
"description": "A simple sync/async file system wrapper for Node.js",
|
|
10
|
+
"keywords": ["filesystem", "fs", "node", "sync", "async"],
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://gitlab.com/fullstackunicorn/filesystem.git"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"index.js"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
20
|
+
}
|
|
21
|
+
}
|