@fullstackunicorn/filesystem 1.0.0 → 1.0.1
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 +2 -2
- package/package.json +9 -2
- package/readme.md +122 -0
package/index.js
CHANGED
|
@@ -10,7 +10,7 @@ export const fss = {
|
|
|
10
10
|
try { return fss.exists(...args) ? JSON.parse(fsSync.readFileSync(...args), 'utf8') : {} }
|
|
11
11
|
catch { return {} }
|
|
12
12
|
},
|
|
13
|
-
writeJSON: (path, json) => fsSync.writeFileSync(path, JSON.stringify(json, null,
|
|
13
|
+
writeJSON: (path, json) => fsSync.writeFileSync(path, JSON.stringify(json, null, 4)),
|
|
14
14
|
readDir: (...args) => fss.exists(...args) ? fsSync.readdirSync(...args) : [],
|
|
15
15
|
readDirVisible: (path) => (
|
|
16
16
|
fss.readDir(path, { withFileTypes: true }) || []
|
|
@@ -43,7 +43,7 @@ export const fsa = {
|
|
|
43
43
|
try { return await fsa.exists(path) ? JSON.parse(await fsAsync.readFile(path, 'utf8')) : {} }
|
|
44
44
|
catch { return {} }
|
|
45
45
|
},
|
|
46
|
-
writeJSON: async (path, json) => await fsAsync.writeFile(path, JSON.stringify(json, null,
|
|
46
|
+
writeJSON: async (path, json) => await fsAsync.writeFile(path, JSON.stringify(json, null, 4)),
|
|
47
47
|
readDir: async (path, opts) => await fsa.exists(path) ? await fsAsync.readdir(path, opts) : [],
|
|
48
48
|
readDirVisible: async (path) => (await fsa.readDir(path, { withFileTypes: true }))
|
|
49
49
|
.filter(e => e.isDirectory() && !e.name.startsWith('.')).map(e => e.name),
|
package/package.json
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fullstackunicorn/filesystem",
|
|
3
3
|
"author": "lucanigido[](https://fullstackunicorn.dev/author/lucanigido)",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.1",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"private": false,
|
|
9
9
|
"description": "A simple sync/async file system wrapper for Node.js",
|
|
10
|
-
"
|
|
10
|
+
"readme": "README.md",
|
|
11
|
+
"keywords": [
|
|
12
|
+
"filesystem",
|
|
13
|
+
"fs",
|
|
14
|
+
"node",
|
|
15
|
+
"sync",
|
|
16
|
+
"async"
|
|
17
|
+
],
|
|
11
18
|
"repository": {
|
|
12
19
|
"type": "git",
|
|
13
20
|
"url": "https://gitlab.com/fullstackunicorn/filesystem.git"
|
package/readme.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# @fullstackunicorn/filesystem
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@fullstackunicorn/filesystem)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
[](https://nodejs.org/)
|
|
6
|
+
|
|
7
|
+
A lightweight, zero-dependency wrapper for Node.js's built-in `fs` module. It provides safe, crash-proof sync (`fss`) and async (`fsa`) APIs for common file system tasks. The goal is simplicity: one-liners that handle edge cases like non-existent paths or files, ensuring your code doesn't throw unexpected errors.
|
|
8
|
+
|
|
9
|
+
Perfect for scripts, CLIs, or apps where you need reliable file I/O without boilerplate checks.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
- **Safe by Default**: Built-in existence checks prevent crashes (e.g., `readJSON` returns `{}` if file missing or invalid).
|
|
13
|
+
- **One-Line Operations**: Write JSON to a new dir? `fss.writeJSON('./new/dir/config.json', data);`—it auto-creates dirs.
|
|
14
|
+
- **Sync & Async**: Choose blocking (`fss`) or promise-based (`fsa`) methods.
|
|
15
|
+
- **No Dependencies**: Pure Node.js (requires Node 14+ for `rm` support).
|
|
16
|
+
- **Clear API**: Consistent methods like `readJSON`, `writeJSON`, `safeUnlink`.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
```bash
|
|
20
|
+
npm install @fullstackunicorn/filesystem
|
|
21
|
+
```
|
|
22
|
+
```bash
|
|
23
|
+
yarn add @fullstackunicorn/filesystem
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
```javascript
|
|
28
|
+
import { fss, fsa } from '@fullstackunicorn/filesystem';
|
|
29
|
+
|
|
30
|
+
// Sync: Safe JSON read (returns {} if missing)
|
|
31
|
+
const config = fss.readJSON('./config.json');
|
|
32
|
+
console.log(config);
|
|
33
|
+
|
|
34
|
+
// Sync: Write JSON (auto-creates dir if needed)
|
|
35
|
+
fss.writeJSON('./logs/app.json', { message: 'Hello, safe FS!' });
|
|
36
|
+
|
|
37
|
+
// Async: List files with extension filter
|
|
38
|
+
const jsFiles = await fsa.listFiles('./src', '.js');
|
|
39
|
+
console.log(jsFiles);
|
|
40
|
+
|
|
41
|
+
// Safe removal (no error if missing)
|
|
42
|
+
fss.safeRemove('./temp/dir');
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## API Reference
|
|
46
|
+
|
|
47
|
+
### Sync API (`fss`)
|
|
48
|
+
All methods are synchronous and return fallbacks on errors (e.g., `null`, `[]`, `{}`).
|
|
49
|
+
|
|
50
|
+
| Method | Description | Parameters | Returns |
|
|
51
|
+
|--------|-------------|------------|---------|
|
|
52
|
+
| `exists(path)` | Checks if path exists. | `path: string` | `boolean` |
|
|
53
|
+
| `readFile(path)` | Reads file as Buffer. | `path: string` | `Buffer \| null` |
|
|
54
|
+
| `readText(path)` | Reads file as UTF-8 string. | `path: string` | `string \| null` |
|
|
55
|
+
| `readJSON(path)` | Reads and parses JSON. | `path: string` | `object` (empty `{}` on error) |
|
|
56
|
+
| `writeJSON(path, json)` | Writes pretty JSON (creates dir if needed). | `path: string`, `json: object` | `void` |
|
|
57
|
+
| `readDir(path, opts?)` | Lists dir contents. | `path: string`, `opts?: object` | `string[]` |
|
|
58
|
+
| `readDirVisible(path)` | Lists visible subdirs (no `.` hidden). | `path: string` | `string[]` |
|
|
59
|
+
| `isDir(path)` | Checks if path is directory. | `path: string` | `boolean` |
|
|
60
|
+
| `ensureDir(path)` | Creates dir recursively if missing. | `path: string` | `void` |
|
|
61
|
+
| `writeText(path, text)` | Writes UTF-8 text. | `path: string`, `text: string` | `void` |
|
|
62
|
+
| `listFiles(path, ext?)` | Lists files (optional extension filter). | `path: string`, `ext?: string` | `string[]` |
|
|
63
|
+
| `safeUnlink(path)` | Removes file (no-op if missing). | `path: string` | `void` |
|
|
64
|
+
| `safeRemove(path)` | Removes file/dir recursively (no-op if missing). | `path: string` | `void` |
|
|
65
|
+
| `copyFile(src, dest)` | Copies file. | `src: string`, `dest: string` | `void` |
|
|
66
|
+
|
|
67
|
+
### Async API (`fsa`)
|
|
68
|
+
Promise-based equivalents of `fss` methods. Use `await` for results.
|
|
69
|
+
|
|
70
|
+
| Method | Description | Parameters | Returns |
|
|
71
|
+
|--------|-------------|------------|---------|
|
|
72
|
+
| `exists(path)` | Checks if path exists. | `path: string` | `Promise<boolean>` |
|
|
73
|
+
| `readFile(path)` | Reads file as Buffer. | `path: string` | `Promise<Buffer \| null>` |
|
|
74
|
+
| `readText(path)` | Reads file as UTF-8 string. | `path: string` | `Promise<string \| null>` |
|
|
75
|
+
| `readJSON(path)` | Reads and parses JSON. | `path: string` | `Promise<object>` (empty `{}` on error) |
|
|
76
|
+
| `writeJSON(path, json)` | Writes pretty JSON (creates dir if needed). | `path: string`, `json: object` | `Promise<void>` |
|
|
77
|
+
| `readDir(path, opts?)` | Lists dir contents. | `path: string`, `opts?: object` | `Promise<string[]>` |
|
|
78
|
+
| `readDirVisible(path)` | Lists visible subdirs (no `.` hidden). | `path: string` | `Promise<string[]>` |
|
|
79
|
+
| `isDir(path)` | Checks if path is directory. | `path: string` | `Promise<boolean>` |
|
|
80
|
+
| `ensureDir(path)` | Creates dir recursively if missing. | `path: string` | `Promise<void>` |
|
|
81
|
+
| `writeText(path, text)` | Writes UTF-8 text. | `path: string`, `text: string` | `Promise<void>` |
|
|
82
|
+
| `listFiles(path, ext?)` | Lists files (optional extension filter). | `path: string`, `ext?: string` | `Promise<string[]>` |
|
|
83
|
+
| `safeUnlink(path)` | Removes file (no-op if missing). | `path: string` | `Promise<void>` |
|
|
84
|
+
| `safeRemove(path)` | Removes file/dir recursively (no-op if missing). | `path: string` | `Promise<void>` |
|
|
85
|
+
| `copyFile(src, dest)` | Copies file. | `src: string`, `dest: string` | `Promise<void>` |
|
|
86
|
+
|
|
87
|
+
**Notes**:
|
|
88
|
+
- JSON methods use 4-space pretty-printing.
|
|
89
|
+
- `readDir` supports `{ withFileTypes: true }` for Dirent objects.
|
|
90
|
+
- Errors are swallowed where possible (e.g., invalid JSON → `{}`); use `try-catch` for strict error handling.
|
|
91
|
+
|
|
92
|
+
## Examples
|
|
93
|
+
### Safe Config Management
|
|
94
|
+
```javascript
|
|
95
|
+
import { fss } from '@fullstackunicorn/filesystem';
|
|
96
|
+
|
|
97
|
+
function loadOrCreateConfig() {
|
|
98
|
+
let config = fss.readJSON('./config.json');
|
|
99
|
+
if (Object.keys(config).length === 0) {
|
|
100
|
+
config = { theme: 'dark', lang: 'en' };
|
|
101
|
+
fss.writeJSON('./config.json', config);
|
|
102
|
+
}
|
|
103
|
+
return config;
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Async Dir Cleanup
|
|
108
|
+
```javascript
|
|
109
|
+
import { fsa } from '@fullstackunicorn/filesystem';
|
|
110
|
+
|
|
111
|
+
async function cleanupTemp() {
|
|
112
|
+
await fsa.ensureDir('./temp');
|
|
113
|
+
// ... write temp files ...
|
|
114
|
+
await fsa.safeRemove('./temp'); // Safe even if empty/missing
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Contributing
|
|
119
|
+
Fork the repo, make changes, and submit a PR. Tests welcome!
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
MIT © [fullstackunicorn.dev/author/lucanigido](https://fullstackunicorn.dev/author/lucanigido)
|