ktfile 1.1.8 → 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/CONTRIBUTING.md +0 -0
- package/LICENSE +0 -0
- package/README.md +120 -120
- package/index.min.js +1 -1
- package/package.json +1 -1
- 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 +1 -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/CONTRIBUTING.md
CHANGED
|
File without changes
|
package/LICENSE
CHANGED
|
File without changes
|
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/index.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let t=[];function s(s,e=t){"/"===s[0]||"\\"===s[0]?(e=[],s=s.slice(1)):/^[a-zA-Z]:\\/.test(s)?(e=[s.slice(0,2)],s=s.slice(3)):e=[...e];const i=s.split(/[\\/]/g);for(const t of i)".."===t?e.pop():"."!==t&&""!==t&&e.push(t);return e}"undefined"!=typeof process&&"cwd"in process&&"function"==typeof process.cwd&&(t=s(process.cwd()));const e=new Map;function i(t,s,e){const i=t.listeners(s);t.removeAllListeners(s);for(const n of i)n!==e&&t.on(s,n);t.on(s,e)}function n(){for(const[t,s]of e.entries())new x(t).delete(s);e.clear(),"undefined"!=typeof process?(process.off("SIGINT",r),process.off("SIGTERM",l),process.off("exit",n)):"undefined"!=typeof window&&(window.removeEventListener("beforeunload",n),window.removeEventListener("pagehide",n))}function r(){n(),process.exit(130)}function l(){n(),process.exit(143)}function a(){"undefined"!=typeof process?(i(process,"exit",n),i(process,"SIGINT",r),i(process,"SIGTERM",l)):"undefined"!=typeof window&&(window.addEventListener("beforeunload",n),window.addEventListener("pagehide",n))}function h(t){return t.includes("/")||t.includes("\\")?"."!==t[0]||"/"!==t[1]&&"\\"!==t[1]?"absolute":"relative":"name"}function u(t){return!/[<>:"/\\|?*\x00-\x1F]/.test(t)&&!/^\.\.?$/.test(t)&&t.length>0&&t.length<=255}function c(t){return s(t,[]).every(t=>/^\.\.?$/.test(t)||u(t))}function f(t){if(t<1024)return[t,"B"];const s=["KB","MB","GB","TB","PB","EB","ZB","YB"];let e=-1;do{t/=1024,e++}while(t>=1024&&e<s.length-1);return[t,s[e]]}function o(t,s=2){const[e,i]=f(t);return e.toFixed(s)+" "+i}class d{split;constructor(t){this.split="string"==typeof t?s(t):[...t]}get fullPath(){return 0===this.split.length?this.separator:this.split.join(this.separator)}createWriteStream(t){if("object"==typeof this.fs&&"createWriteStream"in this.fs&&"function"==typeof this.fs.createWriteStream)return this.fs.createWriteStream(this.split.join("/"),t);throw new Error("File system does not support createWriteStream")}createReadStream(t){if("object"==typeof this.fs&&"createReadStream"in this.fs&&"function"==typeof this.fs.createReadStream)return this.fs.createReadStream(this.split.join("/"),t);throw new Error("File system does not support createReadStream")}createAppendStream(t){if("object"==typeof this.fs&&"createWriteStream"in this.fs&&"function"==typeof this.fs.createWriteStream)return this.fs.createWriteStream(this.split.join("/"),{flags:"a",encoding:t});throw new Error("File system does not support createAppendStream")}createInputStream(t){if("object"==typeof this.fs&&"createReadStream"in this.fs&&"function"==typeof this.fs.createReadStream)return this.fs.createReadStream(this.split.join("/"),{flags:"r",encoding:t});throw new Error("File system does not support createInputStream")}async download(t,s,e){const i=this.createWriteStream();try{const n=await fetch(t,{method:s?.method||"GET",headers:s?.headers,body:s?.body});if(!n.ok)return await i.close(),new Error(`Request Failed. Status Code: ${n.status}`);const r=parseInt(n.headers.get("content-length")||"0",10);e&&e(0,r);let l=0;if(!n.body)return await i.close(),new Error("No response body");for await(const t of n.body)i.write(t),l+=t.length,e&&e(l,r);return await i.close(),null}catch(t){return await i.close(),t instanceof Error?t:new Error(String(t))}}}class p{value;constructor(t){this.value=t}get(t,s=void 0){const e=t.split(".");let i=this.value;for(const t of e){if(void 0===i[t])return s;i=i[t]}return i}set(t,s){const e=t.split(".");let i=this.value;for(let t=0;t<e.length;t++){const n=e[t];t===e.length-1?i[n]=s:(void 0===i[n]&&(i[n]={}),i=i[n])}}}class y extends p{file;constructor(t,s={}){super(s),this.file=t}async init(){return await this.file.exists()&&(this.value=await this.file.readJSON()),this}async save(){return null!==await this.file.writeJSON(this.value)}}async function w(t){try{return t().then(()=>!0).catch(()=>!1)}catch(t){return!1}}async function m(t){try{return t().then(t=>t).catch(()=>null)}catch(t){return null}}class S extends d{static fs;static sep="/";static createTempFile(t,s="ktfile-temp",e=".tmp"){if(!("mkdTemp"in S.fs))throw new Error("mkdTemp is not available in the current FS.");const i=t?t.fullPath:".",n=this.fs.mkdtemp(`${i}/${s}-`);return new S(`${n}${e}`)}get fs(){return S.fs}canExecute(){return w(()=>this.fs.access(this.fullPath,this.fs?.constants?.X_OK??1))}async setExecutable(t=!0){return await w(()=>this.fs.chmod(this.fullPath,t?493:420))?this:null}canRead(){return w(()=>this.fs.access(this.fullPath,this.fs?.constants?.R_OK??4))}async setReadable(t=!0){return await w(()=>this.fs.chmod(this.fullPath,t?420:0))?this:null}canWrite(){return w(()=>this.fs.access(this.fullPath,this.fs?.constants?.W_OK??2))}async setWritable(t=!0){return await w(()=>this.fs.chmod(this.fullPath,t?420:292))?this:null}creationTime(){return m(()=>this.fs.stat(this.fullPath).then(t=>t.birthtime).catch(()=>null))}async setCreationTime(t){return await w(()=>this.fs.utimes(this.fullPath,t,t))?this:null}async lastModified(){return await m(()=>this.fs.stat(this.fullPath).then(t=>t.mtime).catch(()=>null))}async setLastModified(t){return await w(()=>this.fs.utimes(this.fullPath,t,t))?this:null}async exists(){return await m(()=>this.fs.exists(this.fullPath))??await w(()=>this.fs.access(this.fullPath))}lastAccess(){return m(()=>this.fs.stat(this.fullPath).then(t=>t.atime).catch(()=>null))}get name(){return this.fullPath.split(this.separator).pop()||""}get nameWithoutExtension(){const t=this.name,s=t.lastIndexOf(".");return-1===s?t:t.substring(0,s)}get extension(){const t=this.name,s=t.lastIndexOf(".");return-1===s?"":t.substring(s+1)}get parent(){return 0===this.split.length?null:new S(this.split.slice(0,-1))}get uri(){return`file://${this.fullPath}`}get separator(){return S.sep}isDirectory(){return m(()=>this.fs.stat(this.fullPath).then(t=>t.isDirectory()).catch(()=>null))}isFile(){return m(()=>this.fs.stat(this.fullPath).then(t=>t.isFile()).catch(()=>null))}async isEmpty(){const t=await this.isDirectory();return null===t?null:t?0===((await this.listFiles())?.length??0):0===await this.size()}get isHidden(){return this.name.startsWith(".")||this.fullPath.split(this.separator).some(t=>t.startsWith("."))}isSymbolicLink(){return m(()=>this.fs.lstat(this.fullPath).then(t=>t.isSymbolicLink()).catch(()=>null))}async size(){return m(async()=>{const t=await this.fs.stat(this.fullPath).then(t=>t).catch(()=>null);if(!t)return null;if(t.isFile())return t.size;if(t.isDirectory()){const t=await this.listFiles();if(null===t)return null;let s=0;for(const e of t){const t=await e.size();null!==t&&(s+=t)}return s}})}async sizeKB(){const t=await this.size();return null!==t?t/1024:null}async sizeMB(){const t=await this.size();return null!==t?t/1048576:null}async sizeGB(){const t=await this.size();return null!==t?t/1073741824:null}to(...t){return new S(this.fullPath+"/"+t.join("/"))}contains(t){if(t.split.length<=this.split.length)return!1;for(let s=0;s<this.split.length;s++)if(this.split[s]!==t.split[s])return!1;return!0}async createFile(t="",s){return await this.exists()?await this.isFile()?this:null:await this.write(t,s)?this:null}async delete(t,s=t){if(t){if("rmSync"in this.fs)return await w(()=>this.fs.rm(this.fullPath,{recursive:!0,force:s}))?this:null;if("rmdirSync"in this.fs)return await w(()=>this.fs.rmdir(this.fullPath,{recursive:!0}))?this:null;let t=!1;for(const s of await this.listFiles()||[])await s.delete(!0)||(t=!0);if(t)return null}return await w(()=>this.fs.unlink(this.fullPath))?this:null}deleteOnExit(t){e.set(this.fullPath,t),a()}async clear(t){if(t){const t=await this.listFiles();if(null===t)return null;let s=!1;for(const e of t||[])await e.delete(!0)||(s=!0);return s?null:this}return await this.write(""),null}async listFiles(){if(!this.isDirectory)return null;const t=await m(()=>this.fs.readdir(this.fullPath));return t?t.map(t=>new S(`${this.fullPath}/${t}`)):null}listFilenames(){return m(()=>this.fs.readdir(this.fullPath))}async mkdir(t=!1){return await w(()=>this.fs.mkdir(this.fullPath,{recursive:t}))?this:null}async mkdirs(){return await this.mkdir(!0)?this:null}async renameTo(t,s,e){return this.fullPath===t.fullPath?this:!t.exists||s||await t.delete(e)?(e&&await(this.parent?.mkdirs()),await w(()=>this.fs.rename(this.fullPath,t.fullPath))?this:null):null}async copyTo(t,s,e){if(this.fullPath===t.fullPath)return this;if(!this.exists)return null;if(t.exists&&!s&&!await t.delete(e))return null;if(this.isFile){const s=await this.read();return null===s?null:await t.write(s)?this:null}if(!await t.mkdir(e))return null;const i=await this.listFiles();if(null===i)return null;let n=!1;for(const r of i){const i=t.to(r.name);await r.copyTo(i,s,e)||(n=!0)}return n?null:this}async*walk(){if(this.isDirectory){yield this;for(const t of await this.listFiles()||[])yield*t.walk()}else yield this}read(t){return m(()=>this.fs.readFile(this.fullPath,t))}async readLines(t="utf8"){const s=await this.read(t);return"string"==typeof s?s.split(/\r?\n/):null}async readJSON(){const t=await this.read("utf8");if("string"!=typeof t)return null;try{return JSON.parse(t)}catch(t){return null}}async readlink(){if(!this.isSymbolicLink)return null;const t=await m(()=>this.fs.readlink(this.fullPath));return t?new S(t):null}async write(t,s){return null!==s&&"object"==typeof s&&"write"in s&&"function"==typeof s.write&&(t=s.write()),await w(()=>this.fs.writeFile(this.fullPath,t,s))?this:null}async writeJSON(t,s=2){return await w(()=>this.write(JSON.stringify(t,null,s)))?this:null}async append(t,s){return await w(()=>this.fs.appendFile(this.fullPath,t,s))?this:null}get sync(){return new x(this.split)}async configJSON(){return await new y(this).init()}}class P extends p{file;constructor(t,s={}){super(s),this.file=t,this.file.exists&&(this.value=this.file.readJSON())}save(){return null!==this.file.writeJSON(this.value)}}function g(t){try{return t(),!0}catch(t){return!1}}function F(t){try{return t()}catch(t){return null}}class x extends d{static fs;static sep="/";static createTempFile(t,s="ktfile-temp",e=".tmp"){if(!("mkdTempSync"in x.fs))throw new Error("mkdTempSync is not available in the current FS.");const i=t?t.fullPath:".",n=this.fs.mkdtempSync(`${i}/${s}-`);return new x(`${n}${e}`)}get fs(){return x.fs}get canExecute(){return g(()=>this.fs.accessSync(this.fullPath,this.fs?.constants?.X_OK??1))}set canExecute(t){g(()=>this.fs.chmodSync(this.fullPath,t?493:420))}get canRead(){return g(()=>this.fs.accessSync(this.fullPath,this.fs?.constants?.R_OK??4))}set canRead(t){g(()=>this.fs.chmodSync(this.fullPath,t?420:0))}get canWrite(){return g(()=>this.fs.accessSync(this.fullPath,this.fs?.constants?.W_OK??2))}set canWrite(t){g(()=>this.fs.chmodSync(this.fullPath,t?420:292))}get creationTime(){return F(()=>this.fs.statSync(this.fullPath).birthtime)}set creationTime(t){g(()=>this.fs.utimesSync(this.fullPath,t,t))}get lastModified(){return F(()=>this.fs.statSync(this.fullPath).mtime)}set lastModified(t){g(()=>this.fs.utimesSync(this.fullPath,t,t))}get exists(){return F(()=>this.fs.existsSync(this.fullPath))??g(()=>this.fs.accessSync(this.fullPath))}get lastAccess(){return F(()=>this.fs.statSync(this.fullPath).atime)}get name(){return this.fullPath.split(this.separator).pop()||""}get nameWithoutExtension(){const t=this.name,s=t.lastIndexOf(".");return-1===s?t:t.substring(0,s)}get extension(){const t=this.name,s=t.lastIndexOf(".");return-1===s?"":t.substring(s+1)}get parent(){return 0===this.split.length?null:new x(this.split.slice(0,-1))}get uri(){return`file://${this.fullPath}`}get separator(){return x.sep}get isDirectory(){return F(()=>this.fs.statSync(this.fullPath).isDirectory())}get isFile(){return F(()=>this.fs.statSync(this.fullPath).isFile())}get isEmpty(){const t=this.isDirectory;return null===t?null:t?0===(this.listFiles()?.length??0):0===this.size}get isHidden(){return this.name.startsWith(".")||this.fullPath.split(this.separator).some(t=>t.startsWith("."))}get isSymbolicLink(){return F(()=>this.fs.lstatSync(this.fullPath).isSymbolicLink())}get size(){return F(()=>{const t=this.fs.statSync(this.fullPath);return t.isFile()?t.size:this.listFiles().reduce((t,s)=>t+(s.size??0),0)})}get sizeKB(){const t=this.size;return null!==t?t/1024:null}get sizeMB(){const t=this.size;return null!==t?t/1048576:null}get sizeGB(){const t=this.size;return null!==t?t/1073741824:null}to(...t){return new x(this.fullPath+"/"+t.join("/"))}contains(t){if(t.split.length<=this.split.length)return!1;for(let s=0;s<this.split.length;s++)if(this.split[s]!==t.split[s])return!1;return!0}createFile(t="",s){return this.exists?this.isFile?this:null:this.write(t,s)?this:null}delete(t,s=t){if(t){if("rmSync"in this.fs)return g(()=>this.fs.rmSync(this.fullPath,{recursive:!0,force:s}))?this:null;if("rmdirSync"in this.fs)return g(()=>this.fs.rmdirSync(this.fullPath,{recursive:!0}))?this:null;let t=!1;for(const s of this.listFiles()||[])s.delete(!0)||(t=!0);if(t)return null}return g(()=>this.fs.unlinkSync(this.fullPath))?this:null}deleteOnExit(t){e.set(this.fullPath,t),a()}clear(t){if(t){const t=this.listFiles();if(null===t)return null;let s=!1;for(const e of t||[])e.delete(!0)||(s=!0);return s?null:this}return this.write(""),null}listFiles(){if(!this.isDirectory)return null;const t=F(()=>this.fs.readdirSync(this.fullPath));return t?t.map(t=>new x(`${this.fullPath}/${t}`)):null}listFilenames(){return F(()=>this.fs.readdirSync(this.fullPath))}mkdir(t=!1){return g(()=>this.fs.mkdirSync(this.fullPath,{recursive:t}))?this:null}mkdirs(){return this.mkdir(!0)?this:null}renameTo(t,s,e){return this.fullPath===t.fullPath?this:!t.exists||s||t.delete(e)?(e&&this.parent?.mkdirs(),g(()=>this.fs.renameSync(this.fullPath,t.fullPath))?this:null):null}copyTo(t,s,e){if(this.fullPath===t.fullPath)return this;if(!this.exists)return null;if(t.exists&&!s&&!t.delete(e))return null;if(this.isFile){const s=this.read();return null===s?null:t.write(s)?this:null}if(!t.mkdir(e))return null;const i=this.listFiles();if(null===i)return null;let n=!1;for(const r of i){const i=t.to(r.name);r.copyTo(i,s,e)||(n=!0)}return n?null:this}*walk(){if(this.isDirectory){yield this;for(const t of this.listFiles()||[])yield*t.walk()}else yield this}read(t){return F(()=>this.fs.readFileSync(this.fullPath,t))}readLines(t="utf8"){const s=this.read(t);return"string"==typeof s?s.split(/\r?\n/):null}readJSON(){const t=this.read("utf8");if("string"!=typeof t)return null;try{return JSON.parse(t)}catch(t){return null}}readlink(){if(!this.isSymbolicLink)return null;const t=F(()=>this.fs.readlinkSync(this.fullPath));return t?new x(t):null}write(t,s){return null!==s&&"object"==typeof s&&"write"in s&&"function"==typeof s.write&&(t=s.write()),g(()=>this.fs.writeFileSync(this.fullPath,t,s))?this:null}writeJSON(t,s=2){return g(()=>this.write(JSON.stringify(t,null,s)))?this:null}append(t,s){return g(()=>this.fs.appendFileSync(this.fullPath,t,s))?this:null}get async(){return new S(this.split)}get configJSON(){return new P(this)}}function k(t){x.fs=t,S.fs=t.promises}function b(t){return new x(t)}function v(t){return new S(t)}if("undefined"!=typeof process)try{k(await import("fs")),x.sep=S.sep="win32"===process.platform?"\\":"/"}catch{}export{x as File,S as FileAsync,x as FileSync,a as attachCleanup,n as cleanup,e as deleteQueue,v as fileAsync,b as fileSync,o as formatSize,h as getPathType,f as getSizeFormat,k as initFS,u as isValidFilename,c as isValidPath,s as splitPath};
|
|
1
|
+
let t=[];function s(s,e=t){"/"===s[0]||"\\"===s[0]?(e=[],s=s.slice(1)):/^[a-zA-Z]:\\/.test(s)?(e=[s.slice(0,2)],s=s.slice(3)):e=[...e];const i=s.split(/[\\/]/g);for(const t of i)".."===t?e.pop():"."!==t&&""!==t&&e.push(t);return e}"undefined"!=typeof process&&"cwd"in process&&"function"==typeof process.cwd&&(t=s(process.cwd()));const e=new Map;function i(t,s,e){const i=t.listeners(s);t.removeAllListeners(s);for(const n of i)n!==e&&t.on(s,n);t.on(s,e)}function n(){for(const[t,s]of e.entries())new x(t).delete(s);e.clear(),"undefined"!=typeof process?(process.off("SIGINT",r),process.off("SIGTERM",l),process.off("exit",n)):"undefined"!=typeof window&&(window.removeEventListener("beforeunload",n),window.removeEventListener("pagehide",n))}function r(){n(),process.exit(130)}function l(){n(),process.exit(143)}function a(){"undefined"!=typeof process?(i(process,"exit",n),i(process,"SIGINT",r),i(process,"SIGTERM",l)):"undefined"!=typeof window&&(window.addEventListener("beforeunload",n),window.addEventListener("pagehide",n))}function h(t){return t.includes("/")||t.includes("\\")?"."!==t[0]||"/"!==t[1]&&"\\"!==t[1]?"absolute":"relative":"name"}function u(t){return!/[<>:"/\\|?*\x00-\x1F]/.test(t)&&!/^\.\.?$/.test(t)&&t.length>0&&t.length<=255}function c(t){return s(t,[]).every(t=>/^\.\.?$/.test(t)||u(t))}function f(t){if(t<1024)return[t,"B"];const s=["KB","MB","GB","TB","PB","EB","ZB","YB"];let e=-1;do{t/=1024,e++}while(t>=1024&&e<s.length-1);return[t,s[e]]}function o(t,s=2){const[e,i]=f(t);return e.toFixed(s)+" "+i}class d{split;constructor(t){this.split="string"==typeof t?s(t):[...t]}get fullPath(){return(E?this.separator:"")+this.split.join(this.separator)}createWriteStream(t){if("object"==typeof this.fs&&"createWriteStream"in this.fs&&"function"==typeof this.fs.createWriteStream)return this.fs.createWriteStream(this.split.join("/"),t);throw new Error("File system does not support createWriteStream")}createReadStream(t){if("object"==typeof this.fs&&"createReadStream"in this.fs&&"function"==typeof this.fs.createReadStream)return this.fs.createReadStream(this.split.join("/"),t);throw new Error("File system does not support createReadStream")}createAppendStream(t){if("object"==typeof this.fs&&"createWriteStream"in this.fs&&"function"==typeof this.fs.createWriteStream)return this.fs.createWriteStream(this.split.join("/"),{flags:"a",encoding:t});throw new Error("File system does not support createAppendStream")}createInputStream(t){if("object"==typeof this.fs&&"createReadStream"in this.fs&&"function"==typeof this.fs.createReadStream)return this.fs.createReadStream(this.split.join("/"),{flags:"r",encoding:t});throw new Error("File system does not support createInputStream")}async download(t,s,e){const i=this.createWriteStream();try{const n=await fetch(t,{method:s?.method||"GET",headers:s?.headers,body:s?.body});if(!n.ok)return await i.close(),new Error(`Request Failed. Status Code: ${n.status}`);const r=parseInt(n.headers.get("content-length")||"0",10);e&&e(0,r);let l=0;if(!n.body)return await i.close(),new Error("No response body");for await(const t of n.body)i.write(t),l+=t.length,e&&e(l,r);return await i.close(),null}catch(t){return await i.close(),t instanceof Error?t:new Error(String(t))}}}class p{value;constructor(t){this.value=t}get(t,s=void 0){const e=t.split(".");let i=this.value;for(const t of e){if(void 0===i[t])return s;i=i[t]}return i}set(t,s){const e=t.split(".");let i=this.value;for(let t=0;t<e.length;t++){const n=e[t];t===e.length-1?i[n]=s:(void 0===i[n]&&(i[n]={}),i=i[n])}}}class y extends p{file;constructor(t,s={}){super(s),this.file=t}async init(){return await this.file.exists()&&(this.value=await this.file.readJSON()),this}async save(){return null!==await this.file.writeJSON(this.value)}}async function w(t){try{return t().then(()=>!0).catch(()=>!1)}catch(t){return!1}}async function m(t){try{return t().then(t=>t).catch(()=>null)}catch(t){return null}}class S extends d{static fs;static sep="/";static createTempFile(t,s="ktfile-temp",e=".tmp"){if(!("mkdTemp"in S.fs))throw new Error("mkdTemp is not available in the current FS.");const i=t?t.fullPath:".",n=this.fs.mkdtemp(`${i}/${s}-`);return new S(`${n}${e}`)}get fs(){return S.fs}canExecute(){return w(()=>this.fs.access(this.fullPath,this.fs?.constants?.X_OK??1))}async setExecutable(t=!0){return await w(()=>this.fs.chmod(this.fullPath,t?493:420))?this:null}canRead(){return w(()=>this.fs.access(this.fullPath,this.fs?.constants?.R_OK??4))}async setReadable(t=!0){return await w(()=>this.fs.chmod(this.fullPath,t?420:0))?this:null}canWrite(){return w(()=>this.fs.access(this.fullPath,this.fs?.constants?.W_OK??2))}async setWritable(t=!0){return await w(()=>this.fs.chmod(this.fullPath,t?420:292))?this:null}creationTime(){return m(()=>this.fs.stat(this.fullPath).then(t=>t.birthtime).catch(()=>null))}async setCreationTime(t){return await w(()=>this.fs.utimes(this.fullPath,t,t))?this:null}async lastModified(){return await m(()=>this.fs.stat(this.fullPath).then(t=>t.mtime).catch(()=>null))}async setLastModified(t){return await w(()=>this.fs.utimes(this.fullPath,t,t))?this:null}async exists(){return await m(()=>this.fs.exists(this.fullPath))??await w(()=>this.fs.access(this.fullPath))}lastAccess(){return m(()=>this.fs.stat(this.fullPath).then(t=>t.atime).catch(()=>null))}get name(){return this.fullPath.split(this.separator).pop()||""}get nameWithoutExtension(){const t=this.name,s=t.lastIndexOf(".");return-1===s?t:t.substring(0,s)}get extension(){const t=this.name,s=t.lastIndexOf(".");return-1===s?"":t.substring(s+1)}get parent(){return 0===this.split.length?null:new S(this.split.slice(0,-1))}get uri(){return`file://${this.fullPath}`}get separator(){return S.sep}isDirectory(){return m(()=>this.fs.stat(this.fullPath).then(t=>t.isDirectory()).catch(()=>null))}isFile(){return m(()=>this.fs.stat(this.fullPath).then(t=>t.isFile()).catch(()=>null))}async isEmpty(){const t=await this.isDirectory();return null===t?null:t?0===((await this.listFiles())?.length??0):0===await this.size()}get isHidden(){return this.name.startsWith(".")||this.fullPath.split(this.separator).some(t=>t.startsWith("."))}isSymbolicLink(){return m(()=>this.fs.lstat(this.fullPath).then(t=>t.isSymbolicLink()).catch(()=>null))}async size(){return m(async()=>{const t=await this.fs.stat(this.fullPath).then(t=>t).catch(()=>null);if(!t)return null;if(t.isFile())return t.size;if(t.isDirectory()){const t=await this.listFiles();if(null===t)return null;let s=0;for(const e of t){const t=await e.size();null!==t&&(s+=t)}return s}})}async sizeKB(){const t=await this.size();return null!==t?t/1024:null}async sizeMB(){const t=await this.size();return null!==t?t/1048576:null}async sizeGB(){const t=await this.size();return null!==t?t/1073741824:null}to(...t){return new S(this.fullPath+"/"+t.join("/"))}contains(t){if(t.split.length<=this.split.length)return!1;for(let s=0;s<this.split.length;s++)if(this.split[s]!==t.split[s])return!1;return!0}async createFile(t="",s){return await this.exists()?await this.isFile()?this:null:await this.write(t,s)?this:null}async delete(t,s=t){if(t){if("rmSync"in this.fs)return await w(()=>this.fs.rm(this.fullPath,{recursive:!0,force:s}))?this:null;if("rmdirSync"in this.fs)return await w(()=>this.fs.rmdir(this.fullPath,{recursive:!0}))?this:null;let t=!1;for(const s of await this.listFiles()||[])await s.delete(!0)||(t=!0);if(t)return null}return await w(()=>this.fs.unlink(this.fullPath))?this:null}deleteOnExit(t){e.set(this.fullPath,t),a()}async clear(t){if(t){const t=await this.listFiles();if(null===t)return null;let s=!1;for(const e of t||[])await e.delete(!0)||(s=!0);return s?null:this}return await this.write(""),null}async listFiles(){if(!this.isDirectory)return null;const t=await m(()=>this.fs.readdir(this.fullPath));return t?t.map(t=>new S(`${this.fullPath}/${t}`)):null}listFilenames(){return m(()=>this.fs.readdir(this.fullPath))}async mkdir(t=!1){return await w(()=>this.fs.mkdir(this.fullPath,{recursive:t}))?this:null}async mkdirs(){return await this.mkdir(!0)?this:null}async renameTo(t,s,e){return this.fullPath===t.fullPath?this:!t.exists||s||await t.delete(e)?(e&&await(this.parent?.mkdirs()),await w(()=>this.fs.rename(this.fullPath,t.fullPath))?this:null):null}async copyTo(t,s,e){if(this.fullPath===t.fullPath)return this;if(!this.exists)return null;if(t.exists&&!s&&!await t.delete(e))return null;if(this.isFile){const s=await this.read();return null===s?null:await t.write(s)?this:null}if(!await t.mkdir(e))return null;const i=await this.listFiles();if(null===i)return null;let n=!1;for(const r of i){const i=t.to(r.name);await r.copyTo(i,s,e)||(n=!0)}return n?null:this}async*walk(){if(this.isDirectory){yield this;for(const t of await this.listFiles()||[])yield*t.walk()}else yield this}read(t){return m(()=>this.fs.readFile(this.fullPath,t))}async readLines(t="utf8"){const s=await this.read(t);return"string"==typeof s?s.split(/\r?\n/):null}async readJSON(){const t=await this.read("utf8");if("string"!=typeof t)return null;try{return JSON.parse(t)}catch(t){return null}}async readlink(){if(!this.isSymbolicLink)return null;const t=await m(()=>this.fs.readlink(this.fullPath));return t?new S(t):null}async write(t,s){return null!==s&&"object"==typeof s&&"write"in s&&"function"==typeof s.write&&(t=s.write()),await w(()=>this.fs.writeFile(this.fullPath,t,s))?this:null}async writeJSON(t,s=2){return await w(()=>this.write(JSON.stringify(t,null,s)))?this:null}async append(t,s){return await w(()=>this.fs.appendFile(this.fullPath,t,s))?this:null}get sync(){return new x(this.split)}async configJSON(){return await new y(this).init()}}class P extends p{file;constructor(t,s={}){super(s),this.file=t,this.file.exists&&(this.value=this.file.readJSON())}save(){return null!==this.file.writeJSON(this.value)}}function g(t){try{return t(),!0}catch(t){return!1}}function F(t){try{return t()}catch(t){return null}}class x extends d{static fs;static sep="/";static createTempFile(t,s="ktfile-temp",e=".tmp"){if(!("mkdTempSync"in x.fs))throw new Error("mkdTempSync is not available in the current FS.");const i=t?t.fullPath:".",n=this.fs.mkdtempSync(`${i}/${s}-`);return new x(`${n}${e}`)}get fs(){return x.fs}get canExecute(){return g(()=>this.fs.accessSync(this.fullPath,this.fs?.constants?.X_OK??1))}set canExecute(t){g(()=>this.fs.chmodSync(this.fullPath,t?493:420))}get canRead(){return g(()=>this.fs.accessSync(this.fullPath,this.fs?.constants?.R_OK??4))}set canRead(t){g(()=>this.fs.chmodSync(this.fullPath,t?420:0))}get canWrite(){return g(()=>this.fs.accessSync(this.fullPath,this.fs?.constants?.W_OK??2))}set canWrite(t){g(()=>this.fs.chmodSync(this.fullPath,t?420:292))}get creationTime(){return F(()=>this.fs.statSync(this.fullPath).birthtime)}set creationTime(t){g(()=>this.fs.utimesSync(this.fullPath,t,t))}get lastModified(){return F(()=>this.fs.statSync(this.fullPath).mtime)}set lastModified(t){g(()=>this.fs.utimesSync(this.fullPath,t,t))}get exists(){return F(()=>this.fs.existsSync(this.fullPath))??g(()=>this.fs.accessSync(this.fullPath))}get lastAccess(){return F(()=>this.fs.statSync(this.fullPath).atime)}get name(){return this.fullPath.split(this.separator).pop()||""}get nameWithoutExtension(){const t=this.name,s=t.lastIndexOf(".");return-1===s?t:t.substring(0,s)}get extension(){const t=this.name,s=t.lastIndexOf(".");return-1===s?"":t.substring(s+1)}get parent(){return 0===this.split.length?null:new x(this.split.slice(0,-1))}get uri(){return`file://${this.fullPath}`}get separator(){return x.sep}get isDirectory(){return F(()=>this.fs.statSync(this.fullPath).isDirectory())}get isFile(){return F(()=>this.fs.statSync(this.fullPath).isFile())}get isEmpty(){const t=this.isDirectory;return null===t?null:t?0===(this.listFiles()?.length??0):0===this.size}get isHidden(){return this.name.startsWith(".")||this.fullPath.split(this.separator).some(t=>t.startsWith("."))}get isSymbolicLink(){return F(()=>this.fs.lstatSync(this.fullPath).isSymbolicLink())}get size(){return F(()=>{const t=this.fs.statSync(this.fullPath);return t.isFile()?t.size:this.listFiles().reduce((t,s)=>t+(s.size??0),0)})}get sizeKB(){const t=this.size;return null!==t?t/1024:null}get sizeMB(){const t=this.size;return null!==t?t/1048576:null}get sizeGB(){const t=this.size;return null!==t?t/1073741824:null}to(...t){return new x(this.fullPath+"/"+t.join("/"))}contains(t){if(t.split.length<=this.split.length)return!1;for(let s=0;s<this.split.length;s++)if(this.split[s]!==t.split[s])return!1;return!0}createFile(t="",s){return this.exists?this.isFile?this:null:this.write(t,s)?this:null}delete(t,s=t){if(t){if("rmSync"in this.fs)return g(()=>this.fs.rmSync(this.fullPath,{recursive:!0,force:s}))?this:null;if("rmdirSync"in this.fs)return g(()=>this.fs.rmdirSync(this.fullPath,{recursive:!0}))?this:null;let t=!1;for(const s of this.listFiles()||[])s.delete(!0)||(t=!0);if(t)return null}return g(()=>this.fs.unlinkSync(this.fullPath))?this:null}deleteOnExit(t){e.set(this.fullPath,t),a()}clear(t){if(t){const t=this.listFiles();if(null===t)return null;let s=!1;for(const e of t||[])e.delete(!0)||(s=!0);return s?null:this}return this.write(""),null}listFiles(){if(!this.isDirectory)return null;const t=F(()=>this.fs.readdirSync(this.fullPath));return t?t.map(t=>new x(`${this.fullPath}/${t}`)):null}listFilenames(){return F(()=>this.fs.readdirSync(this.fullPath))}mkdir(t=!1){return g(()=>this.fs.mkdirSync(this.fullPath,{recursive:t}))?this:null}mkdirs(){return this.mkdir(!0)?this:null}renameTo(t,s,e){return this.fullPath===t.fullPath?this:!t.exists||s||t.delete(e)?(e&&this.parent?.mkdirs(),g(()=>this.fs.renameSync(this.fullPath,t.fullPath))?this:null):null}copyTo(t,s,e){if(this.fullPath===t.fullPath)return this;if(!this.exists)return null;if(t.exists&&!s&&!t.delete(e))return null;if(this.isFile){const s=this.read();return null===s?null:t.write(s)?this:null}if(!t.mkdir(e))return null;const i=this.listFiles();if(null===i)return null;let n=!1;for(const r of i){const i=t.to(r.name);r.copyTo(i,s,e)||(n=!0)}return n?null:this}*walk(){if(this.isDirectory){yield this;for(const t of this.listFiles()||[])yield*t.walk()}else yield this}read(t){return F(()=>this.fs.readFileSync(this.fullPath,t))}readLines(t="utf8"){const s=this.read(t);return"string"==typeof s?s.split(/\r?\n/):null}readJSON(){const t=this.read("utf8");if("string"!=typeof t)return null;try{return JSON.parse(t)}catch(t){return null}}readlink(){if(!this.isSymbolicLink)return null;const t=F(()=>this.fs.readlinkSync(this.fullPath));return t?new x(t):null}write(t,s){return null!==s&&"object"==typeof s&&"write"in s&&"function"==typeof s.write&&(t=s.write()),g(()=>this.fs.writeFileSync(this.fullPath,t,s))?this:null}writeJSON(t,s=2){return g(()=>this.write(JSON.stringify(t,null,s)))?this:null}append(t,s){return g(()=>this.fs.appendFileSync(this.fullPath,t,s))?this:null}get async(){return new S(this.split)}get configJSON(){return new P(this)}}function k(t){x.fs=t,S.fs=t.promises}function b(t){return new x(t)}function v(t){return new S(t)}let E=!0;if("undefined"!=typeof process)try{k(await import("fs")),E="win32"!==process.platform,x.sep=S.sep=E?"/":"\\"}catch{}export{x as File,S as FileAsync,x as FileSync,a as attachCleanup,n as cleanup,e as deleteQueue,v as fileAsync,b as fileSync,o as formatSize,h as getPathType,f as getSizeFormat,k as initFS,E as isLinux,u as isValidFilename,c as isValidPath,s as splitPath};
|
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",
|
package/types/IConfig.d.ts
CHANGED
|
File without changes
|
package/types/IFile.d.ts
CHANGED
|
File without changes
|
package/types/Utils.d.ts
CHANGED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/types/ktfile.d.ts
CHANGED
|
File without changes
|
package/types/sync/FileSync.d.ts
CHANGED
|
File without changes
|
package/types/sync/ISyncFS.d.ts
CHANGED
|
File without changes
|