ktfile 1.1.9 → 1.2.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/README.md +120 -120
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,20 +24,20 @@ npm install ktfile
|
|
|
24
24
|
### Basic Usage
|
|
25
25
|
|
|
26
26
|
```javascript
|
|
27
|
-
import { fileSync, fileAsync, File } from 'ktfile'
|
|
27
|
+
import { fileSync, fileAsync, File } from 'ktfile'
|
|
28
28
|
|
|
29
29
|
// Synchronous API
|
|
30
|
-
const file = fileSync('/path/to/file.txt')
|
|
31
|
-
file.write('Hello, world!')
|
|
32
|
-
console.log(file.read())
|
|
30
|
+
const file = fileSync('/path/to/file.txt')
|
|
31
|
+
file.write('Hello, world!')
|
|
32
|
+
console.log(file.read()) // "Hello, world!"
|
|
33
33
|
|
|
34
34
|
// Asynchronous API
|
|
35
|
-
const asyncFile = fileAsync('/path/to/file.txt')
|
|
36
|
-
await asyncFile.write('Hello, async world!')
|
|
37
|
-
console.log(await asyncFile.read())
|
|
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
38
|
|
|
39
39
|
// Alternative syntax
|
|
40
|
-
const file2 = new File('/another/path')
|
|
40
|
+
const file2 = new File('/another/path')
|
|
41
41
|
```
|
|
42
42
|
|
|
43
43
|
### Node.js Setup
|
|
@@ -45,10 +45,10 @@ const file2 = new File('/another/path');
|
|
|
45
45
|
The library automatically initializes with Node.js `fs` module when available. For custom environments:
|
|
46
46
|
|
|
47
47
|
```javascript
|
|
48
|
-
import { initFS } from 'ktfile'
|
|
49
|
-
import fs from 'fs'
|
|
48
|
+
import { initFS } from 'ktfile'
|
|
49
|
+
import fs from 'fs'
|
|
50
50
|
|
|
51
|
-
initFS(fs)
|
|
51
|
+
initFS(fs)
|
|
52
52
|
```
|
|
53
53
|
|
|
54
54
|
## API Reference
|
|
@@ -86,89 +86,89 @@ initFS(fs);
|
|
|
86
86
|
##### Path Operations
|
|
87
87
|
```javascript
|
|
88
88
|
// Join paths
|
|
89
|
-
const newPath = file.to('subdirectory', 'file.txt')
|
|
89
|
+
const newPath = file.to('subdirectory', 'file.txt')
|
|
90
90
|
|
|
91
91
|
// Check if path contains another path
|
|
92
|
-
const contains = parentDir.contains(childFile)
|
|
92
|
+
const contains = parentDir.contains(childFile)
|
|
93
93
|
```
|
|
94
94
|
|
|
95
95
|
##### File Creation and Deletion
|
|
96
96
|
```javascript
|
|
97
97
|
// Create file
|
|
98
|
-
file.createFile()
|
|
98
|
+
file.createFile()
|
|
99
99
|
|
|
100
100
|
// Create temporary file
|
|
101
|
-
const tempFile = FileSync.createTempFile(directory, 'prefix', '.tmp')
|
|
101
|
+
const tempFile = FileSync.createTempFile(directory, 'prefix', '.tmp')
|
|
102
102
|
|
|
103
103
|
// Delete file or directory
|
|
104
|
-
file.delete(recursive = false, force = false)
|
|
104
|
+
file.delete(recursive = false, force = false)
|
|
105
105
|
|
|
106
106
|
// Schedule deletion on exit
|
|
107
|
-
file.deleteOnExit(recursive = false)
|
|
107
|
+
file.deleteOnExit(recursive = false)
|
|
108
108
|
|
|
109
109
|
// Clear directory contents
|
|
110
|
-
directory.clear(recursive = true)
|
|
110
|
+
directory.clear(recursive = true)
|
|
111
111
|
```
|
|
112
112
|
|
|
113
113
|
##### Directory Operations
|
|
114
114
|
```javascript
|
|
115
115
|
// Create directory
|
|
116
|
-
directory.mkdir(recursive = false)
|
|
116
|
+
directory.mkdir(recursive = false)
|
|
117
117
|
|
|
118
118
|
// Create directory tree
|
|
119
|
-
directory.mkdirs()
|
|
119
|
+
directory.mkdirs()
|
|
120
120
|
|
|
121
121
|
// List files in directory
|
|
122
|
-
const files = directory.listFiles()
|
|
123
|
-
const filenames = directory.listFilenames()
|
|
122
|
+
const files = directory.listFiles()
|
|
123
|
+
const filenames = directory.listFilenames()
|
|
124
124
|
|
|
125
125
|
// Walk directory tree
|
|
126
126
|
for (const file of directory.walk()) {
|
|
127
|
-
console.log(file.name)
|
|
127
|
+
console.log(file.name)
|
|
128
128
|
}
|
|
129
129
|
```
|
|
130
130
|
|
|
131
131
|
##### File Operations
|
|
132
132
|
```javascript
|
|
133
133
|
// Copy file
|
|
134
|
-
file.copyTo(destination, overwrite = false, recursive = false)
|
|
134
|
+
file.copyTo(destination, overwrite = false, recursive = false)
|
|
135
135
|
|
|
136
136
|
// Move/rename file
|
|
137
|
-
file.renameTo(newLocation, overwrite = false, recursive = false)
|
|
137
|
+
file.renameTo(newLocation, overwrite = false, recursive = false)
|
|
138
138
|
```
|
|
139
139
|
|
|
140
140
|
##### Reading Files
|
|
141
141
|
```javascript
|
|
142
142
|
// Read as string
|
|
143
|
-
const content = file.read('utf8')
|
|
143
|
+
const content = file.read('utf8')
|
|
144
144
|
|
|
145
145
|
// Read as Buffer
|
|
146
|
-
const buffer = file.read()
|
|
146
|
+
const buffer = file.read()
|
|
147
147
|
|
|
148
148
|
// Read lines as array
|
|
149
|
-
const lines = file.readLines('utf8')
|
|
149
|
+
const lines = file.readLines('utf8')
|
|
150
150
|
|
|
151
151
|
// Read JSON
|
|
152
|
-
const data = file.readJSON()
|
|
152
|
+
const data = file.readJSON()
|
|
153
153
|
|
|
154
154
|
// Read symbolic link target
|
|
155
|
-
const target = symlink.readlink()
|
|
155
|
+
const target = symlink.readlink()
|
|
156
156
|
```
|
|
157
157
|
|
|
158
158
|
##### Writing Files
|
|
159
159
|
```javascript
|
|
160
160
|
// Write string or Buffer
|
|
161
|
-
file.write('content', 'utf8')
|
|
162
|
-
file.write(buffer)
|
|
161
|
+
file.write('content', 'utf8')
|
|
162
|
+
file.write(buffer)
|
|
163
163
|
|
|
164
164
|
// Write with custom encoder
|
|
165
|
-
file.write(data, { write: () => data })
|
|
165
|
+
file.write(data, { write: () => data })
|
|
166
166
|
|
|
167
167
|
// Write JSON
|
|
168
|
-
file.writeJSON({ key: 'value' })
|
|
168
|
+
file.writeJSON({ key: 'value' })
|
|
169
169
|
|
|
170
170
|
// Append to file
|
|
171
|
-
file.append('more content', 'utf8')
|
|
171
|
+
file.append('more content', 'utf8')
|
|
172
172
|
```
|
|
173
173
|
|
|
174
174
|
### FileAsync Class
|
|
@@ -194,94 +194,94 @@ All methods that return metadata or perform operations are asynchronous and retu
|
|
|
194
194
|
##### Permission Methods
|
|
195
195
|
```javascript
|
|
196
196
|
// Check permissions
|
|
197
|
-
const canRead = await file.canRead()
|
|
198
|
-
const canWrite = await file.canWrite()
|
|
199
|
-
const canExecute = await file.canExecute()
|
|
197
|
+
const canRead = await file.canRead()
|
|
198
|
+
const canWrite = await file.canWrite()
|
|
199
|
+
const canExecute = await file.canExecute()
|
|
200
200
|
|
|
201
201
|
// Set permissions
|
|
202
|
-
await file.setReadable(true)
|
|
203
|
-
await file.setWritable(false)
|
|
204
|
-
await file.setExecutable(true)
|
|
202
|
+
await file.setReadable(true)
|
|
203
|
+
await file.setWritable(false)
|
|
204
|
+
await file.setExecutable(true)
|
|
205
205
|
```
|
|
206
206
|
|
|
207
207
|
##### Metadata Methods
|
|
208
208
|
```javascript
|
|
209
209
|
// Get timestamps
|
|
210
|
-
const created = await file.creationTime()
|
|
211
|
-
const modified = await file.lastModified()
|
|
212
|
-
const accessed = await file.lastAccess()
|
|
210
|
+
const created = await file.creationTime()
|
|
211
|
+
const modified = await file.lastModified()
|
|
212
|
+
const accessed = await file.lastAccess()
|
|
213
213
|
|
|
214
214
|
// Set timestamps
|
|
215
|
-
await file.setCreationTime(new Date())
|
|
216
|
-
await file.setLastModified(new Date())
|
|
215
|
+
await file.setCreationTime(new Date())
|
|
216
|
+
await file.setLastModified(new Date())
|
|
217
217
|
|
|
218
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()
|
|
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
224
|
|
|
225
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()
|
|
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
230
|
```
|
|
231
231
|
|
|
232
232
|
##### File Operations
|
|
233
233
|
```javascript
|
|
234
234
|
// Create and delete
|
|
235
|
-
await file.createFile()
|
|
236
|
-
await file.delete(recursive, force)
|
|
237
|
-
await directory.clear(recursive)
|
|
235
|
+
await file.createFile()
|
|
236
|
+
await file.delete(recursive, force)
|
|
237
|
+
await directory.clear(recursive)
|
|
238
238
|
|
|
239
239
|
// Copy and move
|
|
240
|
-
await file.copyTo(destination, overwrite, recursive)
|
|
241
|
-
await file.renameTo(newLocation, overwrite, recursive)
|
|
240
|
+
await file.copyTo(destination, overwrite, recursive)
|
|
241
|
+
await file.renameTo(newLocation, overwrite, recursive)
|
|
242
242
|
```
|
|
243
243
|
|
|
244
244
|
##### Directory Operations
|
|
245
245
|
```javascript
|
|
246
246
|
// Create directories
|
|
247
|
-
await directory.mkdir(recursive)
|
|
248
|
-
await directory.mkdirs()
|
|
247
|
+
await directory.mkdir(recursive)
|
|
248
|
+
await directory.mkdirs()
|
|
249
249
|
|
|
250
250
|
// List contents
|
|
251
|
-
const files = await directory.listFiles()
|
|
252
|
-
const filenames = await directory.listFilenames()
|
|
251
|
+
const files = await directory.listFiles()
|
|
252
|
+
const filenames = await directory.listFilenames()
|
|
253
253
|
|
|
254
254
|
// Walk directory tree (AsyncGenerator)
|
|
255
255
|
for await (const file of directory.walk()) {
|
|
256
|
-
console.log(file.name)
|
|
256
|
+
console.log(file.name)
|
|
257
257
|
}
|
|
258
258
|
```
|
|
259
259
|
|
|
260
260
|
##### Reading Files
|
|
261
261
|
```javascript
|
|
262
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()
|
|
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
267
|
|
|
268
268
|
// Read symbolic link
|
|
269
|
-
const target = await symlink.readlink()
|
|
269
|
+
const target = await symlink.readlink()
|
|
270
270
|
```
|
|
271
271
|
|
|
272
272
|
##### Writing Files
|
|
273
273
|
```javascript
|
|
274
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')
|
|
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
279
|
```
|
|
280
280
|
|
|
281
281
|
##### Static Methods
|
|
282
282
|
```javascript
|
|
283
283
|
// Create temporary file
|
|
284
|
-
const tempFile = FileAsync.createTempFile(directory, 'prefix', '.tmp')
|
|
284
|
+
const tempFile = FileAsync.createTempFile(directory, 'prefix', '.tmp')
|
|
285
285
|
```
|
|
286
286
|
|
|
287
287
|
## Examples
|
|
@@ -289,30 +289,30 @@ const tempFile = FileAsync.createTempFile(directory, 'prefix', '.tmp');
|
|
|
289
289
|
### Working with Directories
|
|
290
290
|
|
|
291
291
|
```javascript
|
|
292
|
-
import { fileSync } from 'ktfile'
|
|
292
|
+
import { fileSync } from 'ktfile'
|
|
293
293
|
|
|
294
|
-
const projectDir = fileSync('./my-project')
|
|
294
|
+
const projectDir = fileSync('./my-project')
|
|
295
295
|
|
|
296
296
|
// Create project structure
|
|
297
|
-
projectDir.mkdirs()
|
|
298
|
-
projectDir.to('src').mkdir()
|
|
299
|
-
projectDir.to('tests').mkdir()
|
|
300
|
-
projectDir.to('docs').mkdir()
|
|
297
|
+
projectDir.mkdirs()
|
|
298
|
+
projectDir.to('src').mkdir()
|
|
299
|
+
projectDir.to('tests').mkdir()
|
|
300
|
+
projectDir.to('docs').mkdir()
|
|
301
301
|
|
|
302
302
|
// Create files
|
|
303
|
-
const packageJson = projectDir.to('package.json')
|
|
303
|
+
const packageJson = projectDir.to('package.json')
|
|
304
304
|
packageJson.writeJSON({
|
|
305
305
|
name: 'my-project',
|
|
306
306
|
version: '1.0.0'
|
|
307
|
-
})
|
|
307
|
+
})
|
|
308
308
|
|
|
309
|
-
const readme = projectDir.to('README.md')
|
|
310
|
-
readme.write('# My Project\n\nDescription here...')
|
|
309
|
+
const readme = projectDir.to('README.md')
|
|
310
|
+
readme.write('# My Project\n\nDescription here...')
|
|
311
311
|
|
|
312
312
|
// List all files
|
|
313
313
|
for (const file of projectDir.walk()) {
|
|
314
314
|
if (file.isFile) {
|
|
315
|
-
console.log(`File: ${file.name} (${file.sizeKB} KB)`)
|
|
315
|
+
console.log(`File: ${file.name} (${file.sizeKB} KB)`)
|
|
316
316
|
}
|
|
317
317
|
}
|
|
318
318
|
```
|
|
@@ -320,68 +320,68 @@ for (const file of projectDir.walk()) {
|
|
|
320
320
|
### File Manipulation
|
|
321
321
|
|
|
322
322
|
```javascript
|
|
323
|
-
import { fileSync } from 'ktfile'
|
|
323
|
+
import { fileSync } from 'ktfile'
|
|
324
324
|
|
|
325
|
-
const sourceFile = fileSync('./data.json')
|
|
326
|
-
const backupFile = fileSync('./backup/data-backup.json')
|
|
325
|
+
const sourceFile = fileSync('./data.json')
|
|
326
|
+
const backupFile = fileSync('./backup/data-backup.json')
|
|
327
327
|
|
|
328
328
|
// Create backup directory
|
|
329
|
-
backupFile.parent?.mkdirs()
|
|
329
|
+
backupFile.parent?.mkdirs()
|
|
330
330
|
|
|
331
331
|
// Copy file
|
|
332
|
-
sourceFile.copyTo(backupFile, true)
|
|
332
|
+
sourceFile.copyTo(backupFile, true)
|
|
333
333
|
|
|
334
334
|
// Read and modify JSON
|
|
335
|
-
const data = sourceFile.readJSON()
|
|
336
|
-
data.lastBackup = new Date().toISOString()
|
|
337
|
-
sourceFile.writeJSON(data)
|
|
335
|
+
const data = sourceFile.readJSON()
|
|
336
|
+
data.lastBackup = new Date().toISOString()
|
|
337
|
+
sourceFile.writeJSON(data)
|
|
338
338
|
|
|
339
|
-
console.log(`Backup created: ${backupFile.size} bytes`)
|
|
339
|
+
console.log(`Backup created: ${backupFile.size} bytes`)
|
|
340
340
|
```
|
|
341
341
|
|
|
342
342
|
### Async Operations
|
|
343
343
|
|
|
344
344
|
```javascript
|
|
345
|
-
import { fileAsync } from 'ktfile'
|
|
345
|
+
import { fileAsync } from 'ktfile'
|
|
346
346
|
|
|
347
347
|
async function processFiles() {
|
|
348
|
-
const directory = fileAsync('./uploads')
|
|
349
|
-
const files = await directory.listFiles()
|
|
348
|
+
const directory = fileAsync('./uploads')
|
|
349
|
+
const files = await directory.listFiles()
|
|
350
350
|
|
|
351
351
|
for (const file of files || []) {
|
|
352
352
|
if (file.extension === '.txt') {
|
|
353
|
-
const content = await file.read('utf8')
|
|
354
|
-
const processed = content.toUpperCase()
|
|
353
|
+
const content = await file.read('utf8')
|
|
354
|
+
const processed = content.toUpperCase()
|
|
355
355
|
|
|
356
|
-
const outputFile = file.parent?.to('processed', file.name)
|
|
357
|
-
await outputFile?.parent?.mkdirs()
|
|
358
|
-
await outputFile?.write(processed)
|
|
356
|
+
const outputFile = file.parent?.to('processed', file.name)
|
|
357
|
+
await outputFile?.parent?.mkdirs()
|
|
358
|
+
await outputFile?.write(processed)
|
|
359
359
|
|
|
360
|
-
console.log(`Processed: ${file.name}`)
|
|
360
|
+
console.log(`Processed: ${file.name}`)
|
|
361
361
|
}
|
|
362
362
|
}
|
|
363
363
|
}
|
|
364
364
|
|
|
365
|
-
processFiles().catch(console.error)
|
|
365
|
+
processFiles().catch(console.error)
|
|
366
366
|
```
|
|
367
367
|
|
|
368
368
|
### Temporary Files
|
|
369
369
|
|
|
370
370
|
```javascript
|
|
371
|
-
import { FileSync, fileSync } from 'ktfile'
|
|
371
|
+
import { FileSync, fileSync } from 'ktfile'
|
|
372
372
|
|
|
373
|
-
const tempDir = fileSync('./temp')
|
|
374
|
-
tempDir.mkdirs()
|
|
373
|
+
const tempDir = fileSync('./temp')
|
|
374
|
+
tempDir.mkdirs()
|
|
375
375
|
|
|
376
376
|
// Create temporary file
|
|
377
|
-
const tempFile = FileSync.createTempFile(tempDir, 'data-', '.json')
|
|
378
|
-
tempFile.writeJSON({ processing: true, timestamp: Date.now() })
|
|
377
|
+
const tempFile = FileSync.createTempFile(tempDir, 'data-', '.json')
|
|
378
|
+
tempFile.writeJSON({ processing: true, timestamp: Date.now() })
|
|
379
379
|
|
|
380
380
|
// Use the temporary file
|
|
381
|
-
console.log(`Temp file created: ${tempFile.name}`)
|
|
381
|
+
console.log(`Temp file created: ${tempFile.name}`)
|
|
382
382
|
|
|
383
383
|
// Clean up on exit
|
|
384
|
-
tempFile.deleteOnExit()
|
|
384
|
+
tempFile.deleteOnExit()
|
|
385
385
|
```
|
|
386
386
|
|
|
387
387
|
## Error Handling
|
|
@@ -389,18 +389,18 @@ tempFile.deleteOnExit();
|
|
|
389
389
|
Methods return `null` when operations fail, allowing for graceful error handling:
|
|
390
390
|
|
|
391
391
|
```javascript
|
|
392
|
-
const file = fileSync('./nonexistent.txt')
|
|
392
|
+
const file = fileSync('./nonexistent.txt')
|
|
393
393
|
|
|
394
|
-
const content = file.read()
|
|
394
|
+
const content = file.read()
|
|
395
395
|
if (content === null) {
|
|
396
|
-
console.log('File does not exist or cannot be read')
|
|
396
|
+
console.log('File does not exist or cannot be read')
|
|
397
397
|
} else {
|
|
398
|
-
console.log('File content:', content)
|
|
398
|
+
console.log('File content:', content)
|
|
399
399
|
}
|
|
400
400
|
|
|
401
401
|
// Check existence before operations
|
|
402
402
|
if (file.exists) {
|
|
403
|
-
file.delete()
|
|
403
|
+
file.delete()
|
|
404
404
|
}
|
|
405
405
|
```
|
|
406
406
|
|
|
@@ -409,13 +409,13 @@ if (file.exists) {
|
|
|
409
409
|
KTFile handles platform differences automatically:
|
|
410
410
|
|
|
411
411
|
```javascript
|
|
412
|
-
const file = fileSync('./path/to/file.txt')
|
|
412
|
+
const file = fileSync('./path/to/file.txt')
|
|
413
413
|
|
|
414
414
|
// Uses correct separator for platform
|
|
415
|
-
console.log(file.separator)
|
|
415
|
+
console.log(file.separator) // '/' on Unix, '\' on Windows
|
|
416
416
|
|
|
417
417
|
// Paths are normalized automatically
|
|
418
|
-
const nested = file.parent?.to('..', 'sibling', 'file.txt')
|
|
418
|
+
const nested = file.parent?.to('..', 'sibling', 'file.txt')
|
|
419
419
|
```
|
|
420
420
|
|
|
421
421
|
## License
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ktfile",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "A powerful, cross-platform file system library for JavaScript/TypeScript that provides both synchronous and asynchronous APIs with a clean, object-oriented interface.",
|
|
5
5
|
"main": "index.min.js",
|
|
6
6
|
"types": "types/ktfile.d.ts",
|