@bemoje/fs 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/README.md +130 -0
- package/index.mjs +3 -7
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# @bemoje/fs
|
|
2
|
+
|
|
3
|
+
File system utilities for reading, updating, and managing files and directories.
|
|
4
|
+
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](https://nodejs.org/api/esm.html)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @bemoje/fs
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
### Walk a Directory
|
|
17
|
+
|
|
18
|
+
Recursively traverse a directory tree:
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { walkDirectory } from '@bemoje/fs'
|
|
22
|
+
|
|
23
|
+
// Get all file paths
|
|
24
|
+
const files = walkDirectory('./src', { only: 'files' })
|
|
25
|
+
|
|
26
|
+
// Get paths with stats
|
|
27
|
+
const entries = walkDirectory('./src', { stats: true })
|
|
28
|
+
entries.forEach(([path, stats]) => {
|
|
29
|
+
console.log(path, stats.size)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
// Filter directories and limit depth
|
|
33
|
+
const shallow = walkDirectory('./src', { maxDepth: 2, filter: (dirpath, basename) => basename !== 'node_modules' })
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Update Files
|
|
37
|
+
|
|
38
|
+
Read, transform, and write files in one operation:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { updateFile, updateFileSync } from '@bemoje/fs'
|
|
42
|
+
|
|
43
|
+
// Async
|
|
44
|
+
await updateFile('config.json', (content) => {
|
|
45
|
+
return content.replace(/localhost/g, 'production.host')
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
// Sync
|
|
49
|
+
updateFileSync('config.json', (content) => {
|
|
50
|
+
return content.replace(/localhost/g, 'production.host')
|
|
51
|
+
})
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Update File Lines
|
|
55
|
+
|
|
56
|
+
Transform files line-by-line:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { updateFileLines, updateFileLinesSync } from '@bemoje/fs'
|
|
60
|
+
|
|
61
|
+
await updateFileLines('data.csv', (lines) => {
|
|
62
|
+
return lines.filter((line) => line.trim() !== '')
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
updateFileLinesSync('data.csv', (lines) => {
|
|
66
|
+
return lines.map((line) => line.toUpperCase())
|
|
67
|
+
})
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Update JSON Files
|
|
71
|
+
|
|
72
|
+
Read, transform, and write JSON files:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { updateJsonFile, updateJsonFileSync } from '@bemoje/fs'
|
|
76
|
+
|
|
77
|
+
await updateJsonFile('package.json', (pkg) => {
|
|
78
|
+
pkg.version = '2.0.0'
|
|
79
|
+
return pkg
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
updateJsonFileSync('tsconfig.json', (config) => {
|
|
83
|
+
config.compilerOptions.strict = true
|
|
84
|
+
return config
|
|
85
|
+
})
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### File Utilities
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import { getFileAge, deleteOlderThan, readFileFirstLine, getFirstFileInDir } from '@bemoje/fs'
|
|
92
|
+
|
|
93
|
+
// Get file age in milliseconds
|
|
94
|
+
const age = await getFileAge('./data.json')
|
|
95
|
+
|
|
96
|
+
// Delete files older than 24 hours
|
|
97
|
+
await deleteOlderThan('./cache', 24 * 60 * 60 * 1000)
|
|
98
|
+
|
|
99
|
+
// Read only the first line of a file
|
|
100
|
+
const header = await readFileFirstLine('./data.csv')
|
|
101
|
+
|
|
102
|
+
// Get the first file in a directory
|
|
103
|
+
const firstFile = await getFirstFileInDir('./uploads')
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Data URL
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
import { removeDataUrlSchemePrefix } from '@bemoje/fs'
|
|
110
|
+
|
|
111
|
+
removeDataUrlSchemePrefix('data:image/png;base64,iVBOR...')
|
|
112
|
+
// 'iVBOR...'
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## API Reference
|
|
116
|
+
|
|
117
|
+
| Export | Description |
|
|
118
|
+
| --------------------------- | ----------------------------------------------------- |
|
|
119
|
+
| `walkDirectory` | Recursively walk a directory with filtering and stats |
|
|
120
|
+
| `updateFile` | Async read-transform-write a file |
|
|
121
|
+
| `updateFileSync` | Sync read-transform-write a file |
|
|
122
|
+
| `updateFileLines` | Async read-transform-write a file by lines |
|
|
123
|
+
| `updateFileLinesSync` | Sync read-transform-write a file by lines |
|
|
124
|
+
| `updateJsonFile` | Async read-transform-write a JSON file |
|
|
125
|
+
| `updateJsonFileSync` | Sync read-transform-write a JSON file |
|
|
126
|
+
| `deleteOlderThan` | Delete files older than a given age |
|
|
127
|
+
| `getFileAge` | Get file age in milliseconds |
|
|
128
|
+
| `getFirstFileInDir` | Get the first file name in a directory |
|
|
129
|
+
| `readFileFirstLine` | Read only the first line of a file |
|
|
130
|
+
| `removeDataUrlSchemePrefix` | Strip data URL scheme prefix from a string |
|
package/index.mjs
CHANGED
|
@@ -135,13 +135,9 @@ function walkDirectory(dirpath, options) {
|
|
|
135
135
|
stats
|
|
136
136
|
];
|
|
137
137
|
});
|
|
138
|
-
|
|
139
|
-
return
|
|
140
|
-
}
|
|
141
|
-
return entries.map(([fspath]) => {
|
|
142
|
-
return fspath;
|
|
143
|
-
});
|
|
144
|
-
}
|
|
138
|
+
return options.stats ? entries : entries.map(([fspath]) => {
|
|
139
|
+
return fspath;
|
|
140
|
+
});
|
|
145
141
|
}
|
|
146
142
|
__name(walkDirectory, "walkDirectory");
|
|
147
143
|
function convertToWalkdirOptions(options) {
|