@ooneex/fs 0.0.4
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/LICENSE +21 -0
- package/README.md +601 -0
- package/dist/index.d.ts +1659 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +13 -0
- package/package.json +43 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,1659 @@
|
|
|
1
|
+
import { Dirent as Dirent2, Stats as Stats2 } from "node:fs";
|
|
2
|
+
import { Dirent, Stats } from "node:fs";
|
|
3
|
+
/**
|
|
4
|
+
* Interface for file operations.
|
|
5
|
+
*
|
|
6
|
+
* Provides methods for reading, writing, and managing files using Bun's optimized file I/O APIs.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { File, type IFile } from "@ooneex/fs";
|
|
11
|
+
*
|
|
12
|
+
* function processFile(file: IFile): Promise<string> {
|
|
13
|
+
* return file.text();
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
* const file = new File("/path/to/file.txt");
|
|
17
|
+
* const content = await processFile(file);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
interface IFile {
|
|
21
|
+
/**
|
|
22
|
+
* Returns the file path.
|
|
23
|
+
*
|
|
24
|
+
* @returns The absolute or relative path of the file
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const path = file.getPath(); // "/path/to/file.txt"
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
getPath: () => string;
|
|
32
|
+
/**
|
|
33
|
+
* Returns the file name including extension.
|
|
34
|
+
*
|
|
35
|
+
* @returns The base name of the file
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const name = file.getName(); // "document.pdf"
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
getName: () => string;
|
|
43
|
+
/**
|
|
44
|
+
* Returns the file extension without the leading dot.
|
|
45
|
+
*
|
|
46
|
+
* @returns The file extension or empty string if none
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const ext = file.getExtension(); // "pdf"
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
getExtension: () => string;
|
|
54
|
+
/**
|
|
55
|
+
* Returns the directory containing the file.
|
|
56
|
+
*
|
|
57
|
+
* @returns The parent directory as an IDirectory instance
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const dir = file.getDirectory();
|
|
62
|
+
* console.log(dir.getPath()); // "/path/to"
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
getDirectory: () => IDirectory;
|
|
66
|
+
/**
|
|
67
|
+
* Returns the file size in bytes.
|
|
68
|
+
*
|
|
69
|
+
* @returns The size of the file in bytes, or 0 if file doesn't exist
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const size = file.getSize(); // 1024
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
getSize: () => number;
|
|
77
|
+
/**
|
|
78
|
+
* Returns the MIME type of the file.
|
|
79
|
+
*
|
|
80
|
+
* @returns The MIME type string (e.g., "text/plain", "application/json")
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* const type = file.getType(); // "application/json;charset=utf-8"
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
getType: () => string;
|
|
88
|
+
/**
|
|
89
|
+
* Checks if the file exists on disk.
|
|
90
|
+
*
|
|
91
|
+
* @returns A promise that resolves to true if the file exists
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* if (await file.exists()) {
|
|
96
|
+
* console.log("File found");
|
|
97
|
+
* }
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
exists: () => Promise<boolean>;
|
|
101
|
+
/**
|
|
102
|
+
* Reads the file content as a string.
|
|
103
|
+
*
|
|
104
|
+
* @returns A promise that resolves to the file content as a string
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* const content = await file.text();
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
text: () => Promise<string>;
|
|
112
|
+
/**
|
|
113
|
+
* Reads and parses the file content as JSON.
|
|
114
|
+
*
|
|
115
|
+
* @typeParam T - The expected type of the parsed JSON
|
|
116
|
+
* @returns A promise that resolves to the parsed JSON object
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```typescript
|
|
120
|
+
* const config = await file.json<{ name: string }>();
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
json: <T = unknown>() => Promise<T>;
|
|
124
|
+
/**
|
|
125
|
+
* Reads the file content as an ArrayBuffer.
|
|
126
|
+
*
|
|
127
|
+
* @returns A promise that resolves to the file content as an ArrayBuffer
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* const buffer = await file.arrayBuffer();
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
arrayBuffer: () => Promise<ArrayBuffer>;
|
|
135
|
+
/**
|
|
136
|
+
* Reads the file content as a Uint8Array.
|
|
137
|
+
*
|
|
138
|
+
* @returns A promise that resolves to the file content as a Uint8Array
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* const bytes = await file.bytes();
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
bytes: () => Promise<Uint8Array>;
|
|
146
|
+
/**
|
|
147
|
+
* Returns a ReadableStream for incremental file reading.
|
|
148
|
+
*
|
|
149
|
+
* @returns A ReadableStream of Uint8Array chunks
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* const stream = file.stream();
|
|
154
|
+
* for await (const chunk of stream) {
|
|
155
|
+
* console.log(chunk);
|
|
156
|
+
* }
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
stream: () => ReadableStream<Uint8Array>;
|
|
160
|
+
/**
|
|
161
|
+
* Returns a ReadableStream for incremental file reading as text.
|
|
162
|
+
*
|
|
163
|
+
* @returns A ReadableStream of string chunks
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```typescript
|
|
167
|
+
* const stream = file.streamAsText();
|
|
168
|
+
* for await (const chunk of stream) {
|
|
169
|
+
* console.log(chunk);
|
|
170
|
+
* }
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
streamAsText: () => ReadableStream<string>;
|
|
174
|
+
/**
|
|
175
|
+
* Returns a ReadableStream for incremental JSON parsing from a JSON array file.
|
|
176
|
+
*
|
|
177
|
+
* @typeParam T - The expected type of each JSON element
|
|
178
|
+
* @returns A ReadableStream of parsed JSON elements
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```typescript
|
|
182
|
+
* const stream = file.streamAsJson<{ id: number }>();
|
|
183
|
+
* for await (const item of stream) {
|
|
184
|
+
* console.log(item.id);
|
|
185
|
+
* }
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
streamAsJson: <T = unknown>() => ReadableStream<T>;
|
|
189
|
+
/**
|
|
190
|
+
* Writes data to the file, overwriting existing content.
|
|
191
|
+
*
|
|
192
|
+
* @param data - The data to write
|
|
193
|
+
* @returns A promise that resolves to the number of bytes written
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* ```typescript
|
|
197
|
+
* await file.write("Hello, World!");
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
write: (data: FileWriteDataType) => Promise<number>;
|
|
201
|
+
/**
|
|
202
|
+
* Appends data to the end of the file.
|
|
203
|
+
*
|
|
204
|
+
* @param data - The data to append (string or Uint8Array)
|
|
205
|
+
* @returns A promise that resolves to the total number of bytes in the file
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```typescript
|
|
209
|
+
* await file.append("New line\n");
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
append: (data: string | Uint8Array) => Promise<number>;
|
|
213
|
+
/**
|
|
214
|
+
* Copies the file to a destination path.
|
|
215
|
+
*
|
|
216
|
+
* @param destination - The destination file path
|
|
217
|
+
* @returns A promise that resolves to a new File instance for the copied file
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```typescript
|
|
221
|
+
* const copiedFile = await file.copy("/path/to/backup.txt");
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
copy: (destination: string) => Promise<IFile>;
|
|
225
|
+
/**
|
|
226
|
+
* Deletes the file from disk.
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* ```typescript
|
|
230
|
+
* await file.delete();
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
233
|
+
delete: () => Promise<void>;
|
|
234
|
+
/**
|
|
235
|
+
* Downloads a file from a URL and saves it to this file's path.
|
|
236
|
+
*
|
|
237
|
+
* @param url - The URL to download the file from
|
|
238
|
+
* @returns A promise that resolves to the number of bytes written
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```typescript
|
|
242
|
+
* const bytes = await file.download("https://example.com/image.png");
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
download: (url: string | URL) => Promise<number>;
|
|
246
|
+
/**
|
|
247
|
+
* Returns a FileSink for incremental writing.
|
|
248
|
+
*
|
|
249
|
+
* @param options - Optional configuration for the writer
|
|
250
|
+
* @returns A FileSink instance for buffered writing
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```typescript
|
|
254
|
+
* const writer = file.writer();
|
|
255
|
+
* writer.write("Line 1\n");
|
|
256
|
+
* writer.end();
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
writer: (options?: FileWriterOptionsType) => BunFileSinkType;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Interface for directory operations.
|
|
263
|
+
*
|
|
264
|
+
* Provides methods for creating, listing, copying, and managing directories.
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```typescript
|
|
268
|
+
* import { Directory, type IDirectory } from "@ooneex/fs";
|
|
269
|
+
*
|
|
270
|
+
* function cleanupDir(dir: IDirectory): Promise<void> {
|
|
271
|
+
* return dir.rm({ recursive: true });
|
|
272
|
+
* }
|
|
273
|
+
*
|
|
274
|
+
* const dir = new Directory("/tmp/cache");
|
|
275
|
+
* await cleanupDir(dir);
|
|
276
|
+
* ```
|
|
277
|
+
*/
|
|
278
|
+
interface IDirectory {
|
|
279
|
+
/**
|
|
280
|
+
* Returns the directory path.
|
|
281
|
+
*
|
|
282
|
+
* @returns The absolute or relative path of the directory
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```typescript
|
|
286
|
+
* const path = dir.getPath(); // "/path/to/directory"
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
getPath: () => string;
|
|
290
|
+
/**
|
|
291
|
+
* Returns the directory name.
|
|
292
|
+
*
|
|
293
|
+
* @returns The base name of the directory
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* ```typescript
|
|
297
|
+
* const name = dir.getName(); // "mydir"
|
|
298
|
+
* ```
|
|
299
|
+
*/
|
|
300
|
+
getName: () => string;
|
|
301
|
+
/**
|
|
302
|
+
* Returns the parent directory path.
|
|
303
|
+
*
|
|
304
|
+
* @returns The path of the parent directory
|
|
305
|
+
*
|
|
306
|
+
* @example
|
|
307
|
+
* ```typescript
|
|
308
|
+
* const parent = dir.getParent(); // "/path/to"
|
|
309
|
+
* ```
|
|
310
|
+
*/
|
|
311
|
+
getParent: () => string;
|
|
312
|
+
/**
|
|
313
|
+
* Checks if the directory exists.
|
|
314
|
+
*
|
|
315
|
+
* @returns A promise that resolves to true if the directory exists
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* ```typescript
|
|
319
|
+
* if (await dir.exists()) {
|
|
320
|
+
* console.log("Directory found");
|
|
321
|
+
* }
|
|
322
|
+
* ```
|
|
323
|
+
*/
|
|
324
|
+
exists: () => Promise<boolean>;
|
|
325
|
+
/**
|
|
326
|
+
* Creates the directory on disk.
|
|
327
|
+
*
|
|
328
|
+
* @param options - Optional configuration for directory creation
|
|
329
|
+
* @returns A promise that resolves when the directory is created
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```typescript
|
|
333
|
+
* await dir.mkdir({ recursive: true });
|
|
334
|
+
* ```
|
|
335
|
+
*/
|
|
336
|
+
mkdir: (options?: DirectoryCreateOptionsType) => Promise<void>;
|
|
337
|
+
/**
|
|
338
|
+
* Deletes the directory from disk.
|
|
339
|
+
*
|
|
340
|
+
* @param options - Optional configuration for directory deletion
|
|
341
|
+
* @returns A promise that resolves when the directory is deleted
|
|
342
|
+
*
|
|
343
|
+
* @example
|
|
344
|
+
* ```typescript
|
|
345
|
+
* await dir.rm({ recursive: true, force: true });
|
|
346
|
+
* ```
|
|
347
|
+
*/
|
|
348
|
+
rm: (options?: DirectoryDeleteOptionsType) => Promise<void>;
|
|
349
|
+
/**
|
|
350
|
+
* Lists the contents of the directory.
|
|
351
|
+
*
|
|
352
|
+
* @param options - Optional configuration for listing
|
|
353
|
+
* @returns A promise that resolves to an array of file/directory names
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* ```typescript
|
|
357
|
+
* const files = await dir.ls(); // ["file1.txt", "subdir"]
|
|
358
|
+
* ```
|
|
359
|
+
*/
|
|
360
|
+
ls: (options?: DirectoryListOptionsType) => Promise<string[]>;
|
|
361
|
+
/**
|
|
362
|
+
* Lists the contents of the directory with type information.
|
|
363
|
+
*
|
|
364
|
+
* @param options - Optional configuration for listing
|
|
365
|
+
* @returns A promise that resolves to an array of Dirent objects
|
|
366
|
+
*
|
|
367
|
+
* @example
|
|
368
|
+
* ```typescript
|
|
369
|
+
* const entries = await dir.lsWithTypes();
|
|
370
|
+
* entries.filter(e => e.isFile());
|
|
371
|
+
* ```
|
|
372
|
+
*/
|
|
373
|
+
lsWithTypes: (options?: DirectoryListOptionsType) => Promise<Dirent[]>;
|
|
374
|
+
/**
|
|
375
|
+
* Copies the directory to a destination path.
|
|
376
|
+
*
|
|
377
|
+
* @param destination - The destination directory path
|
|
378
|
+
* @param options - Optional configuration for copying
|
|
379
|
+
* @returns A promise that resolves when the directory is copied
|
|
380
|
+
*
|
|
381
|
+
* @example
|
|
382
|
+
* ```typescript
|
|
383
|
+
* await dir.cp("/path/to/backup");
|
|
384
|
+
* ```
|
|
385
|
+
*/
|
|
386
|
+
cp: (destination: string, options?: DirectoryCopyOptionsType) => Promise<void>;
|
|
387
|
+
/**
|
|
388
|
+
* Moves (renames) the directory to a new location.
|
|
389
|
+
*
|
|
390
|
+
* @param destination - The new directory path
|
|
391
|
+
* @returns A promise that resolves when the directory is moved
|
|
392
|
+
*
|
|
393
|
+
* @example
|
|
394
|
+
* ```typescript
|
|
395
|
+
* await dir.mv("/path/to/newname");
|
|
396
|
+
* ```
|
|
397
|
+
*/
|
|
398
|
+
mv: (destination: string) => Promise<void>;
|
|
399
|
+
/**
|
|
400
|
+
* Returns the directory statistics.
|
|
401
|
+
*
|
|
402
|
+
* @returns A promise that resolves to a Stats object
|
|
403
|
+
*
|
|
404
|
+
* @example
|
|
405
|
+
* ```typescript
|
|
406
|
+
* const stats = await dir.stat();
|
|
407
|
+
* console.log(stats.mtime);
|
|
408
|
+
* ```
|
|
409
|
+
*/
|
|
410
|
+
stat: () => Promise<Stats>;
|
|
411
|
+
/**
|
|
412
|
+
* Watches the directory for changes.
|
|
413
|
+
*
|
|
414
|
+
* @param callback - Function called when changes are detected
|
|
415
|
+
* @param options - Optional configuration for watching
|
|
416
|
+
* @returns A FSWatcher that can be closed to stop watching
|
|
417
|
+
*
|
|
418
|
+
* @example
|
|
419
|
+
* ```typescript
|
|
420
|
+
* const watcher = dir.watch((event, filename) => {
|
|
421
|
+
* console.log(event, filename);
|
|
422
|
+
* });
|
|
423
|
+
* ```
|
|
424
|
+
*/
|
|
425
|
+
watch: (callback: DirectoryWatchCallbackType, options?: DirectoryWatchOptionsType) => DirectoryWatcherType;
|
|
426
|
+
/**
|
|
427
|
+
* Checks if the directory is empty.
|
|
428
|
+
*
|
|
429
|
+
* @returns A promise that resolves to true if the directory has no contents
|
|
430
|
+
*
|
|
431
|
+
* @example
|
|
432
|
+
* ```typescript
|
|
433
|
+
* if (await dir.isEmpty()) {
|
|
434
|
+
* await dir.rm();
|
|
435
|
+
* }
|
|
436
|
+
* ```
|
|
437
|
+
*/
|
|
438
|
+
isEmpty: () => Promise<boolean>;
|
|
439
|
+
/**
|
|
440
|
+
* Calculates the total size of all contents in the directory.
|
|
441
|
+
*
|
|
442
|
+
* @returns A promise that resolves to the total size in bytes
|
|
443
|
+
*
|
|
444
|
+
* @example
|
|
445
|
+
* ```typescript
|
|
446
|
+
* const size = await dir.getSize();
|
|
447
|
+
* console.log(`${size} bytes`);
|
|
448
|
+
* ```
|
|
449
|
+
*/
|
|
450
|
+
getSize: () => Promise<number>;
|
|
451
|
+
/**
|
|
452
|
+
* Gets a list of files (not directories) in the directory.
|
|
453
|
+
*
|
|
454
|
+
* @param options - Optional configuration for getting files
|
|
455
|
+
* @returns A promise that resolves to an array of File instances
|
|
456
|
+
*
|
|
457
|
+
* @example
|
|
458
|
+
* ```typescript
|
|
459
|
+
* // Get immediate files
|
|
460
|
+
* const files = await dir.getFiles();
|
|
461
|
+
*
|
|
462
|
+
* // Get all files recursively
|
|
463
|
+
* const allFiles = await dir.getFiles({ recursive: true });
|
|
464
|
+
*
|
|
465
|
+
* // Get only TypeScript files
|
|
466
|
+
* const tsFiles = await dir.getFiles({ pattern: /\.ts$/ });
|
|
467
|
+
* ```
|
|
468
|
+
*/
|
|
469
|
+
getFiles: (options?: DirectoryGetFilesOptionsType) => Promise<IFile[]>;
|
|
470
|
+
/**
|
|
471
|
+
* Gets a list of subdirectories (not files) in the directory.
|
|
472
|
+
*
|
|
473
|
+
* @param options - Optional configuration for getting directories
|
|
474
|
+
* @returns A promise that resolves to an array of Directory instances
|
|
475
|
+
*
|
|
476
|
+
* @example
|
|
477
|
+
* ```typescript
|
|
478
|
+
* // Get immediate subdirectories
|
|
479
|
+
* const dirs = await dir.getDirectories();
|
|
480
|
+
*
|
|
481
|
+
* // Get all subdirectories recursively
|
|
482
|
+
* const allDirs = await dir.getDirectories({ recursive: true });
|
|
483
|
+
*
|
|
484
|
+
* // Get only directories starting with "test"
|
|
485
|
+
* const testDirs = await dir.getDirectories({ pattern: /^test/ });
|
|
486
|
+
* ```
|
|
487
|
+
*/
|
|
488
|
+
getDirectories: (options?: DirectoryGetDirectoriesOptionsType) => Promise<IDirectory[]>;
|
|
489
|
+
/**
|
|
490
|
+
* Changes to a subdirectory and returns a new Directory instance.
|
|
491
|
+
*
|
|
492
|
+
* @param paths - The relative path segments to the subdirectory
|
|
493
|
+
* @returns A new Directory instance pointing to the subdirectory
|
|
494
|
+
*
|
|
495
|
+
* @example
|
|
496
|
+
* ```typescript
|
|
497
|
+
* const dir = new Directory("/path/to/project");
|
|
498
|
+
*
|
|
499
|
+
* // Navigate to subdirectory
|
|
500
|
+
* const srcDir = dir.cd("src");
|
|
501
|
+
*
|
|
502
|
+
* // Navigate multiple levels with multiple args
|
|
503
|
+
* const componentsDir = dir.cd("src", "components", "ui");
|
|
504
|
+
*
|
|
505
|
+
* // Navigate to parent
|
|
506
|
+
* const parentDir = dir.cd("..");
|
|
507
|
+
* ```
|
|
508
|
+
*/
|
|
509
|
+
cd: (...paths: string[]) => IDirectory;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Types of data that can be written to a file.
|
|
513
|
+
*
|
|
514
|
+
* Supports strings, binary data (Blob, ArrayBuffer, TypedArrays), and Response objects.
|
|
515
|
+
*
|
|
516
|
+
* @example
|
|
517
|
+
* ```typescript
|
|
518
|
+
* // String
|
|
519
|
+
* await file.write("Hello, World!");
|
|
520
|
+
*
|
|
521
|
+
* // Uint8Array
|
|
522
|
+
* await file.write(new Uint8Array([72, 101, 108, 108, 111]));
|
|
523
|
+
*
|
|
524
|
+
* // Response from fetch
|
|
525
|
+
* const response = await fetch("https://example.com/data");
|
|
526
|
+
* await file.write(response);
|
|
527
|
+
* ```
|
|
528
|
+
*/
|
|
529
|
+
type FileWriteDataType = string | Blob | ArrayBuffer | SharedArrayBuffer | Uint8Array | Int8Array | Uint16Array | Int16Array | Uint32Array | Int32Array | Float32Array | Float64Array | Response;
|
|
530
|
+
/**
|
|
531
|
+
* Options for configuring the FileSink writer.
|
|
532
|
+
*
|
|
533
|
+
* @example
|
|
534
|
+
* ```typescript
|
|
535
|
+
* const writer = file.writer({
|
|
536
|
+
* highWaterMark: 1024 * 1024 // 1MB buffer
|
|
537
|
+
* });
|
|
538
|
+
* ```
|
|
539
|
+
*/
|
|
540
|
+
type FileWriterOptionsType = {
|
|
541
|
+
/**
|
|
542
|
+
* The buffer size in bytes before auto-flushing.
|
|
543
|
+
* When the internal buffer reaches this size, it will automatically flush to disk.
|
|
544
|
+
*
|
|
545
|
+
* @default undefined (uses Bun's default)
|
|
546
|
+
*
|
|
547
|
+
* @example
|
|
548
|
+
* ```typescript
|
|
549
|
+
* { highWaterMark: 1024 * 1024 } // 1MB buffer
|
|
550
|
+
* ```
|
|
551
|
+
*/
|
|
552
|
+
highWaterMark?: number;
|
|
553
|
+
};
|
|
554
|
+
/**
|
|
555
|
+
* Options for configuring file creation.
|
|
556
|
+
*
|
|
557
|
+
* @example
|
|
558
|
+
* ```typescript
|
|
559
|
+
* const file = new File("/path/to/file.txt", {
|
|
560
|
+
* type: "text/plain"
|
|
561
|
+
* });
|
|
562
|
+
* ```
|
|
563
|
+
*/
|
|
564
|
+
type FileOptionsType = {
|
|
565
|
+
/**
|
|
566
|
+
* The MIME type to use for the file.
|
|
567
|
+
* Overrides automatic MIME type detection.
|
|
568
|
+
*
|
|
569
|
+
* @example
|
|
570
|
+
* ```typescript
|
|
571
|
+
* { type: "application/json" }
|
|
572
|
+
* { type: "text/html;charset=utf-8" }
|
|
573
|
+
* ```
|
|
574
|
+
*/
|
|
575
|
+
type?: string;
|
|
576
|
+
};
|
|
577
|
+
/**
|
|
578
|
+
* Type representing Bun's FileSink for incremental file writing.
|
|
579
|
+
*
|
|
580
|
+
* FileSink provides buffered writing with manual flush control.
|
|
581
|
+
*
|
|
582
|
+
* @example
|
|
583
|
+
* ```typescript
|
|
584
|
+
* const writer: BunFileSinkType = file.writer();
|
|
585
|
+
* writer.write("chunk 1");
|
|
586
|
+
* writer.write("chunk 2");
|
|
587
|
+
* writer.flush(); // Write buffer to disk
|
|
588
|
+
* writer.end(); // Flush and close
|
|
589
|
+
* ```
|
|
590
|
+
*/
|
|
591
|
+
type BunFileSinkType = ReturnType<ReturnType<typeof Bun.file>["writer"]>;
|
|
592
|
+
/**
|
|
593
|
+
* Options for creating a directory.
|
|
594
|
+
*
|
|
595
|
+
* @example
|
|
596
|
+
* ```typescript
|
|
597
|
+
* await dir.create({
|
|
598
|
+
* recursive: true,
|
|
599
|
+
* mode: 0o755
|
|
600
|
+
* });
|
|
601
|
+
* ```
|
|
602
|
+
*/
|
|
603
|
+
type DirectoryCreateOptionsType = {
|
|
604
|
+
/**
|
|
605
|
+
* Whether to create parent directories if they don't exist.
|
|
606
|
+
*
|
|
607
|
+
* @default true
|
|
608
|
+
*
|
|
609
|
+
* @example
|
|
610
|
+
* ```typescript
|
|
611
|
+
* // Creates /path/to/nested/directory and all parents
|
|
612
|
+
* await dir.create({ recursive: true });
|
|
613
|
+
* ```
|
|
614
|
+
*/
|
|
615
|
+
recursive?: boolean;
|
|
616
|
+
/**
|
|
617
|
+
* The file mode (permissions) for the directory.
|
|
618
|
+
* Uses octal notation (e.g., 0o755 for rwxr-xr-x).
|
|
619
|
+
*
|
|
620
|
+
* @example
|
|
621
|
+
* ```typescript
|
|
622
|
+
* { mode: 0o700 } // rwx------
|
|
623
|
+
* { mode: 0o755 } // rwxr-xr-x
|
|
624
|
+
* ```
|
|
625
|
+
*/
|
|
626
|
+
mode?: number;
|
|
627
|
+
};
|
|
628
|
+
/**
|
|
629
|
+
* Options for deleting a directory.
|
|
630
|
+
*
|
|
631
|
+
* @example
|
|
632
|
+
* ```typescript
|
|
633
|
+
* await dir.delete({
|
|
634
|
+
* recursive: true,
|
|
635
|
+
* force: true
|
|
636
|
+
* });
|
|
637
|
+
* ```
|
|
638
|
+
*/
|
|
639
|
+
type DirectoryDeleteOptionsType = {
|
|
640
|
+
/**
|
|
641
|
+
* Whether to delete contents recursively.
|
|
642
|
+
* Required for non-empty directories.
|
|
643
|
+
*
|
|
644
|
+
* @default true
|
|
645
|
+
*
|
|
646
|
+
* @example
|
|
647
|
+
* ```typescript
|
|
648
|
+
* // Delete directory and all contents
|
|
649
|
+
* await dir.delete({ recursive: true });
|
|
650
|
+
* ```
|
|
651
|
+
*/
|
|
652
|
+
recursive?: boolean;
|
|
653
|
+
/**
|
|
654
|
+
* Whether to ignore errors if directory doesn't exist.
|
|
655
|
+
*
|
|
656
|
+
* @default false
|
|
657
|
+
*
|
|
658
|
+
* @example
|
|
659
|
+
* ```typescript
|
|
660
|
+
* // Won't throw if directory doesn't exist
|
|
661
|
+
* await dir.delete({ force: true });
|
|
662
|
+
* ```
|
|
663
|
+
*/
|
|
664
|
+
force?: boolean;
|
|
665
|
+
};
|
|
666
|
+
/**
|
|
667
|
+
* Options for listing directory contents.
|
|
668
|
+
*
|
|
669
|
+
* @example
|
|
670
|
+
* ```typescript
|
|
671
|
+
* const files = await dir.ls({ recursive: true });
|
|
672
|
+
* ```
|
|
673
|
+
*/
|
|
674
|
+
type DirectoryListOptionsType = {
|
|
675
|
+
/**
|
|
676
|
+
* Whether to list contents recursively including subdirectories.
|
|
677
|
+
*
|
|
678
|
+
* @default false
|
|
679
|
+
*
|
|
680
|
+
* @example
|
|
681
|
+
* ```typescript
|
|
682
|
+
* // List all files in directory tree
|
|
683
|
+
* const allFiles = await dir.ls({ recursive: true });
|
|
684
|
+
* ```
|
|
685
|
+
*/
|
|
686
|
+
recursive?: boolean;
|
|
687
|
+
};
|
|
688
|
+
/**
|
|
689
|
+
* Options for getting files from a directory.
|
|
690
|
+
*
|
|
691
|
+
* @example
|
|
692
|
+
* ```typescript
|
|
693
|
+
* const tsFiles = await dir.getFiles({ recursive: true, pattern: /\.ts$/ });
|
|
694
|
+
* ```
|
|
695
|
+
*/
|
|
696
|
+
type DirectoryGetFilesOptionsType = {
|
|
697
|
+
/**
|
|
698
|
+
* Whether to get files recursively from subdirectories.
|
|
699
|
+
*
|
|
700
|
+
* @default false
|
|
701
|
+
*
|
|
702
|
+
* @example
|
|
703
|
+
* ```typescript
|
|
704
|
+
* // Get all files in directory tree
|
|
705
|
+
* const allFiles = await dir.getFiles({ recursive: true });
|
|
706
|
+
* ```
|
|
707
|
+
*/
|
|
708
|
+
recursive?: boolean;
|
|
709
|
+
/**
|
|
710
|
+
* Regular expression pattern to filter files by name.
|
|
711
|
+
* Only files matching the pattern will be returned.
|
|
712
|
+
*
|
|
713
|
+
* @example
|
|
714
|
+
* ```typescript
|
|
715
|
+
* // Get only TypeScript files
|
|
716
|
+
* const tsFiles = await dir.getFiles({ pattern: /\.ts$/ });
|
|
717
|
+
*
|
|
718
|
+
* // Get files starting with "test"
|
|
719
|
+
* const testFiles = await dir.getFiles({ pattern: /^test/ });
|
|
720
|
+
* ```
|
|
721
|
+
*/
|
|
722
|
+
pattern?: RegExp;
|
|
723
|
+
};
|
|
724
|
+
/**
|
|
725
|
+
* Options for getting subdirectories from a directory.
|
|
726
|
+
*
|
|
727
|
+
* @example
|
|
728
|
+
* ```typescript
|
|
729
|
+
* const srcDirs = await dir.getDirectories({ recursive: true, pattern: /^src/ });
|
|
730
|
+
* ```
|
|
731
|
+
*/
|
|
732
|
+
type DirectoryGetDirectoriesOptionsType = {
|
|
733
|
+
/**
|
|
734
|
+
* Whether to get directories recursively from subdirectories.
|
|
735
|
+
*
|
|
736
|
+
* @default false
|
|
737
|
+
*
|
|
738
|
+
* @example
|
|
739
|
+
* ```typescript
|
|
740
|
+
* // Get all directories in directory tree
|
|
741
|
+
* const allDirs = await dir.getDirectories({ recursive: true });
|
|
742
|
+
* ```
|
|
743
|
+
*/
|
|
744
|
+
recursive?: boolean;
|
|
745
|
+
/**
|
|
746
|
+
* Regular expression pattern to filter directories by name.
|
|
747
|
+
* Only directories matching the pattern will be returned.
|
|
748
|
+
*
|
|
749
|
+
* @example
|
|
750
|
+
* ```typescript
|
|
751
|
+
* // Get only directories starting with "test"
|
|
752
|
+
* const testDirs = await dir.getDirectories({ pattern: /^test/ });
|
|
753
|
+
*
|
|
754
|
+
* // Get directories ending with "-config"
|
|
755
|
+
* const configDirs = await dir.getDirectories({ pattern: /-config$/ });
|
|
756
|
+
* ```
|
|
757
|
+
*/
|
|
758
|
+
pattern?: RegExp;
|
|
759
|
+
};
|
|
760
|
+
/**
|
|
761
|
+
* Options for copying a directory.
|
|
762
|
+
*
|
|
763
|
+
* @example
|
|
764
|
+
* ```typescript
|
|
765
|
+
* await dir.copy("/path/to/destination", {
|
|
766
|
+
* recursive: true,
|
|
767
|
+
* overwrite: true
|
|
768
|
+
* });
|
|
769
|
+
* ```
|
|
770
|
+
*/
|
|
771
|
+
type DirectoryCopyOptionsType = {
|
|
772
|
+
/**
|
|
773
|
+
* Whether to copy contents recursively.
|
|
774
|
+
*
|
|
775
|
+
* @default true
|
|
776
|
+
*
|
|
777
|
+
* @example
|
|
778
|
+
* ```typescript
|
|
779
|
+
* // Copy directory and all contents
|
|
780
|
+
* await dir.copy(dest, { recursive: true });
|
|
781
|
+
* ```
|
|
782
|
+
*/
|
|
783
|
+
recursive?: boolean;
|
|
784
|
+
/**
|
|
785
|
+
* Whether to overwrite existing files at destination.
|
|
786
|
+
*
|
|
787
|
+
* @default false
|
|
788
|
+
*
|
|
789
|
+
* @example
|
|
790
|
+
* ```typescript
|
|
791
|
+
* // Overwrite existing files
|
|
792
|
+
* await dir.copy(dest, { overwrite: true });
|
|
793
|
+
* ```
|
|
794
|
+
*/
|
|
795
|
+
overwrite?: boolean;
|
|
796
|
+
};
|
|
797
|
+
/**
|
|
798
|
+
* Options for watching a directory.
|
|
799
|
+
*
|
|
800
|
+
* @example
|
|
801
|
+
* ```typescript
|
|
802
|
+
* const watcher = dir.watch(callback, { recursive: true });
|
|
803
|
+
* ```
|
|
804
|
+
*/
|
|
805
|
+
type DirectoryWatchOptionsType = {
|
|
806
|
+
/**
|
|
807
|
+
* Whether to watch subdirectories recursively.
|
|
808
|
+
*
|
|
809
|
+
* @default false
|
|
810
|
+
*
|
|
811
|
+
* @example
|
|
812
|
+
* ```typescript
|
|
813
|
+
* // Watch all subdirectories
|
|
814
|
+
* dir.watch(callback, { recursive: true });
|
|
815
|
+
* ```
|
|
816
|
+
*/
|
|
817
|
+
recursive?: boolean;
|
|
818
|
+
};
|
|
819
|
+
/**
|
|
820
|
+
* Types of events emitted by the directory watcher.
|
|
821
|
+
*
|
|
822
|
+
* - `"rename"`: A file or directory was created, deleted, or renamed
|
|
823
|
+
* - `"change"`: A file's contents were modified
|
|
824
|
+
*
|
|
825
|
+
* @example
|
|
826
|
+
* ```typescript
|
|
827
|
+
* dir.watch((event: DirectoryWatchEventType, filename) => {
|
|
828
|
+
* if (event === "rename") {
|
|
829
|
+
* console.log("File created/deleted:", filename);
|
|
830
|
+
* } else if (event === "change") {
|
|
831
|
+
* console.log("File modified:", filename);
|
|
832
|
+
* }
|
|
833
|
+
* });
|
|
834
|
+
* ```
|
|
835
|
+
*/
|
|
836
|
+
type DirectoryWatchEventType = "rename" | "change";
|
|
837
|
+
/**
|
|
838
|
+
* Callback function for directory watch events.
|
|
839
|
+
*
|
|
840
|
+
* @param event - The type of change that occurred ("rename" or "change")
|
|
841
|
+
* @param filename - The name of the file that changed, or null if unavailable
|
|
842
|
+
*
|
|
843
|
+
* @example
|
|
844
|
+
* ```typescript
|
|
845
|
+
* const callback: DirectoryWatchCallbackType = (event, filename) => {
|
|
846
|
+
* console.log(`${event}: ${filename}`);
|
|
847
|
+
* };
|
|
848
|
+
*
|
|
849
|
+
* dir.watch(callback);
|
|
850
|
+
* ```
|
|
851
|
+
*/
|
|
852
|
+
type DirectoryWatchCallbackType = (event: DirectoryWatchEventType, filename: string | null) => void;
|
|
853
|
+
/**
|
|
854
|
+
* Type representing Node.js FSWatcher returned by the watch method.
|
|
855
|
+
*
|
|
856
|
+
* Use the `.close()` method to stop watching.
|
|
857
|
+
*
|
|
858
|
+
* @example
|
|
859
|
+
* ```typescript
|
|
860
|
+
* const watcher: DirectoryWatcherType = dir.watch((event, filename) => {
|
|
861
|
+
* console.log(event, filename);
|
|
862
|
+
* });
|
|
863
|
+
*
|
|
864
|
+
* // Stop watching
|
|
865
|
+
* watcher.close();
|
|
866
|
+
*
|
|
867
|
+
* // Or close on SIGINT
|
|
868
|
+
* process.on("SIGINT", () => {
|
|
869
|
+
* watcher.close();
|
|
870
|
+
* process.exit(0);
|
|
871
|
+
* });
|
|
872
|
+
* ```
|
|
873
|
+
*/
|
|
874
|
+
type DirectoryWatcherType = ReturnType<typeof import("node:fs").watch>;
|
|
875
|
+
/**
|
|
876
|
+
* A class for performing directory operations using Bun's optimized fs APIs.
|
|
877
|
+
*
|
|
878
|
+
* @example
|
|
879
|
+
* ```typescript
|
|
880
|
+
* import { Directory } from "@ooneex/fs";
|
|
881
|
+
*
|
|
882
|
+
* const dir = new Directory("/path/to/directory");
|
|
883
|
+
*
|
|
884
|
+
* // Create directory
|
|
885
|
+
* await dir.mkdir();
|
|
886
|
+
*
|
|
887
|
+
* // List contents
|
|
888
|
+
* const files = await dir.ls();
|
|
889
|
+
*
|
|
890
|
+
* // Check if empty
|
|
891
|
+
* if (await dir.isEmpty()) {
|
|
892
|
+
* await dir.rm();
|
|
893
|
+
* }
|
|
894
|
+
* ```
|
|
895
|
+
*/
|
|
896
|
+
declare class Directory implements IDirectory {
|
|
897
|
+
private readonly path;
|
|
898
|
+
/**
|
|
899
|
+
* Creates a new Directory instance.
|
|
900
|
+
*
|
|
901
|
+
* @param path - The directory path as a string
|
|
902
|
+
*
|
|
903
|
+
* @example
|
|
904
|
+
* ```typescript
|
|
905
|
+
* const dir = new Directory("/path/to/directory");
|
|
906
|
+
* const homeDir = new Directory("~/Documents");
|
|
907
|
+
* const relativeDir = new Directory("./src");
|
|
908
|
+
* ```
|
|
909
|
+
*/
|
|
910
|
+
constructor(path: string);
|
|
911
|
+
/**
|
|
912
|
+
* Returns the directory path.
|
|
913
|
+
*
|
|
914
|
+
* @returns The absolute or relative path of the directory
|
|
915
|
+
*
|
|
916
|
+
* @example
|
|
917
|
+
* ```typescript
|
|
918
|
+
* const dir = new Directory("/path/to/mydir");
|
|
919
|
+
* console.log(dir.getPath()); // "/path/to/mydir"
|
|
920
|
+
* ```
|
|
921
|
+
*/
|
|
922
|
+
getPath(): string;
|
|
923
|
+
/**
|
|
924
|
+
* Returns the directory name.
|
|
925
|
+
*
|
|
926
|
+
* @returns The base name of the directory
|
|
927
|
+
*
|
|
928
|
+
* @example
|
|
929
|
+
* ```typescript
|
|
930
|
+
* const dir = new Directory("/path/to/mydir");
|
|
931
|
+
* console.log(dir.getName()); // "mydir"
|
|
932
|
+
* ```
|
|
933
|
+
*/
|
|
934
|
+
getName(): string;
|
|
935
|
+
/**
|
|
936
|
+
* Returns the parent directory path.
|
|
937
|
+
*
|
|
938
|
+
* @returns The path of the parent directory
|
|
939
|
+
*
|
|
940
|
+
* @example
|
|
941
|
+
* ```typescript
|
|
942
|
+
* const dir = new Directory("/path/to/mydir");
|
|
943
|
+
* console.log(dir.getParent()); // "/path/to"
|
|
944
|
+
* ```
|
|
945
|
+
*/
|
|
946
|
+
getParent(): string;
|
|
947
|
+
/**
|
|
948
|
+
* Checks if the directory exists.
|
|
949
|
+
*
|
|
950
|
+
* @returns A promise that resolves to true if the directory exists
|
|
951
|
+
*
|
|
952
|
+
* @example
|
|
953
|
+
* ```typescript
|
|
954
|
+
* const dir = new Directory("/path/to/directory");
|
|
955
|
+
*
|
|
956
|
+
* if (await dir.exists()) {
|
|
957
|
+
* console.log("Directory exists!");
|
|
958
|
+
* } else {
|
|
959
|
+
* await dir.mkdir();
|
|
960
|
+
* }
|
|
961
|
+
* ```
|
|
962
|
+
*/
|
|
963
|
+
exists(): Promise<boolean>;
|
|
964
|
+
/**
|
|
965
|
+
* Creates the directory on disk.
|
|
966
|
+
*
|
|
967
|
+
* @param options - Optional configuration for directory creation
|
|
968
|
+
* @param options.recursive - Create parent directories if needed (default: true)
|
|
969
|
+
* @param options.mode - Directory permissions (e.g., 0o755)
|
|
970
|
+
* @throws {DirectoryException} If the directory cannot be created
|
|
971
|
+
*
|
|
972
|
+
* @example
|
|
973
|
+
* ```typescript
|
|
974
|
+
* const dir = new Directory("/path/to/new/nested/directory");
|
|
975
|
+
*
|
|
976
|
+
* // Create with all parent directories (default behavior)
|
|
977
|
+
* await dir.mkdir();
|
|
978
|
+
*
|
|
979
|
+
* // Create with specific permissions
|
|
980
|
+
* await dir.mkdir({ mode: 0o700 });
|
|
981
|
+
*
|
|
982
|
+
* // Create without recursive (fails if parent doesn't exist)
|
|
983
|
+
* await dir.mkdir({ recursive: false });
|
|
984
|
+
* ```
|
|
985
|
+
*/
|
|
986
|
+
mkdir(options?: DirectoryCreateOptionsType): Promise<void>;
|
|
987
|
+
/**
|
|
988
|
+
* Deletes the directory from disk.
|
|
989
|
+
*
|
|
990
|
+
* @param options - Optional configuration for directory deletion
|
|
991
|
+
* @param options.recursive - Delete contents recursively (default: true)
|
|
992
|
+
* @param options.force - Ignore errors if directory doesn't exist (default: false)
|
|
993
|
+
* @throws {DirectoryException} If the directory cannot be deleted
|
|
994
|
+
*
|
|
995
|
+
* @example
|
|
996
|
+
* ```typescript
|
|
997
|
+
* const dir = new Directory("/path/to/directory");
|
|
998
|
+
*
|
|
999
|
+
* // Delete directory and all contents
|
|
1000
|
+
* await dir.rm();
|
|
1001
|
+
*
|
|
1002
|
+
* // Delete only if empty
|
|
1003
|
+
* await dir.rm({ recursive: false });
|
|
1004
|
+
*
|
|
1005
|
+
* // Delete without throwing if doesn't exist
|
|
1006
|
+
* await dir.rm({ force: true });
|
|
1007
|
+
* ```
|
|
1008
|
+
*/
|
|
1009
|
+
rm(options?: DirectoryDeleteOptionsType): Promise<void>;
|
|
1010
|
+
/**
|
|
1011
|
+
* Lists the contents of the directory.
|
|
1012
|
+
*
|
|
1013
|
+
* @param options - Optional configuration for listing
|
|
1014
|
+
* @param options.recursive - List contents recursively (default: false)
|
|
1015
|
+
* @returns A promise that resolves to an array of file/directory names
|
|
1016
|
+
* @throws {DirectoryException} If the directory cannot be listed
|
|
1017
|
+
*
|
|
1018
|
+
* @example
|
|
1019
|
+
* ```typescript
|
|
1020
|
+
* const dir = new Directory("/path/to/directory");
|
|
1021
|
+
*
|
|
1022
|
+
* // List immediate contents
|
|
1023
|
+
* const files = await dir.ls();
|
|
1024
|
+
* console.log(files); // ["file1.txt", "file2.txt", "subdir"]
|
|
1025
|
+
*
|
|
1026
|
+
* // List all contents recursively
|
|
1027
|
+
* const allFiles = await dir.ls({ recursive: true });
|
|
1028
|
+
* console.log(allFiles); // ["file1.txt", "file2.txt", "subdir", "subdir/nested.txt"]
|
|
1029
|
+
* ```
|
|
1030
|
+
*/
|
|
1031
|
+
ls(options?: DirectoryListOptionsType): Promise<string[]>;
|
|
1032
|
+
/**
|
|
1033
|
+
* Lists the contents of the directory with type information.
|
|
1034
|
+
*
|
|
1035
|
+
* @param options - Optional configuration for listing
|
|
1036
|
+
* @param options.recursive - List contents recursively (default: false)
|
|
1037
|
+
* @returns A promise that resolves to an array of Dirent objects
|
|
1038
|
+
* @throws {DirectoryException} If the directory cannot be listed
|
|
1039
|
+
*
|
|
1040
|
+
* @example
|
|
1041
|
+
* ```typescript
|
|
1042
|
+
* const dir = new Directory("/path/to/directory");
|
|
1043
|
+
* const entries = await dir.lsWithTypes();
|
|
1044
|
+
*
|
|
1045
|
+
* for (const entry of entries) {
|
|
1046
|
+
* if (entry.isFile()) {
|
|
1047
|
+
* console.log(`File: ${entry.name}`);
|
|
1048
|
+
* } else if (entry.isDirectory()) {
|
|
1049
|
+
* console.log(`Directory: ${entry.name}`);
|
|
1050
|
+
* } else if (entry.isSymbolicLink()) {
|
|
1051
|
+
* console.log(`Symlink: ${entry.name}`);
|
|
1052
|
+
* }
|
|
1053
|
+
* }
|
|
1054
|
+
* ```
|
|
1055
|
+
*/
|
|
1056
|
+
lsWithTypes(options?: DirectoryListOptionsType): Promise<Dirent2[]>;
|
|
1057
|
+
/**
|
|
1058
|
+
* Copies the directory to a destination path.
|
|
1059
|
+
*
|
|
1060
|
+
* @param destination - The destination directory path
|
|
1061
|
+
* @param options - Optional configuration for copying
|
|
1062
|
+
* @param options.recursive - Copy contents recursively (default: true)
|
|
1063
|
+
* @param options.overwrite - Overwrite existing files (default: false)
|
|
1064
|
+
* @throws {DirectoryException} If the directory cannot be copied
|
|
1065
|
+
*
|
|
1066
|
+
* @example
|
|
1067
|
+
* ```typescript
|
|
1068
|
+
* const dir = new Directory("/path/to/source");
|
|
1069
|
+
*
|
|
1070
|
+
* // Copy directory and all contents
|
|
1071
|
+
* await dir.cp("/path/to/destination");
|
|
1072
|
+
*
|
|
1073
|
+
* // Copy with overwrite
|
|
1074
|
+
* await dir.cp("/path/to/destination", { overwrite: true });
|
|
1075
|
+
*
|
|
1076
|
+
* // Copy only the directory structure (no files)
|
|
1077
|
+
* await dir.cp("/path/to/destination", { recursive: false });
|
|
1078
|
+
* ```
|
|
1079
|
+
*/
|
|
1080
|
+
cp(destination: string, options?: DirectoryCopyOptionsType): Promise<void>;
|
|
1081
|
+
/**
|
|
1082
|
+
* Moves (renames) the directory to a new location.
|
|
1083
|
+
*
|
|
1084
|
+
* @param destination - The new directory path
|
|
1085
|
+
* @throws {DirectoryException} If the directory cannot be moved
|
|
1086
|
+
*
|
|
1087
|
+
* @example
|
|
1088
|
+
* ```typescript
|
|
1089
|
+
* const dir = new Directory("/path/to/oldname");
|
|
1090
|
+
*
|
|
1091
|
+
* // Rename directory
|
|
1092
|
+
* await dir.mv("/path/to/newname");
|
|
1093
|
+
*
|
|
1094
|
+
* // Move to different location
|
|
1095
|
+
* await dir.mv("/different/path/dirname");
|
|
1096
|
+
* ```
|
|
1097
|
+
*/
|
|
1098
|
+
mv(destination: string): Promise<void>;
|
|
1099
|
+
/**
|
|
1100
|
+
* Returns the directory statistics.
|
|
1101
|
+
*
|
|
1102
|
+
* @returns A promise that resolves to a Stats object
|
|
1103
|
+
* @throws {DirectoryException} If stats cannot be retrieved
|
|
1104
|
+
*
|
|
1105
|
+
* @example
|
|
1106
|
+
* ```typescript
|
|
1107
|
+
* const dir = new Directory("/path/to/directory");
|
|
1108
|
+
* const stats = await dir.stat();
|
|
1109
|
+
*
|
|
1110
|
+
* console.log(stats.isDirectory()); // true
|
|
1111
|
+
* console.log(stats.mode); // 16877 (permissions)
|
|
1112
|
+
* console.log(stats.mtime); // Date object (modification time)
|
|
1113
|
+
* console.log(stats.birthtime); // Date object (creation time)
|
|
1114
|
+
* ```
|
|
1115
|
+
*/
|
|
1116
|
+
stat(): Promise<Stats2>;
|
|
1117
|
+
/**
|
|
1118
|
+
* Watches the directory for changes.
|
|
1119
|
+
*
|
|
1120
|
+
* @param callback - Function called when changes are detected
|
|
1121
|
+
* @param options - Optional configuration for watching
|
|
1122
|
+
* @param options.recursive - Watch subdirectories recursively (default: false)
|
|
1123
|
+
* @returns A FSWatcher that can be closed to stop watching
|
|
1124
|
+
*
|
|
1125
|
+
* @example
|
|
1126
|
+
* ```typescript
|
|
1127
|
+
* const dir = new Directory("/path/to/directory");
|
|
1128
|
+
*
|
|
1129
|
+
* // Watch for changes
|
|
1130
|
+
* const watcher = dir.watch((event, filename) => {
|
|
1131
|
+
* console.log(`${event}: ${filename}`);
|
|
1132
|
+
* });
|
|
1133
|
+
*
|
|
1134
|
+
* // Watch recursively
|
|
1135
|
+
* const recursiveWatcher = dir.watch(
|
|
1136
|
+
* (event, filename) => console.log(event, filename),
|
|
1137
|
+
* { recursive: true }
|
|
1138
|
+
* );
|
|
1139
|
+
*
|
|
1140
|
+
* // Stop watching
|
|
1141
|
+
* watcher.close();
|
|
1142
|
+
*
|
|
1143
|
+
* // Handle close with SIGINT
|
|
1144
|
+
* process.on("SIGINT", () => {
|
|
1145
|
+
* watcher.close();
|
|
1146
|
+
* process.exit(0);
|
|
1147
|
+
* });
|
|
1148
|
+
* ```
|
|
1149
|
+
*/
|
|
1150
|
+
watch(callback: DirectoryWatchCallbackType, options?: DirectoryWatchOptionsType): DirectoryWatcherType;
|
|
1151
|
+
/**
|
|
1152
|
+
* Checks if the directory is empty.
|
|
1153
|
+
*
|
|
1154
|
+
* @returns A promise that resolves to true if the directory has no contents
|
|
1155
|
+
* @throws {DirectoryException} If the directory cannot be checked
|
|
1156
|
+
*
|
|
1157
|
+
* @example
|
|
1158
|
+
* ```typescript
|
|
1159
|
+
* const dir = new Directory("/path/to/directory");
|
|
1160
|
+
*
|
|
1161
|
+
* if (await dir.isEmpty()) {
|
|
1162
|
+
* console.log("Directory is empty");
|
|
1163
|
+
* await dir.rm();
|
|
1164
|
+
* } else {
|
|
1165
|
+
* console.log("Directory has contents");
|
|
1166
|
+
* }
|
|
1167
|
+
* ```
|
|
1168
|
+
*/
|
|
1169
|
+
isEmpty(): Promise<boolean>;
|
|
1170
|
+
/**
|
|
1171
|
+
* Calculates the total size of all contents in the directory.
|
|
1172
|
+
*
|
|
1173
|
+
* @returns A promise that resolves to the total size in bytes
|
|
1174
|
+
* @throws {DirectoryException} If the size cannot be calculated
|
|
1175
|
+
*
|
|
1176
|
+
* @example
|
|
1177
|
+
* ```typescript
|
|
1178
|
+
* const dir = new Directory("/path/to/directory");
|
|
1179
|
+
* const sizeBytes = await dir.getSize();
|
|
1180
|
+
*
|
|
1181
|
+
* // Format as human-readable
|
|
1182
|
+
* const sizeMB = (sizeBytes / (1024 * 1024)).toFixed(2);
|
|
1183
|
+
* console.log(`Directory size: ${sizeMB} MB`);
|
|
1184
|
+
* ```
|
|
1185
|
+
*/
|
|
1186
|
+
getSize(): Promise<number>;
|
|
1187
|
+
/**
|
|
1188
|
+
* Gets a list of files (not directories) in the directory.
|
|
1189
|
+
*
|
|
1190
|
+
* @param options - Optional configuration for getting files
|
|
1191
|
+
* @param options.recursive - Get files recursively from subdirectories (default: false)
|
|
1192
|
+
* @param options.pattern - Regular expression to filter files by name
|
|
1193
|
+
* @returns A promise that resolves to an array of File instances
|
|
1194
|
+
* @throws {DirectoryException} If the files cannot be listed
|
|
1195
|
+
*
|
|
1196
|
+
* @example
|
|
1197
|
+
* ```typescript
|
|
1198
|
+
* const dir = new Directory("/path/to/directory");
|
|
1199
|
+
*
|
|
1200
|
+
* // Get immediate files only
|
|
1201
|
+
* const files = await dir.getFiles();
|
|
1202
|
+
* for (const file of files) {
|
|
1203
|
+
* console.log(file.getName());
|
|
1204
|
+
* }
|
|
1205
|
+
*
|
|
1206
|
+
* // Get all files recursively
|
|
1207
|
+
* const allFiles = await dir.getFiles({ recursive: true });
|
|
1208
|
+
*
|
|
1209
|
+
* // Get only TypeScript files
|
|
1210
|
+
* const tsFiles = await dir.getFiles({ pattern: /\.ts$/ });
|
|
1211
|
+
*
|
|
1212
|
+
* // Get TypeScript files recursively
|
|
1213
|
+
* const allTsFiles = await dir.getFiles({ recursive: true, pattern: /\.tsx?$/ });
|
|
1214
|
+
* ```
|
|
1215
|
+
*/
|
|
1216
|
+
getFiles(options?: DirectoryGetFilesOptionsType): Promise<IFile[]>;
|
|
1217
|
+
/**
|
|
1218
|
+
* Gets a list of subdirectories (not files) in the directory.
|
|
1219
|
+
*
|
|
1220
|
+
* @param options - Optional configuration for getting directories
|
|
1221
|
+
* @param options.recursive - Get directories recursively from subdirectories (default: false)
|
|
1222
|
+
* @param options.pattern - Regular expression to filter directories by name
|
|
1223
|
+
* @returns A promise that resolves to an array of Directory instances
|
|
1224
|
+
* @throws {DirectoryException} If the directories cannot be listed
|
|
1225
|
+
*
|
|
1226
|
+
* @example
|
|
1227
|
+
* ```typescript
|
|
1228
|
+
* const dir = new Directory("/path/to/directory");
|
|
1229
|
+
*
|
|
1230
|
+
* // Get immediate subdirectories only
|
|
1231
|
+
* const dirs = await dir.getDirectories();
|
|
1232
|
+
* for (const subdir of dirs) {
|
|
1233
|
+
* console.log(subdir.getName());
|
|
1234
|
+
* }
|
|
1235
|
+
*
|
|
1236
|
+
* // Get all subdirectories recursively
|
|
1237
|
+
* const allDirs = await dir.getDirectories({ recursive: true });
|
|
1238
|
+
*
|
|
1239
|
+
* // Get only directories starting with "test"
|
|
1240
|
+
* const testDirs = await dir.getDirectories({ pattern: /^test/ });
|
|
1241
|
+
*
|
|
1242
|
+
* // Get directories recursively matching pattern
|
|
1243
|
+
* const srcDirs = await dir.getDirectories({ recursive: true, pattern: /^src/ });
|
|
1244
|
+
* ```
|
|
1245
|
+
*/
|
|
1246
|
+
getDirectories(options?: DirectoryGetDirectoriesOptionsType): Promise<IDirectory[]>;
|
|
1247
|
+
/**
|
|
1248
|
+
* Changes to a subdirectory and returns a new Directory instance.
|
|
1249
|
+
*
|
|
1250
|
+
* @param paths - The relative path segments to the subdirectory
|
|
1251
|
+
* @returns A new Directory instance pointing to the subdirectory
|
|
1252
|
+
*
|
|
1253
|
+
* @example
|
|
1254
|
+
* ```typescript
|
|
1255
|
+
* const dir = new Directory("/path/to/project");
|
|
1256
|
+
*
|
|
1257
|
+
* // Navigate to subdirectory
|
|
1258
|
+
* const srcDir = dir.cd("src");
|
|
1259
|
+
* console.log(srcDir.getPath()); // "/path/to/project/src"
|
|
1260
|
+
*
|
|
1261
|
+
* // Navigate multiple levels with multiple args
|
|
1262
|
+
* const componentsDir = dir.cd("src", "components", "ui");
|
|
1263
|
+
* console.log(componentsDir.getPath()); // "/path/to/project/src/components/ui"
|
|
1264
|
+
*
|
|
1265
|
+
* // Navigate to parent
|
|
1266
|
+
* const parentDir = dir.cd("..");
|
|
1267
|
+
* console.log(parentDir.getPath()); // "/path/to"
|
|
1268
|
+
*
|
|
1269
|
+
* // Chain navigation
|
|
1270
|
+
* const deepDir = dir.cd("src").cd("components").cd("ui");
|
|
1271
|
+
* ```
|
|
1272
|
+
*/
|
|
1273
|
+
cd(...paths: string[]): IDirectory;
|
|
1274
|
+
private calculateSize;
|
|
1275
|
+
}
|
|
1276
|
+
import { Exception } from "@ooneex/exception";
|
|
1277
|
+
declare class DirectoryException extends Exception {
|
|
1278
|
+
constructor(message: string, data?: Record<string, unknown>);
|
|
1279
|
+
}
|
|
1280
|
+
/**
|
|
1281
|
+
* A class for performing file operations using Bun's optimized file I/O APIs.
|
|
1282
|
+
*
|
|
1283
|
+
* @example
|
|
1284
|
+
* ```typescript
|
|
1285
|
+
* import { File } from "@ooneex/fs";
|
|
1286
|
+
*
|
|
1287
|
+
* const file = new File("/path/to/file.txt");
|
|
1288
|
+
*
|
|
1289
|
+
* // Read file content
|
|
1290
|
+
* const content = await file.text();
|
|
1291
|
+
*
|
|
1292
|
+
* // Write to file
|
|
1293
|
+
* await file.write("Hello, World!");
|
|
1294
|
+
*
|
|
1295
|
+
* // Check if file exists
|
|
1296
|
+
* if (await file.exists()) {
|
|
1297
|
+
* console.log("File exists!");
|
|
1298
|
+
* }
|
|
1299
|
+
* ```
|
|
1300
|
+
*/
|
|
1301
|
+
declare class File implements IFile {
|
|
1302
|
+
private readonly path;
|
|
1303
|
+
private readonly options;
|
|
1304
|
+
/**
|
|
1305
|
+
* Creates a new File instance.
|
|
1306
|
+
*
|
|
1307
|
+
* @param path - The file path as a string or URL
|
|
1308
|
+
* @param options - Optional configuration options
|
|
1309
|
+
*
|
|
1310
|
+
* @example
|
|
1311
|
+
* ```typescript
|
|
1312
|
+
* // Using string path
|
|
1313
|
+
* const file = new File("/path/to/file.txt");
|
|
1314
|
+
*
|
|
1315
|
+
* // Using URL
|
|
1316
|
+
* const file = new File(new URL("file:///path/to/file.txt"));
|
|
1317
|
+
*
|
|
1318
|
+
* // With custom MIME type
|
|
1319
|
+
* const file = new File("/path/to/file", { type: "application/json" });
|
|
1320
|
+
* ```
|
|
1321
|
+
*/
|
|
1322
|
+
constructor(path: string | URL, options?: FileOptionsType);
|
|
1323
|
+
private getBunFile;
|
|
1324
|
+
/**
|
|
1325
|
+
* Returns the file path.
|
|
1326
|
+
*
|
|
1327
|
+
* @returns The absolute or relative path of the file
|
|
1328
|
+
*
|
|
1329
|
+
* @example
|
|
1330
|
+
* ```typescript
|
|
1331
|
+
* const file = new File("/path/to/document.pdf");
|
|
1332
|
+
* console.log(file.getPath()); // "/path/to/document.pdf"
|
|
1333
|
+
* ```
|
|
1334
|
+
*/
|
|
1335
|
+
getPath(): string;
|
|
1336
|
+
/**
|
|
1337
|
+
* Returns the file name including extension.
|
|
1338
|
+
*
|
|
1339
|
+
* @returns The base name of the file
|
|
1340
|
+
*
|
|
1341
|
+
* @example
|
|
1342
|
+
* ```typescript
|
|
1343
|
+
* const file = new File("/path/to/document.pdf");
|
|
1344
|
+
* console.log(file.getName()); // "document.pdf"
|
|
1345
|
+
* ```
|
|
1346
|
+
*/
|
|
1347
|
+
getName(): string;
|
|
1348
|
+
/**
|
|
1349
|
+
* Returns the file extension without the leading dot.
|
|
1350
|
+
*
|
|
1351
|
+
* @returns The file extension or empty string if none
|
|
1352
|
+
*
|
|
1353
|
+
* @example
|
|
1354
|
+
* ```typescript
|
|
1355
|
+
* const file = new File("/path/to/document.pdf");
|
|
1356
|
+
* console.log(file.getExtension()); // "pdf"
|
|
1357
|
+
*
|
|
1358
|
+
* const noExt = new File("/path/to/README");
|
|
1359
|
+
* console.log(noExt.getExtension()); // ""
|
|
1360
|
+
* ```
|
|
1361
|
+
*/
|
|
1362
|
+
getExtension(): string;
|
|
1363
|
+
/**
|
|
1364
|
+
* Returns the directory containing the file.
|
|
1365
|
+
*
|
|
1366
|
+
* @returns The parent directory as an IDirectory instance
|
|
1367
|
+
*
|
|
1368
|
+
* @example
|
|
1369
|
+
* ```typescript
|
|
1370
|
+
* const file = new File("/path/to/document.pdf");
|
|
1371
|
+
* const dir = file.getDirectory();
|
|
1372
|
+
* console.log(dir.getPath()); // "/path/to"
|
|
1373
|
+
* ```
|
|
1374
|
+
*/
|
|
1375
|
+
getDirectory(): IDirectory;
|
|
1376
|
+
/**
|
|
1377
|
+
* Returns the file size in bytes.
|
|
1378
|
+
*
|
|
1379
|
+
* @returns The size of the file in bytes, or 0 if file doesn't exist
|
|
1380
|
+
*
|
|
1381
|
+
* @example
|
|
1382
|
+
* ```typescript
|
|
1383
|
+
* const file = new File("/path/to/document.pdf");
|
|
1384
|
+
* console.log(file.getSize()); // 1024
|
|
1385
|
+
* ```
|
|
1386
|
+
*/
|
|
1387
|
+
getSize(): number;
|
|
1388
|
+
/**
|
|
1389
|
+
* Returns the MIME type of the file.
|
|
1390
|
+
*
|
|
1391
|
+
* @returns The MIME type string (e.g., "text/plain", "application/json")
|
|
1392
|
+
*
|
|
1393
|
+
* @example
|
|
1394
|
+
* ```typescript
|
|
1395
|
+
* const txtFile = new File("/path/to/file.txt");
|
|
1396
|
+
* console.log(txtFile.getType()); // "text/plain;charset=utf-8"
|
|
1397
|
+
*
|
|
1398
|
+
* const jsonFile = new File("/path/to/data.json");
|
|
1399
|
+
* console.log(jsonFile.getType()); // "application/json;charset=utf-8"
|
|
1400
|
+
* ```
|
|
1401
|
+
*/
|
|
1402
|
+
getType(): string;
|
|
1403
|
+
/**
|
|
1404
|
+
* Checks if the file exists on disk.
|
|
1405
|
+
*
|
|
1406
|
+
* @returns A promise that resolves to true if the file exists
|
|
1407
|
+
*
|
|
1408
|
+
* @example
|
|
1409
|
+
* ```typescript
|
|
1410
|
+
* const file = new File("/path/to/file.txt");
|
|
1411
|
+
*
|
|
1412
|
+
* if (await file.exists()) {
|
|
1413
|
+
* console.log("File exists!");
|
|
1414
|
+
* } else {
|
|
1415
|
+
* console.log("File not found");
|
|
1416
|
+
* }
|
|
1417
|
+
* ```
|
|
1418
|
+
*/
|
|
1419
|
+
exists(): Promise<boolean>;
|
|
1420
|
+
/**
|
|
1421
|
+
* Reads the file content as a string.
|
|
1422
|
+
*
|
|
1423
|
+
* @returns A promise that resolves to the file content as a string
|
|
1424
|
+
* @throws {FileException} If the file cannot be read
|
|
1425
|
+
*
|
|
1426
|
+
* @example
|
|
1427
|
+
* ```typescript
|
|
1428
|
+
* const file = new File("/path/to/file.txt");
|
|
1429
|
+
* const content = await file.text();
|
|
1430
|
+
* console.log(content); // "Hello, World!"
|
|
1431
|
+
* ```
|
|
1432
|
+
*/
|
|
1433
|
+
text(): Promise<string>;
|
|
1434
|
+
/**
|
|
1435
|
+
* Reads and parses the file content as JSON.
|
|
1436
|
+
*
|
|
1437
|
+
* @typeParam T - The expected type of the parsed JSON
|
|
1438
|
+
* @returns A promise that resolves to the parsed JSON object
|
|
1439
|
+
* @throws {FileException} If the file cannot be read or parsed
|
|
1440
|
+
*
|
|
1441
|
+
* @example
|
|
1442
|
+
* ```typescript
|
|
1443
|
+
* interface Config {
|
|
1444
|
+
* name: string;
|
|
1445
|
+
* version: number;
|
|
1446
|
+
* }
|
|
1447
|
+
*
|
|
1448
|
+
* const file = new File("/path/to/config.json");
|
|
1449
|
+
* const config = await file.json<Config>();
|
|
1450
|
+
* console.log(config.name); // "my-app"
|
|
1451
|
+
* ```
|
|
1452
|
+
*/
|
|
1453
|
+
json<T = unknown>(): Promise<T>;
|
|
1454
|
+
/**
|
|
1455
|
+
* Reads the file content as an ArrayBuffer.
|
|
1456
|
+
*
|
|
1457
|
+
* @returns A promise that resolves to the file content as an ArrayBuffer
|
|
1458
|
+
* @throws {FileException} If the file cannot be read
|
|
1459
|
+
*
|
|
1460
|
+
* @example
|
|
1461
|
+
* ```typescript
|
|
1462
|
+
* const file = new File("/path/to/binary.bin");
|
|
1463
|
+
* const buffer = await file.arrayBuffer();
|
|
1464
|
+
* const view = new DataView(buffer);
|
|
1465
|
+
* console.log(view.getInt32(0)); // First 4 bytes as int32
|
|
1466
|
+
* ```
|
|
1467
|
+
*/
|
|
1468
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
1469
|
+
/**
|
|
1470
|
+
* Reads the file content as a Uint8Array.
|
|
1471
|
+
*
|
|
1472
|
+
* @returns A promise that resolves to the file content as a Uint8Array
|
|
1473
|
+
* @throws {FileException} If the file cannot be read
|
|
1474
|
+
*
|
|
1475
|
+
* @example
|
|
1476
|
+
* ```typescript
|
|
1477
|
+
* const file = new File("/path/to/binary.bin");
|
|
1478
|
+
* const bytes = await file.bytes();
|
|
1479
|
+
* console.log(bytes[0]); // First byte
|
|
1480
|
+
* console.log(bytes.length); // Total bytes
|
|
1481
|
+
* ```
|
|
1482
|
+
*/
|
|
1483
|
+
bytes(): Promise<Uint8Array>;
|
|
1484
|
+
/**
|
|
1485
|
+
* Returns a ReadableStream for incremental file reading.
|
|
1486
|
+
*
|
|
1487
|
+
* @returns A ReadableStream of Uint8Array chunks
|
|
1488
|
+
*
|
|
1489
|
+
* @example
|
|
1490
|
+
* ```typescript
|
|
1491
|
+
* const file = new File("/path/to/large-file.txt");
|
|
1492
|
+
* const stream = file.stream();
|
|
1493
|
+
*
|
|
1494
|
+
* for await (const chunk of stream) {
|
|
1495
|
+
* console.log(`Received ${chunk.length} bytes`);
|
|
1496
|
+
* }
|
|
1497
|
+
* ```
|
|
1498
|
+
*/
|
|
1499
|
+
stream(): ReadableStream<Uint8Array>;
|
|
1500
|
+
/**
|
|
1501
|
+
* Returns a ReadableStream for incremental file reading as text.
|
|
1502
|
+
*
|
|
1503
|
+
* @returns A ReadableStream of string chunks
|
|
1504
|
+
*
|
|
1505
|
+
* @example
|
|
1506
|
+
* ```typescript
|
|
1507
|
+
* const file = new File("/path/to/large-file.txt");
|
|
1508
|
+
* const stream = file.streamAsText();
|
|
1509
|
+
*
|
|
1510
|
+
* for await (const chunk of stream) {
|
|
1511
|
+
* console.log(`Received text: ${chunk}`);
|
|
1512
|
+
* }
|
|
1513
|
+
* ```
|
|
1514
|
+
*/
|
|
1515
|
+
streamAsText(): ReadableStream<string>;
|
|
1516
|
+
/**
|
|
1517
|
+
* Returns a ReadableStream for incremental JSON parsing from a JSON array file.
|
|
1518
|
+
*
|
|
1519
|
+
* Reads a file containing a JSON array and streams each parsed element individually.
|
|
1520
|
+
* This is useful for processing large JSON array files without loading everything into memory.
|
|
1521
|
+
*
|
|
1522
|
+
* @typeParam T - The expected type of each JSON element
|
|
1523
|
+
* @returns A ReadableStream of parsed JSON elements
|
|
1524
|
+
*
|
|
1525
|
+
* @example
|
|
1526
|
+
* ```typescript
|
|
1527
|
+
* // For a file containing: [{"id": 1}, {"id": 2}, {"id": 3}]
|
|
1528
|
+
* const file = new File("/path/to/data.json");
|
|
1529
|
+
* const stream = file.streamAsJson<{ id: number }>();
|
|
1530
|
+
*
|
|
1531
|
+
* for await (const item of stream) {
|
|
1532
|
+
* console.log(item.id); // 1, 2, 3
|
|
1533
|
+
* }
|
|
1534
|
+
* ```
|
|
1535
|
+
*/
|
|
1536
|
+
streamAsJson<T = unknown>(): ReadableStream<T>;
|
|
1537
|
+
/**
|
|
1538
|
+
* Writes data to the file, overwriting existing content.
|
|
1539
|
+
*
|
|
1540
|
+
* @param data - The data to write (string, Blob, ArrayBuffer, TypedArray, or Response)
|
|
1541
|
+
* @returns A promise that resolves to the number of bytes written
|
|
1542
|
+
* @throws {FileException} If the file cannot be written
|
|
1543
|
+
*
|
|
1544
|
+
* @example
|
|
1545
|
+
* ```typescript
|
|
1546
|
+
* const file = new File("/path/to/file.txt");
|
|
1547
|
+
*
|
|
1548
|
+
* // Write string
|
|
1549
|
+
* await file.write("Hello, World!");
|
|
1550
|
+
*
|
|
1551
|
+
* // Write Uint8Array
|
|
1552
|
+
* await file.write(new Uint8Array([72, 101, 108, 108, 111]));
|
|
1553
|
+
*
|
|
1554
|
+
* // Write from Response
|
|
1555
|
+
* const response = await fetch("https://example.com/data");
|
|
1556
|
+
* await file.write(response);
|
|
1557
|
+
* ```
|
|
1558
|
+
*/
|
|
1559
|
+
write(data: FileWriteDataType): Promise<number>;
|
|
1560
|
+
/**
|
|
1561
|
+
* Appends data to the end of the file.
|
|
1562
|
+
*
|
|
1563
|
+
* @param data - The data to append (string or Uint8Array)
|
|
1564
|
+
* @returns A promise that resolves to the total number of bytes in the file
|
|
1565
|
+
* @throws {FileException} If the file cannot be appended to
|
|
1566
|
+
*
|
|
1567
|
+
* @example
|
|
1568
|
+
* ```typescript
|
|
1569
|
+
* const file = new File("/path/to/log.txt");
|
|
1570
|
+
*
|
|
1571
|
+
* // Append string
|
|
1572
|
+
* await file.append("New log entry\n");
|
|
1573
|
+
*
|
|
1574
|
+
* // Append binary data
|
|
1575
|
+
* await file.append(new Uint8Array([10, 20, 30]));
|
|
1576
|
+
* ```
|
|
1577
|
+
*/
|
|
1578
|
+
append(data: string | Uint8Array): Promise<number>;
|
|
1579
|
+
/**
|
|
1580
|
+
* Copies the file to a destination path.
|
|
1581
|
+
*
|
|
1582
|
+
* @param destination - The destination file path
|
|
1583
|
+
* @returns A promise that resolves to a new File instance for the copied file
|
|
1584
|
+
* @throws {FileException} If the file cannot be copied
|
|
1585
|
+
*
|
|
1586
|
+
* @example
|
|
1587
|
+
* ```typescript
|
|
1588
|
+
* const file = new File("/path/to/original.txt");
|
|
1589
|
+
* const copiedFile = await file.copy("/path/to/backup.txt");
|
|
1590
|
+
*
|
|
1591
|
+
* // The original file is preserved
|
|
1592
|
+
* console.log(await file.exists()); // true
|
|
1593
|
+
* // Access the copied file
|
|
1594
|
+
* console.log(await copiedFile.text()); // same content as original
|
|
1595
|
+
* ```
|
|
1596
|
+
*/
|
|
1597
|
+
copy(destination: string): Promise<IFile>;
|
|
1598
|
+
/**
|
|
1599
|
+
* Deletes the file from disk.
|
|
1600
|
+
*
|
|
1601
|
+
* @throws {FileException} If the file cannot be deleted
|
|
1602
|
+
*
|
|
1603
|
+
* @example
|
|
1604
|
+
* ```typescript
|
|
1605
|
+
* const file = new File("/path/to/temp.txt");
|
|
1606
|
+
*
|
|
1607
|
+
* if (await file.exists()) {
|
|
1608
|
+
* await file.delete();
|
|
1609
|
+
* console.log("File deleted");
|
|
1610
|
+
* }
|
|
1611
|
+
* ```
|
|
1612
|
+
*/
|
|
1613
|
+
delete(): Promise<void>;
|
|
1614
|
+
/**
|
|
1615
|
+
* Downloads a file from a URL and saves it to this file's path.
|
|
1616
|
+
*
|
|
1617
|
+
* @param url - The URL to download the file from
|
|
1618
|
+
* @returns A promise that resolves to the number of bytes written
|
|
1619
|
+
* @throws {FileException} If the download or write operation fails
|
|
1620
|
+
*
|
|
1621
|
+
* @example
|
|
1622
|
+
* ```typescript
|
|
1623
|
+
* const file = new File("/path/to/image.png");
|
|
1624
|
+
*
|
|
1625
|
+
* // Download from URL
|
|
1626
|
+
* const bytes = await file.download("https://example.com/image.png");
|
|
1627
|
+
* console.log(`Downloaded ${bytes} bytes`);
|
|
1628
|
+
*
|
|
1629
|
+
* // Download with URL object
|
|
1630
|
+
* await file.download(new URL("https://example.com/data.json"));
|
|
1631
|
+
* ```
|
|
1632
|
+
*/
|
|
1633
|
+
download(url: string | URL): Promise<number>;
|
|
1634
|
+
/**
|
|
1635
|
+
* Returns a FileSink for incremental writing.
|
|
1636
|
+
*
|
|
1637
|
+
* @param options - Optional configuration for the writer
|
|
1638
|
+
* @returns A FileSink instance for buffered writing
|
|
1639
|
+
*
|
|
1640
|
+
* @example
|
|
1641
|
+
* ```typescript
|
|
1642
|
+
* const file = new File("/path/to/output.txt");
|
|
1643
|
+
* const writer = file.writer({ highWaterMark: 1024 * 1024 }); // 1MB buffer
|
|
1644
|
+
*
|
|
1645
|
+
* writer.write("Line 1\n");
|
|
1646
|
+
* writer.write("Line 2\n");
|
|
1647
|
+
* writer.flush(); // Flush buffer to disk
|
|
1648
|
+
*
|
|
1649
|
+
* writer.write("Line 3\n");
|
|
1650
|
+
* writer.end(); // Flush and close
|
|
1651
|
+
* ```
|
|
1652
|
+
*/
|
|
1653
|
+
writer(options?: FileWriterOptionsType): BunFileSinkType;
|
|
1654
|
+
}
|
|
1655
|
+
import { Exception as Exception2 } from "@ooneex/exception";
|
|
1656
|
+
declare class FileException extends Exception2 {
|
|
1657
|
+
constructor(message: string, data?: Record<string, unknown>);
|
|
1658
|
+
}
|
|
1659
|
+
export { IFile, IDirectory, FileWriterOptionsType, FileWriteDataType, FileOptionsType, FileException, File, DirectoryWatcherType, DirectoryWatchOptionsType, DirectoryWatchEventType, DirectoryWatchCallbackType, DirectoryListOptionsType, DirectoryGetFilesOptionsType, DirectoryGetDirectoriesOptionsType, DirectoryException, DirectoryDeleteOptionsType, DirectoryCreateOptionsType, DirectoryCopyOptionsType, Directory, BunFileSinkType };
|