ktfile 1.2.0 → 1.2.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/CONTRIBUTING.md +290 -290
- package/LICENSE +21 -21
- package/README.md +433 -433
- package/index.min.js +1 -1
- package/package.json +44 -44
- package/types/IConfig.d.ts +0 -0
- package/types/IFile.d.ts +0 -0
- package/types/Utils.d.ts +0 -0
- package/types/async/ConfigAsync.d.ts +0 -0
- package/types/async/FileAsync.d.ts +0 -0
- package/types/async/IAsyncFS.d.ts +0 -0
- package/types/ktfile.d.ts +0 -0
- package/types/sync/ConfigSync.d.ts +0 -0
- package/types/sync/FileSync.d.ts +0 -0
- package/types/sync/ISyncFS.d.ts +0 -0
package/README.md
CHANGED
|
@@ -1,433 +1,433 @@
|
|
|
1
|
-
# KTFile
|
|
2
|
-
|
|
3
|
-
A powerful, cross-platform file system library for JavaScript/TypeScript that provides both synchronous and asynchronous APIs with a clean, object-oriented interface.
|
|
4
|
-
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
- 🚀 **Dual API**: Both synchronous (`FileSync`) and asynchronous (`FileAsync`) operations
|
|
8
|
-
- 🔧 **Cross-platform**: Works in Node.js and other JavaScript environments
|
|
9
|
-
- 📁 **Rich file operations**: Create, read, write, copy, move, delete files and directories
|
|
10
|
-
- 🎯 **Type-safe**: Full TypeScript support with detailed type definitions
|
|
11
|
-
- 🔗 **Path manipulation**: Intuitive path joining and navigation
|
|
12
|
-
- 📊 **File metadata**: Access file properties like size, timestamps, permissions
|
|
13
|
-
- 🔄 **Directory traversal**: Walk through directory trees with generators
|
|
14
|
-
- 📝 **Multiple formats**: Support for text, binary, and JSON file operations
|
|
15
|
-
|
|
16
|
-
## Installation
|
|
17
|
-
|
|
18
|
-
```bash
|
|
19
|
-
npm install ktfile
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## Quick Start
|
|
23
|
-
|
|
24
|
-
### Basic Usage
|
|
25
|
-
|
|
26
|
-
```javascript
|
|
27
|
-
import { fileSync, fileAsync, File } from 'ktfile'
|
|
28
|
-
|
|
29
|
-
// Synchronous API
|
|
30
|
-
const file = fileSync('/path/to/file.txt')
|
|
31
|
-
file.write('Hello, world!')
|
|
32
|
-
console.log(file.read()) // "Hello, world!"
|
|
33
|
-
|
|
34
|
-
// Asynchronous API
|
|
35
|
-
const asyncFile = fileAsync('/path/to/file.txt')
|
|
36
|
-
await asyncFile.write('Hello, async world!')
|
|
37
|
-
console.log(await asyncFile.read()) // "Hello, async world!"
|
|
38
|
-
|
|
39
|
-
// Alternative syntax
|
|
40
|
-
const file2 = new File('/another/path')
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
### Node.js Setup
|
|
44
|
-
|
|
45
|
-
The library automatically initializes with Node.js `fs` module when available. For custom environments:
|
|
46
|
-
|
|
47
|
-
```javascript
|
|
48
|
-
import { initFS } from 'ktfile'
|
|
49
|
-
import fs from 'fs'
|
|
50
|
-
|
|
51
|
-
initFS(fs)
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
## API Reference
|
|
55
|
-
|
|
56
|
-
### FileSync Class
|
|
57
|
-
|
|
58
|
-
#### Properties
|
|
59
|
-
|
|
60
|
-
- `fs: ISyncFS` - The underlying file system instance
|
|
61
|
-
- `canExecute: boolean` - File execute permission
|
|
62
|
-
- `canRead: boolean` - File read permission
|
|
63
|
-
- `canWrite: boolean` - File write permission
|
|
64
|
-
- `creationTime: Date | null` - File creation timestamp
|
|
65
|
-
- `lastModified: Date | null` - Last modification timestamp
|
|
66
|
-
- `lastAccess: Date | null` - Last access timestamp
|
|
67
|
-
- `exists: boolean | null` - Whether the file exists
|
|
68
|
-
- `name: string` - File name with extension
|
|
69
|
-
- `nameWithoutExtension: string` - File name without extension
|
|
70
|
-
- `extension: string` - File extension
|
|
71
|
-
- `parent: FileSync | null` - Parent directory
|
|
72
|
-
- `uri: string` - File URI
|
|
73
|
-
- `separator: string` - Path separator for the platform
|
|
74
|
-
- `isDirectory: boolean | null` - Whether this is a directory
|
|
75
|
-
- `isFile: boolean | null` - Whether this is a regular file
|
|
76
|
-
- `isEmpty`: boolean | null` - Whether the file or directory is empty
|
|
77
|
-
- `isHidden: boolean` - Whether the file is hidden
|
|
78
|
-
- `isSymbolicLink: boolean | null` - Whether this is a symbolic link
|
|
79
|
-
- `size: number | null` - File size in bytes
|
|
80
|
-
- `sizeKB: number | null` - File size in kilobytes
|
|
81
|
-
- `sizeMB: number | null` - File size in megabytes
|
|
82
|
-
- `sizeGB: number | null` - File size in gigabytes
|
|
83
|
-
|
|
84
|
-
#### Methods
|
|
85
|
-
|
|
86
|
-
##### Path Operations
|
|
87
|
-
```javascript
|
|
88
|
-
// Join paths
|
|
89
|
-
const newPath = file.to('subdirectory', 'file.txt')
|
|
90
|
-
|
|
91
|
-
// Check if path contains another path
|
|
92
|
-
const contains = parentDir.contains(childFile)
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
##### File Creation and Deletion
|
|
96
|
-
```javascript
|
|
97
|
-
// Create file
|
|
98
|
-
file.createFile()
|
|
99
|
-
|
|
100
|
-
// Create temporary file
|
|
101
|
-
const tempFile = FileSync.createTempFile(directory, 'prefix', '.tmp')
|
|
102
|
-
|
|
103
|
-
// Delete file or directory
|
|
104
|
-
file.delete(recursive = false, force = false)
|
|
105
|
-
|
|
106
|
-
// Schedule deletion on exit
|
|
107
|
-
file.deleteOnExit(recursive = false)
|
|
108
|
-
|
|
109
|
-
// Clear directory contents
|
|
110
|
-
directory.clear(recursive = true)
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
##### Directory Operations
|
|
114
|
-
```javascript
|
|
115
|
-
// Create directory
|
|
116
|
-
directory.mkdir(recursive = false)
|
|
117
|
-
|
|
118
|
-
// Create directory tree
|
|
119
|
-
directory.mkdirs()
|
|
120
|
-
|
|
121
|
-
// List files in directory
|
|
122
|
-
const files = directory.listFiles()
|
|
123
|
-
const filenames = directory.listFilenames()
|
|
124
|
-
|
|
125
|
-
// Walk directory tree
|
|
126
|
-
for (const file of directory.walk()) {
|
|
127
|
-
console.log(file.name)
|
|
128
|
-
}
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
##### File Operations
|
|
132
|
-
```javascript
|
|
133
|
-
// Copy file
|
|
134
|
-
file.copyTo(destination, overwrite = false, recursive = false)
|
|
135
|
-
|
|
136
|
-
// Move/rename file
|
|
137
|
-
file.renameTo(newLocation, overwrite = false, recursive = false)
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
##### Reading Files
|
|
141
|
-
```javascript
|
|
142
|
-
// Read as string
|
|
143
|
-
const content = file.read('utf8')
|
|
144
|
-
|
|
145
|
-
// Read as Buffer
|
|
146
|
-
const buffer = file.read()
|
|
147
|
-
|
|
148
|
-
// Read lines as array
|
|
149
|
-
const lines = file.readLines('utf8')
|
|
150
|
-
|
|
151
|
-
// Read JSON
|
|
152
|
-
const data = file.readJSON()
|
|
153
|
-
|
|
154
|
-
// Read symbolic link target
|
|
155
|
-
const target = symlink.readlink()
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
##### Writing Files
|
|
159
|
-
```javascript
|
|
160
|
-
// Write string or Buffer
|
|
161
|
-
file.write('content', 'utf8')
|
|
162
|
-
file.write(buffer)
|
|
163
|
-
|
|
164
|
-
// Write with custom encoder
|
|
165
|
-
file.write(data, { write: () => data })
|
|
166
|
-
|
|
167
|
-
// Write JSON
|
|
168
|
-
file.writeJSON({ key: 'value' })
|
|
169
|
-
|
|
170
|
-
// Append to file
|
|
171
|
-
file.append('more content', 'utf8')
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
### FileAsync Class
|
|
175
|
-
|
|
176
|
-
The `FileAsync` class provides Promise-based asynchronous operations with a similar API to `FileSync`:
|
|
177
|
-
|
|
178
|
-
#### Properties
|
|
179
|
-
|
|
180
|
-
- `fs: IAsyncFS` - The underlying async file system instance
|
|
181
|
-
- `name: string` - File name with extension
|
|
182
|
-
- `nameWithoutExtension: string` - File name without extension
|
|
183
|
-
- `extension: string` - File extension
|
|
184
|
-
- `parent: FileAsync | null` - Parent directory
|
|
185
|
-
- `uri: string` - File URI
|
|
186
|
-
- `separator: string` - Path separator for the platform
|
|
187
|
-
- `isHidden: boolean` - Whether the file is hidden
|
|
188
|
-
- `sync: FileSync` - Access to synchronous version of this file
|
|
189
|
-
|
|
190
|
-
#### Methods
|
|
191
|
-
|
|
192
|
-
All methods that return metadata or perform operations are asynchronous and return Promises:
|
|
193
|
-
|
|
194
|
-
##### Permission Methods
|
|
195
|
-
```javascript
|
|
196
|
-
// Check permissions
|
|
197
|
-
const canRead = await file.canRead()
|
|
198
|
-
const canWrite = await file.canWrite()
|
|
199
|
-
const canExecute = await file.canExecute()
|
|
200
|
-
|
|
201
|
-
// Set permissions
|
|
202
|
-
await file.setReadable(true)
|
|
203
|
-
await file.setWritable(false)
|
|
204
|
-
await file.setExecutable(true)
|
|
205
|
-
```
|
|
206
|
-
|
|
207
|
-
##### Metadata Methods
|
|
208
|
-
```javascript
|
|
209
|
-
// Get timestamps
|
|
210
|
-
const created = await file.creationTime()
|
|
211
|
-
const modified = await file.lastModified()
|
|
212
|
-
const accessed = await file.lastAccess()
|
|
213
|
-
|
|
214
|
-
// Set timestamps
|
|
215
|
-
await file.setCreationTime(new Date())
|
|
216
|
-
await file.setLastModified(new Date())
|
|
217
|
-
|
|
218
|
-
// File information
|
|
219
|
-
const exists = await file.exists()
|
|
220
|
-
const isDir = await file.isDirectory()
|
|
221
|
-
const isFile = await file.isFile()
|
|
222
|
-
const isEmpty = await file.isEmpty()
|
|
223
|
-
const isLink = await file.isSymbolicLink()
|
|
224
|
-
|
|
225
|
-
// File sizes
|
|
226
|
-
const bytes = await file.size()
|
|
227
|
-
const kb = await file.sizeKB()
|
|
228
|
-
const mb = await file.sizeMB()
|
|
229
|
-
const gb = await file.sizeGB()
|
|
230
|
-
```
|
|
231
|
-
|
|
232
|
-
##### File Operations
|
|
233
|
-
```javascript
|
|
234
|
-
// Create and delete
|
|
235
|
-
await file.createFile()
|
|
236
|
-
await file.delete(recursive, force)
|
|
237
|
-
await directory.clear(recursive)
|
|
238
|
-
|
|
239
|
-
// Copy and move
|
|
240
|
-
await file.copyTo(destination, overwrite, recursive)
|
|
241
|
-
await file.renameTo(newLocation, overwrite, recursive)
|
|
242
|
-
```
|
|
243
|
-
|
|
244
|
-
##### Directory Operations
|
|
245
|
-
```javascript
|
|
246
|
-
// Create directories
|
|
247
|
-
await directory.mkdir(recursive)
|
|
248
|
-
await directory.mkdirs()
|
|
249
|
-
|
|
250
|
-
// List contents
|
|
251
|
-
const files = await directory.listFiles()
|
|
252
|
-
const filenames = await directory.listFilenames()
|
|
253
|
-
|
|
254
|
-
// Walk directory tree (AsyncGenerator)
|
|
255
|
-
for await (const file of directory.walk()) {
|
|
256
|
-
console.log(file.name)
|
|
257
|
-
}
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
##### Reading Files
|
|
261
|
-
```javascript
|
|
262
|
-
// Read file contents
|
|
263
|
-
const content = await file.read('utf8')
|
|
264
|
-
const buffer = await file.read()
|
|
265
|
-
const lines = await file.readLines('utf8')
|
|
266
|
-
const data = await file.readJSON()
|
|
267
|
-
|
|
268
|
-
// Read symbolic link
|
|
269
|
-
const target = await symlink.readlink()
|
|
270
|
-
```
|
|
271
|
-
|
|
272
|
-
##### Writing Files
|
|
273
|
-
```javascript
|
|
274
|
-
// Write content
|
|
275
|
-
await file.write('content', 'utf8')
|
|
276
|
-
await file.write(buffer)
|
|
277
|
-
await file.writeJSON({ key: 'value' })
|
|
278
|
-
await file.append('more content', 'utf8')
|
|
279
|
-
```
|
|
280
|
-
|
|
281
|
-
##### Static Methods
|
|
282
|
-
```javascript
|
|
283
|
-
// Create temporary file
|
|
284
|
-
const tempFile = FileAsync.createTempFile(directory, 'prefix', '.tmp')
|
|
285
|
-
```
|
|
286
|
-
|
|
287
|
-
## Examples
|
|
288
|
-
|
|
289
|
-
### Working with Directories
|
|
290
|
-
|
|
291
|
-
```javascript
|
|
292
|
-
import { fileSync } from 'ktfile'
|
|
293
|
-
|
|
294
|
-
const projectDir = fileSync('./my-project')
|
|
295
|
-
|
|
296
|
-
// Create project structure
|
|
297
|
-
projectDir.mkdirs()
|
|
298
|
-
projectDir.to('src').mkdir()
|
|
299
|
-
projectDir.to('tests').mkdir()
|
|
300
|
-
projectDir.to('docs').mkdir()
|
|
301
|
-
|
|
302
|
-
// Create files
|
|
303
|
-
const packageJson = projectDir.to('package.json')
|
|
304
|
-
packageJson.writeJSON({
|
|
305
|
-
name: 'my-project',
|
|
306
|
-
version: '1.0.0'
|
|
307
|
-
})
|
|
308
|
-
|
|
309
|
-
const readme = projectDir.to('README.md')
|
|
310
|
-
readme.write('# My Project\n\nDescription here...')
|
|
311
|
-
|
|
312
|
-
// List all files
|
|
313
|
-
for (const file of projectDir.walk()) {
|
|
314
|
-
if (file.isFile) {
|
|
315
|
-
console.log(`File: ${file.name} (${file.sizeKB} KB)`)
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
```
|
|
319
|
-
|
|
320
|
-
### File Manipulation
|
|
321
|
-
|
|
322
|
-
```javascript
|
|
323
|
-
import { fileSync } from 'ktfile'
|
|
324
|
-
|
|
325
|
-
const sourceFile = fileSync('./data.json')
|
|
326
|
-
const backupFile = fileSync('./backup/data-backup.json')
|
|
327
|
-
|
|
328
|
-
// Create backup directory
|
|
329
|
-
backupFile.parent?.mkdirs()
|
|
330
|
-
|
|
331
|
-
// Copy file
|
|
332
|
-
sourceFile.copyTo(backupFile, true)
|
|
333
|
-
|
|
334
|
-
// Read and modify JSON
|
|
335
|
-
const data = sourceFile.readJSON()
|
|
336
|
-
data.lastBackup = new Date().toISOString()
|
|
337
|
-
sourceFile.writeJSON(data)
|
|
338
|
-
|
|
339
|
-
console.log(`Backup created: ${backupFile.size} bytes`)
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
### Async Operations
|
|
343
|
-
|
|
344
|
-
```javascript
|
|
345
|
-
import { fileAsync } from 'ktfile'
|
|
346
|
-
|
|
347
|
-
async function processFiles() {
|
|
348
|
-
const directory = fileAsync('./uploads')
|
|
349
|
-
const files = await directory.listFiles()
|
|
350
|
-
|
|
351
|
-
for (const file of files || []) {
|
|
352
|
-
if (file.extension === '.txt') {
|
|
353
|
-
const content = await file.read('utf8')
|
|
354
|
-
const processed = content.toUpperCase()
|
|
355
|
-
|
|
356
|
-
const outputFile = file.parent?.to('processed', file.name)
|
|
357
|
-
await outputFile?.parent?.mkdirs()
|
|
358
|
-
await outputFile?.write(processed)
|
|
359
|
-
|
|
360
|
-
console.log(`Processed: ${file.name}`)
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
processFiles().catch(console.error)
|
|
366
|
-
```
|
|
367
|
-
|
|
368
|
-
### Temporary Files
|
|
369
|
-
|
|
370
|
-
```javascript
|
|
371
|
-
import { FileSync, fileSync } from 'ktfile'
|
|
372
|
-
|
|
373
|
-
const tempDir = fileSync('./temp')
|
|
374
|
-
tempDir.mkdirs()
|
|
375
|
-
|
|
376
|
-
// Create temporary file
|
|
377
|
-
const tempFile = FileSync.createTempFile(tempDir, 'data-', '.json')
|
|
378
|
-
tempFile.writeJSON({ processing: true, timestamp: Date.now() })
|
|
379
|
-
|
|
380
|
-
// Use the temporary file
|
|
381
|
-
console.log(`Temp file created: ${tempFile.name}`)
|
|
382
|
-
|
|
383
|
-
// Clean up on exit
|
|
384
|
-
tempFile.deleteOnExit()
|
|
385
|
-
```
|
|
386
|
-
|
|
387
|
-
## Error Handling
|
|
388
|
-
|
|
389
|
-
Methods return `null` when operations fail, allowing for graceful error handling:
|
|
390
|
-
|
|
391
|
-
```javascript
|
|
392
|
-
const file = fileSync('./nonexistent.txt')
|
|
393
|
-
|
|
394
|
-
const content = file.read()
|
|
395
|
-
if (content === null) {
|
|
396
|
-
console.log('File does not exist or cannot be read')
|
|
397
|
-
} else {
|
|
398
|
-
console.log('File content:', content)
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
// Check existence before operations
|
|
402
|
-
if (file.exists) {
|
|
403
|
-
file.delete()
|
|
404
|
-
}
|
|
405
|
-
```
|
|
406
|
-
|
|
407
|
-
## Cross-Platform Compatibility
|
|
408
|
-
|
|
409
|
-
KTFile handles platform differences automatically:
|
|
410
|
-
|
|
411
|
-
```javascript
|
|
412
|
-
const file = fileSync('./path/to/file.txt')
|
|
413
|
-
|
|
414
|
-
// Uses correct separator for platform
|
|
415
|
-
console.log(file.separator) // '/' on Unix, '\' on Windows
|
|
416
|
-
|
|
417
|
-
// Paths are normalized automatically
|
|
418
|
-
const nested = file.parent?.to('..', 'sibling', 'file.txt')
|
|
419
|
-
```
|
|
420
|
-
|
|
421
|
-
## License
|
|
422
|
-
|
|
423
|
-
MIT License
|
|
424
|
-
|
|
425
|
-
See the [LICENSE](LICENSE) file for details.
|
|
426
|
-
|
|
427
|
-
## Contributing
|
|
428
|
-
|
|
429
|
-
Contributions are welcome! Please read the [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to this project.
|
|
430
|
-
|
|
431
|
-
## Support
|
|
432
|
-
|
|
433
|
-
If you find this library useful, please consider starring the repository on GitHub and sharing it with others!
|
|
1
|
+
# KTFile
|
|
2
|
+
|
|
3
|
+
A powerful, cross-platform file system library for JavaScript/TypeScript that provides both synchronous and asynchronous APIs with a clean, object-oriented interface.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🚀 **Dual API**: Both synchronous (`FileSync`) and asynchronous (`FileAsync`) operations
|
|
8
|
+
- 🔧 **Cross-platform**: Works in Node.js and other JavaScript environments
|
|
9
|
+
- 📁 **Rich file operations**: Create, read, write, copy, move, delete files and directories
|
|
10
|
+
- 🎯 **Type-safe**: Full TypeScript support with detailed type definitions
|
|
11
|
+
- 🔗 **Path manipulation**: Intuitive path joining and navigation
|
|
12
|
+
- 📊 **File metadata**: Access file properties like size, timestamps, permissions
|
|
13
|
+
- 🔄 **Directory traversal**: Walk through directory trees with generators
|
|
14
|
+
- 📝 **Multiple formats**: Support for text, binary, and JSON file operations
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install ktfile
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
### Basic Usage
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
import { fileSync, fileAsync, File } from 'ktfile'
|
|
28
|
+
|
|
29
|
+
// Synchronous API
|
|
30
|
+
const file = fileSync('/path/to/file.txt')
|
|
31
|
+
file.write('Hello, world!')
|
|
32
|
+
console.log(file.read()) // "Hello, world!"
|
|
33
|
+
|
|
34
|
+
// Asynchronous API
|
|
35
|
+
const asyncFile = fileAsync('/path/to/file.txt')
|
|
36
|
+
await asyncFile.write('Hello, async world!')
|
|
37
|
+
console.log(await asyncFile.read()) // "Hello, async world!"
|
|
38
|
+
|
|
39
|
+
// Alternative syntax
|
|
40
|
+
const file2 = new File('/another/path')
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Node.js Setup
|
|
44
|
+
|
|
45
|
+
The library automatically initializes with Node.js `fs` module when available. For custom environments:
|
|
46
|
+
|
|
47
|
+
```javascript
|
|
48
|
+
import { initFS } from 'ktfile'
|
|
49
|
+
import fs from 'fs'
|
|
50
|
+
|
|
51
|
+
initFS(fs)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## API Reference
|
|
55
|
+
|
|
56
|
+
### FileSync Class
|
|
57
|
+
|
|
58
|
+
#### Properties
|
|
59
|
+
|
|
60
|
+
- `fs: ISyncFS` - The underlying file system instance
|
|
61
|
+
- `canExecute: boolean` - File execute permission
|
|
62
|
+
- `canRead: boolean` - File read permission
|
|
63
|
+
- `canWrite: boolean` - File write permission
|
|
64
|
+
- `creationTime: Date | null` - File creation timestamp
|
|
65
|
+
- `lastModified: Date | null` - Last modification timestamp
|
|
66
|
+
- `lastAccess: Date | null` - Last access timestamp
|
|
67
|
+
- `exists: boolean | null` - Whether the file exists
|
|
68
|
+
- `name: string` - File name with extension
|
|
69
|
+
- `nameWithoutExtension: string` - File name without extension
|
|
70
|
+
- `extension: string` - File extension
|
|
71
|
+
- `parent: FileSync | null` - Parent directory
|
|
72
|
+
- `uri: string` - File URI
|
|
73
|
+
- `separator: string` - Path separator for the platform
|
|
74
|
+
- `isDirectory: boolean | null` - Whether this is a directory
|
|
75
|
+
- `isFile: boolean | null` - Whether this is a regular file
|
|
76
|
+
- `isEmpty`: boolean | null` - Whether the file or directory is empty
|
|
77
|
+
- `isHidden: boolean` - Whether the file is hidden
|
|
78
|
+
- `isSymbolicLink: boolean | null` - Whether this is a symbolic link
|
|
79
|
+
- `size: number | null` - File size in bytes
|
|
80
|
+
- `sizeKB: number | null` - File size in kilobytes
|
|
81
|
+
- `sizeMB: number | null` - File size in megabytes
|
|
82
|
+
- `sizeGB: number | null` - File size in gigabytes
|
|
83
|
+
|
|
84
|
+
#### Methods
|
|
85
|
+
|
|
86
|
+
##### Path Operations
|
|
87
|
+
```javascript
|
|
88
|
+
// Join paths
|
|
89
|
+
const newPath = file.to('subdirectory', 'file.txt')
|
|
90
|
+
|
|
91
|
+
// Check if path contains another path
|
|
92
|
+
const contains = parentDir.contains(childFile)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
##### File Creation and Deletion
|
|
96
|
+
```javascript
|
|
97
|
+
// Create file
|
|
98
|
+
file.createFile()
|
|
99
|
+
|
|
100
|
+
// Create temporary file
|
|
101
|
+
const tempFile = FileSync.createTempFile(directory, 'prefix', '.tmp')
|
|
102
|
+
|
|
103
|
+
// Delete file or directory
|
|
104
|
+
file.delete(recursive = false, force = false)
|
|
105
|
+
|
|
106
|
+
// Schedule deletion on exit
|
|
107
|
+
file.deleteOnExit(recursive = false)
|
|
108
|
+
|
|
109
|
+
// Clear directory contents
|
|
110
|
+
directory.clear(recursive = true)
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
##### Directory Operations
|
|
114
|
+
```javascript
|
|
115
|
+
// Create directory
|
|
116
|
+
directory.mkdir(recursive = false)
|
|
117
|
+
|
|
118
|
+
// Create directory tree
|
|
119
|
+
directory.mkdirs()
|
|
120
|
+
|
|
121
|
+
// List files in directory
|
|
122
|
+
const files = directory.listFiles()
|
|
123
|
+
const filenames = directory.listFilenames()
|
|
124
|
+
|
|
125
|
+
// Walk directory tree
|
|
126
|
+
for (const file of directory.walk()) {
|
|
127
|
+
console.log(file.name)
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
##### File Operations
|
|
132
|
+
```javascript
|
|
133
|
+
// Copy file
|
|
134
|
+
file.copyTo(destination, overwrite = false, recursive = false)
|
|
135
|
+
|
|
136
|
+
// Move/rename file
|
|
137
|
+
file.renameTo(newLocation, overwrite = false, recursive = false)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
##### Reading Files
|
|
141
|
+
```javascript
|
|
142
|
+
// Read as string
|
|
143
|
+
const content = file.read('utf8')
|
|
144
|
+
|
|
145
|
+
// Read as Buffer
|
|
146
|
+
const buffer = file.read()
|
|
147
|
+
|
|
148
|
+
// Read lines as array
|
|
149
|
+
const lines = file.readLines('utf8')
|
|
150
|
+
|
|
151
|
+
// Read JSON
|
|
152
|
+
const data = file.readJSON()
|
|
153
|
+
|
|
154
|
+
// Read symbolic link target
|
|
155
|
+
const target = symlink.readlink()
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
##### Writing Files
|
|
159
|
+
```javascript
|
|
160
|
+
// Write string or Buffer
|
|
161
|
+
file.write('content', 'utf8')
|
|
162
|
+
file.write(buffer)
|
|
163
|
+
|
|
164
|
+
// Write with custom encoder
|
|
165
|
+
file.write(data, { write: () => data })
|
|
166
|
+
|
|
167
|
+
// Write JSON
|
|
168
|
+
file.writeJSON({ key: 'value' })
|
|
169
|
+
|
|
170
|
+
// Append to file
|
|
171
|
+
file.append('more content', 'utf8')
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### FileAsync Class
|
|
175
|
+
|
|
176
|
+
The `FileAsync` class provides Promise-based asynchronous operations with a similar API to `FileSync`:
|
|
177
|
+
|
|
178
|
+
#### Properties
|
|
179
|
+
|
|
180
|
+
- `fs: IAsyncFS` - The underlying async file system instance
|
|
181
|
+
- `name: string` - File name with extension
|
|
182
|
+
- `nameWithoutExtension: string` - File name without extension
|
|
183
|
+
- `extension: string` - File extension
|
|
184
|
+
- `parent: FileAsync | null` - Parent directory
|
|
185
|
+
- `uri: string` - File URI
|
|
186
|
+
- `separator: string` - Path separator for the platform
|
|
187
|
+
- `isHidden: boolean` - Whether the file is hidden
|
|
188
|
+
- `sync: FileSync` - Access to synchronous version of this file
|
|
189
|
+
|
|
190
|
+
#### Methods
|
|
191
|
+
|
|
192
|
+
All methods that return metadata or perform operations are asynchronous and return Promises:
|
|
193
|
+
|
|
194
|
+
##### Permission Methods
|
|
195
|
+
```javascript
|
|
196
|
+
// Check permissions
|
|
197
|
+
const canRead = await file.canRead()
|
|
198
|
+
const canWrite = await file.canWrite()
|
|
199
|
+
const canExecute = await file.canExecute()
|
|
200
|
+
|
|
201
|
+
// Set permissions
|
|
202
|
+
await file.setReadable(true)
|
|
203
|
+
await file.setWritable(false)
|
|
204
|
+
await file.setExecutable(true)
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
##### Metadata Methods
|
|
208
|
+
```javascript
|
|
209
|
+
// Get timestamps
|
|
210
|
+
const created = await file.creationTime()
|
|
211
|
+
const modified = await file.lastModified()
|
|
212
|
+
const accessed = await file.lastAccess()
|
|
213
|
+
|
|
214
|
+
// Set timestamps
|
|
215
|
+
await file.setCreationTime(new Date())
|
|
216
|
+
await file.setLastModified(new Date())
|
|
217
|
+
|
|
218
|
+
// File information
|
|
219
|
+
const exists = await file.exists()
|
|
220
|
+
const isDir = await file.isDirectory()
|
|
221
|
+
const isFile = await file.isFile()
|
|
222
|
+
const isEmpty = await file.isEmpty()
|
|
223
|
+
const isLink = await file.isSymbolicLink()
|
|
224
|
+
|
|
225
|
+
// File sizes
|
|
226
|
+
const bytes = await file.size()
|
|
227
|
+
const kb = await file.sizeKB()
|
|
228
|
+
const mb = await file.sizeMB()
|
|
229
|
+
const gb = await file.sizeGB()
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
##### File Operations
|
|
233
|
+
```javascript
|
|
234
|
+
// Create and delete
|
|
235
|
+
await file.createFile()
|
|
236
|
+
await file.delete(recursive, force)
|
|
237
|
+
await directory.clear(recursive)
|
|
238
|
+
|
|
239
|
+
// Copy and move
|
|
240
|
+
await file.copyTo(destination, overwrite, recursive)
|
|
241
|
+
await file.renameTo(newLocation, overwrite, recursive)
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
##### Directory Operations
|
|
245
|
+
```javascript
|
|
246
|
+
// Create directories
|
|
247
|
+
await directory.mkdir(recursive)
|
|
248
|
+
await directory.mkdirs()
|
|
249
|
+
|
|
250
|
+
// List contents
|
|
251
|
+
const files = await directory.listFiles()
|
|
252
|
+
const filenames = await directory.listFilenames()
|
|
253
|
+
|
|
254
|
+
// Walk directory tree (AsyncGenerator)
|
|
255
|
+
for await (const file of directory.walk()) {
|
|
256
|
+
console.log(file.name)
|
|
257
|
+
}
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
##### Reading Files
|
|
261
|
+
```javascript
|
|
262
|
+
// Read file contents
|
|
263
|
+
const content = await file.read('utf8')
|
|
264
|
+
const buffer = await file.read()
|
|
265
|
+
const lines = await file.readLines('utf8')
|
|
266
|
+
const data = await file.readJSON()
|
|
267
|
+
|
|
268
|
+
// Read symbolic link
|
|
269
|
+
const target = await symlink.readlink()
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
##### Writing Files
|
|
273
|
+
```javascript
|
|
274
|
+
// Write content
|
|
275
|
+
await file.write('content', 'utf8')
|
|
276
|
+
await file.write(buffer)
|
|
277
|
+
await file.writeJSON({ key: 'value' })
|
|
278
|
+
await file.append('more content', 'utf8')
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
##### Static Methods
|
|
282
|
+
```javascript
|
|
283
|
+
// Create temporary file
|
|
284
|
+
const tempFile = FileAsync.createTempFile(directory, 'prefix', '.tmp')
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
## Examples
|
|
288
|
+
|
|
289
|
+
### Working with Directories
|
|
290
|
+
|
|
291
|
+
```javascript
|
|
292
|
+
import { fileSync } from 'ktfile'
|
|
293
|
+
|
|
294
|
+
const projectDir = fileSync('./my-project')
|
|
295
|
+
|
|
296
|
+
// Create project structure
|
|
297
|
+
projectDir.mkdirs()
|
|
298
|
+
projectDir.to('src').mkdir()
|
|
299
|
+
projectDir.to('tests').mkdir()
|
|
300
|
+
projectDir.to('docs').mkdir()
|
|
301
|
+
|
|
302
|
+
// Create files
|
|
303
|
+
const packageJson = projectDir.to('package.json')
|
|
304
|
+
packageJson.writeJSON({
|
|
305
|
+
name: 'my-project',
|
|
306
|
+
version: '1.0.0'
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
const readme = projectDir.to('README.md')
|
|
310
|
+
readme.write('# My Project\n\nDescription here...')
|
|
311
|
+
|
|
312
|
+
// List all files
|
|
313
|
+
for (const file of projectDir.walk()) {
|
|
314
|
+
if (file.isFile) {
|
|
315
|
+
console.log(`File: ${file.name} (${file.sizeKB} KB)`)
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
### File Manipulation
|
|
321
|
+
|
|
322
|
+
```javascript
|
|
323
|
+
import { fileSync } from 'ktfile'
|
|
324
|
+
|
|
325
|
+
const sourceFile = fileSync('./data.json')
|
|
326
|
+
const backupFile = fileSync('./backup/data-backup.json')
|
|
327
|
+
|
|
328
|
+
// Create backup directory
|
|
329
|
+
backupFile.parent?.mkdirs()
|
|
330
|
+
|
|
331
|
+
// Copy file
|
|
332
|
+
sourceFile.copyTo(backupFile, true)
|
|
333
|
+
|
|
334
|
+
// Read and modify JSON
|
|
335
|
+
const data = sourceFile.readJSON()
|
|
336
|
+
data.lastBackup = new Date().toISOString()
|
|
337
|
+
sourceFile.writeJSON(data)
|
|
338
|
+
|
|
339
|
+
console.log(`Backup created: ${backupFile.size} bytes`)
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
### Async Operations
|
|
343
|
+
|
|
344
|
+
```javascript
|
|
345
|
+
import { fileAsync } from 'ktfile'
|
|
346
|
+
|
|
347
|
+
async function processFiles() {
|
|
348
|
+
const directory = fileAsync('./uploads')
|
|
349
|
+
const files = await directory.listFiles()
|
|
350
|
+
|
|
351
|
+
for (const file of files || []) {
|
|
352
|
+
if (file.extension === '.txt') {
|
|
353
|
+
const content = await file.read('utf8')
|
|
354
|
+
const processed = content.toUpperCase()
|
|
355
|
+
|
|
356
|
+
const outputFile = file.parent?.to('processed', file.name)
|
|
357
|
+
await outputFile?.parent?.mkdirs()
|
|
358
|
+
await outputFile?.write(processed)
|
|
359
|
+
|
|
360
|
+
console.log(`Processed: ${file.name}`)
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
processFiles().catch(console.error)
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
### Temporary Files
|
|
369
|
+
|
|
370
|
+
```javascript
|
|
371
|
+
import { FileSync, fileSync } from 'ktfile'
|
|
372
|
+
|
|
373
|
+
const tempDir = fileSync('./temp')
|
|
374
|
+
tempDir.mkdirs()
|
|
375
|
+
|
|
376
|
+
// Create temporary file
|
|
377
|
+
const tempFile = FileSync.createTempFile(tempDir, 'data-', '.json')
|
|
378
|
+
tempFile.writeJSON({ processing: true, timestamp: Date.now() })
|
|
379
|
+
|
|
380
|
+
// Use the temporary file
|
|
381
|
+
console.log(`Temp file created: ${tempFile.name}`)
|
|
382
|
+
|
|
383
|
+
// Clean up on exit
|
|
384
|
+
tempFile.deleteOnExit()
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
## Error Handling
|
|
388
|
+
|
|
389
|
+
Methods return `null` when operations fail, allowing for graceful error handling:
|
|
390
|
+
|
|
391
|
+
```javascript
|
|
392
|
+
const file = fileSync('./nonexistent.txt')
|
|
393
|
+
|
|
394
|
+
const content = file.read()
|
|
395
|
+
if (content === null) {
|
|
396
|
+
console.log('File does not exist or cannot be read')
|
|
397
|
+
} else {
|
|
398
|
+
console.log('File content:', content)
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// Check existence before operations
|
|
402
|
+
if (file.exists) {
|
|
403
|
+
file.delete()
|
|
404
|
+
}
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
## Cross-Platform Compatibility
|
|
408
|
+
|
|
409
|
+
KTFile handles platform differences automatically:
|
|
410
|
+
|
|
411
|
+
```javascript
|
|
412
|
+
const file = fileSync('./path/to/file.txt')
|
|
413
|
+
|
|
414
|
+
// Uses correct separator for platform
|
|
415
|
+
console.log(file.separator) // '/' on Unix, '\' on Windows
|
|
416
|
+
|
|
417
|
+
// Paths are normalized automatically
|
|
418
|
+
const nested = file.parent?.to('..', 'sibling', 'file.txt')
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
## License
|
|
422
|
+
|
|
423
|
+
MIT License
|
|
424
|
+
|
|
425
|
+
See the [LICENSE](LICENSE) file for details.
|
|
426
|
+
|
|
427
|
+
## Contributing
|
|
428
|
+
|
|
429
|
+
Contributions are welcome! Please read the [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to this project.
|
|
430
|
+
|
|
431
|
+
## Support
|
|
432
|
+
|
|
433
|
+
If you find this library useful, please consider starring the repository on GitHub and sharing it with others!
|