@tauri-apps/plugin-fs 2.5.2 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,7 +12,7 @@ Access the file system.
12
12
 
13
13
  ## Install
14
14
 
15
- _This plugin requires a Rust version of at least **1.77.2**_
15
+ _This plugin requires a Rust version of at least **1.90**_
16
16
 
17
17
  There are three general methods of installation that we can recommend.
18
18
 
package/dist-js/index.cjs CHANGED
@@ -88,10 +88,16 @@ var core = require('@tauri-apps/api/core');
88
88
  *
89
89
  * @module
90
90
  */
91
+ /**
92
+ * Defines how the offset given to {@linkcode FileHandle.seek} is interpreted.
93
+ */
91
94
  exports.SeekMode = void 0;
92
95
  (function (SeekMode) {
96
+ /** The offset is relative to the start of the file. */
93
97
  SeekMode[SeekMode["Start"] = 0] = "Start";
98
+ /** The offset is relative to the current cursor position. */
94
99
  SeekMode[SeekMode["Current"] = 1] = "Current";
100
+ /** The offset is relative to the end of the file. */
95
101
  SeekMode[SeekMode["End"] = 2] = "End";
96
102
  })(exports.SeekMode || (exports.SeekMode = {}));
97
103
  function parseFileInfo(r) {
@@ -165,6 +171,8 @@ class FileHandle extends core.Resource {
165
171
  * await file.close();
166
172
  * ```
167
173
  *
174
+ * @param buffer The buffer the file contents are read into.
175
+ * @returns A promise resolving to the number of bytes read, or `null` when the end of the file was reached.
168
176
  * @since 2.0.0
169
177
  */
170
178
  async read(buffer) {
@@ -215,6 +223,9 @@ class FileHandle extends core.Resource {
215
223
  * await file.close();
216
224
  * ```
217
225
  *
226
+ * @param offset The number of bytes the cursor is moved by.
227
+ * @param whence Defines the position the `offset` is relative to.
228
+ * @returns A promise resolving to the new cursor position, relative to the start of the file.
218
229
  * @since 2.0.0
219
230
  */
220
231
  async seek(offset, whence) {
@@ -236,6 +247,7 @@ class FileHandle extends core.Resource {
236
247
  * await file.close();
237
248
  * ```
238
249
  *
250
+ * @returns A promise resolving to the metadata of this file.
239
251
  * @since 2.0.0
240
252
  */
241
253
  async stat() {
@@ -266,6 +278,7 @@ class FileHandle extends core.Resource {
266
278
  * await file.close();
267
279
  * ```
268
280
  *
281
+ * @param len The length the file is truncated or extended to, in bytes. When not provided the entire file contents are truncated.
269
282
  * @since 2.0.0
270
283
  */
271
284
  async truncate(len) {
@@ -292,6 +305,8 @@ class FileHandle extends core.Resource {
292
305
  * await file.close();
293
306
  * ```
294
307
  *
308
+ * @param data The bytes written to the file.
309
+ * @returns A promise resolving to the number of bytes written.
295
310
  * @since 2.0.0
296
311
  */
297
312
  async write(data) {
@@ -313,6 +328,9 @@ class FileHandle extends core.Resource {
313
328
  * await file.close();
314
329
  * ```
315
330
  *
331
+ * @param path The path of the file, relative to `options.baseDir` when it is provided.
332
+ * @param options Options defining the base directory of `path`.
333
+ * @returns A promise resolving to the handle of the created file.
316
334
  * @since 2.0.0
317
335
  */
318
336
  async function create(path, options) {
@@ -339,6 +357,9 @@ async function create(path, options) {
339
357
  * await file.close();
340
358
  * ```
341
359
  *
360
+ * @param path The path of the file, relative to `options.baseDir` when it is provided.
361
+ * @param options Options defining the base directory of `path` and how the file is opened.
362
+ * @returns A promise resolving to the handle of the open file.
342
363
  * @since 2.0.0
343
364
  */
344
365
  async function open(path, options) {
@@ -359,6 +380,9 @@ async function open(path, options) {
359
380
  * await copyFile('app.conf', 'app.conf.bk', { fromPathBaseDir: BaseDirectory.AppConfig, toPathBaseDir: BaseDirectory.AppConfig });
360
381
  * ```
361
382
  *
383
+ * @param fromPath The path of the file to copy from.
384
+ * @param toPath The path of the file to copy to.
385
+ * @param options Options defining the base directory of each path.
362
386
  * @since 2.0.0
363
387
  */
364
388
  async function copyFile(fromPath, toPath, options) {
@@ -380,6 +404,8 @@ async function copyFile(fromPath, toPath, options) {
380
404
  * await mkdir('users', { baseDir: BaseDirectory.AppLocalData });
381
405
  * ```
382
406
  *
407
+ * @param path The path of the directory to create.
408
+ * @param options Options defining the base directory of `path`, the directory permissions and whether intermediate directories are created.
383
409
  * @since 2.0.0
384
410
  */
385
411
  async function mkdir(path, options) {
@@ -397,20 +423,23 @@ async function mkdir(path, options) {
397
423
  * ```typescript
398
424
  * import { readDir, BaseDirectory } from '@tauri-apps/plugin-fs';
399
425
  * import { join } from '@tauri-apps/api/path';
400
- * const dir = "users"
401
- * const entries = await readDir('users', { baseDir: BaseDirectory.AppLocalData });
402
- * processEntriesRecursively(dir, entries);
426
+ * const dir = 'users';
427
+ * const entries = await readDir(dir, { baseDir: BaseDirectory.AppLocalData });
428
+ * await processEntriesRecursively(dir, entries);
403
429
  * async function processEntriesRecursively(parent, entries) {
404
430
  * for (const entry of entries) {
405
431
  * console.log(`Entry: ${entry.name}`);
406
432
  * if (entry.isDirectory) {
407
- * const dir = await join(parent, entry.name);
408
- * processEntriesRecursively(dir, await readDir(dir, { baseDir: BaseDirectory.AppLocalData }))
433
+ * const entryPath = await join(parent, entry.name);
434
+ * await processEntriesRecursively(entryPath, await readDir(entryPath, { baseDir: BaseDirectory.AppLocalData }));
409
435
  * }
410
436
  * }
411
437
  * }
412
438
  * ```
413
439
  *
440
+ * @param path The path of the directory to read.
441
+ * @param options Options defining the base directory of `path`.
442
+ * @returns A promise resolving to the list of entries in the directory.
414
443
  * @since 2.0.0
415
444
  */
416
445
  async function readDir(path, options) {
@@ -431,6 +460,9 @@ async function readDir(path, options) {
431
460
  * const contents = await readFile('avatar.png', { baseDir: BaseDirectory.Resource });
432
461
  * ```
433
462
  *
463
+ * @param path The path of the file to read.
464
+ * @param options Options defining the base directory of `path`.
465
+ * @returns A promise resolving to the contents of the file as bytes.
434
466
  * @since 2.0.0
435
467
  */
436
468
  async function readFile(path, options) {
@@ -451,6 +483,9 @@ async function readFile(path, options) {
451
483
  * const contents = await readTextFile('app.conf', { baseDir: BaseDirectory.AppConfig });
452
484
  * ```
453
485
  *
486
+ * @param path The path of the file to read.
487
+ * @param options Options defining the base directory of `path` and the text encoding.
488
+ * @returns A promise resolving to the contents of the file as a string.
454
489
  * @since 2.0.0
455
490
  */
456
491
  async function readTextFile(path, options) {
@@ -477,6 +512,9 @@ async function readTextFile(path, options) {
477
512
  * You could also call {@linkcode AsyncIterableIterator.next} to advance the
478
513
  * iterator so you can lazily read the next line whenever you want.
479
514
  *
515
+ * @param path The path of the file to read.
516
+ * @param options Options defining the base directory of `path` and the text encoding.
517
+ * @returns A promise resolving to an iterator over the lines of the file.
480
518
  * @since 2.0.0
481
519
  */
482
520
  async function readTextFileLines(path, options) {
@@ -497,7 +535,15 @@ async function readTextFileLines(path, options) {
497
535
  options: options != null ? { ...options, encoding } : undefined
498
536
  });
499
537
  }
500
- const arr = await core.invoke('plugin:fs|read_text_file_lines_next', { rid: this.rid });
538
+ let arr;
539
+ try {
540
+ arr = await core.invoke('plugin:fs|read_text_file_lines_next', { rid: this.rid });
541
+ }
542
+ catch (error) {
543
+ // the resource is closed on errors, the next iteration starts over
544
+ this.rid = null;
545
+ throw error;
546
+ }
501
547
  const bytes = arr instanceof ArrayBuffer ? new Uint8Array(arr) : Uint8Array.from(arr);
502
548
  // Rust side will never return an empty array for this command and
503
549
  // ensure there is at least one elements there.
@@ -516,6 +562,16 @@ async function readTextFileLines(path, options) {
516
562
  done
517
563
  };
518
564
  },
565
+ // called when a `for await` loop exits early (`break`, `return` or `throw`)
566
+ async return() {
567
+ if (this.rid !== null) {
568
+ const rid = this.rid;
569
+ this.rid = null;
570
+ // close the file, otherwise it stays open until the webview is destroyed
571
+ await new core.Resource(rid).close();
572
+ }
573
+ return { value: null, done: true };
574
+ },
519
575
  [Symbol.asyncIterator]() {
520
576
  return this;
521
577
  }
@@ -531,6 +587,8 @@ async function readTextFileLines(path, options) {
531
587
  * await remove('users', { baseDir: BaseDirectory.AppLocalData });
532
588
  * ```
533
589
  *
590
+ * @param path The path of the file or directory to remove.
591
+ * @param options Options defining the base directory of `path` and whether directories are removed recursively.
534
592
  * @since 2.0.0
535
593
  */
536
594
  async function remove(path, options) {
@@ -555,6 +613,9 @@ async function remove(path, options) {
555
613
  * await rename('avatar.png', 'deleted.png', { oldPathBaseDir: BaseDirectory.App, newPathBaseDir: BaseDirectory.AppLocalData });
556
614
  * ```
557
615
  *
616
+ * @param oldPath The path of the file or directory to rename.
617
+ * @param newPath The path the file or directory is renamed to.
618
+ * @param options Options defining the base directory of each path.
558
619
  * @since 2.0.0
559
620
  */
560
621
  async function rename(oldPath, newPath, options) {
@@ -579,6 +640,9 @@ async function rename(oldPath, newPath, options) {
579
640
  * console.log(fileInfo.isFile); // true
580
641
  * ```
581
642
  *
643
+ * @param path The path of the file or directory to inspect.
644
+ * @param options Options defining the base directory of `path`.
645
+ * @returns A promise resolving to the metadata of the file or directory.
582
646
  * @since 2.0.0
583
647
  */
584
648
  async function stat(path, options) {
@@ -600,6 +664,9 @@ async function stat(path, options) {
600
664
  * console.log(fileInfo.isFile); // true
601
665
  * ```
602
666
  *
667
+ * @param path The path of the file, directory or symlink to inspect.
668
+ * @param options Options defining the base directory of `path`.
669
+ * @returns A promise resolving to the metadata of the path itself.
603
670
  * @since 2.0.0
604
671
  */
605
672
  async function lstat(path, options) {
@@ -627,6 +694,9 @@ async function lstat(path, options) {
627
694
  * console.log(data); // "Hello W"
628
695
  * ```
629
696
  *
697
+ * @param path The path of the file to truncate or extend.
698
+ * @param len The length the file is resized to, in bytes. Defaults to `0`.
699
+ * @param options Options defining the base directory of `path`.
630
700
  * @since 2.0.0
631
701
  */
632
702
  async function truncate(path, len, options) {
@@ -650,6 +720,9 @@ async function truncate(path, len, options) {
650
720
  * await writeFile('file.txt', data, { baseDir: BaseDirectory.AppLocalData });
651
721
  * ```
652
722
  *
723
+ * @param path The path of the file to write to.
724
+ * @param data The bytes written to the file, either as a buffer or as a stream of chunks.
725
+ * @param options Options defining the base directory of `path` and how the file is opened.
653
726
  * @since 2.0.0
654
727
  */
655
728
  async function writeFile(path, data, options) {
@@ -661,7 +734,9 @@ async function writeFile(path, data, options) {
661
734
  read: false,
662
735
  create: true,
663
736
  write: true,
664
- ...options
737
+ ...options,
738
+ // overwrite the previous contents, like when writing a buffer
739
+ truncate: !options?.append
665
740
  });
666
741
  const reader = data.getReader();
667
742
  try {
@@ -687,16 +762,20 @@ async function writeFile(path, data, options) {
687
762
  }
688
763
  }
689
764
  /**
690
- * Writes UTF-8 string `data` to the given `path`, by default creating a new file if needed, else overwriting.
691
- @example
692
- * ```typescript
693
- * import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
694
- *
695
- * await writeTextFile('file.txt', "Hello world", { baseDir: BaseDirectory.AppLocalData });
696
- * ```
697
- *
698
- * @since 2.0.0
699
- */
765
+ * Writes UTF-8 string `data` to the given `path`, by default creating a new file if needed, else overwriting.
766
+ *
767
+ * @example
768
+ * ```typescript
769
+ * import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
770
+ *
771
+ * await writeTextFile('file.txt', "Hello world", { baseDir: BaseDirectory.AppLocalData });
772
+ * ```
773
+ *
774
+ * @param path The path of the file to write to.
775
+ * @param data The UTF-8 string written to the file.
776
+ * @param options Options defining the base directory of `path` and how the file is opened.
777
+ * @since 2.0.0
778
+ */
700
779
  async function writeTextFile(path, data, options) {
701
780
  if (path instanceof URL && path.protocol !== 'file:') {
702
781
  throw new TypeError('Must be a file URL.');
@@ -718,6 +797,9 @@ async function writeTextFile(path, data, options) {
718
797
  * await exists('avatar.png', { baseDir: BaseDirectory.AppData });
719
798
  * ```
720
799
  *
800
+ * @param path The path to check.
801
+ * @param options Options defining the base directory of `path`.
802
+ * @returns A promise resolving to `true` when the path exists, `false` otherwise.
721
803
  * @since 2.0.0
722
804
  */
723
805
  async function exists(path, options) {
@@ -754,6 +836,27 @@ async function watchInternal(paths, cb, options) {
754
836
  /**
755
837
  * Watch changes (after a delay) on files or directories.
756
838
  *
839
+ * Events that happen within the `delayMs` window are grouped and delivered in a single callback call.
840
+ * Requires the `watch` Cargo feature of the Rust plugin to be enabled.
841
+ *
842
+ * @example
843
+ * ```typescript
844
+ * import { watch, BaseDirectory } from '@tauri-apps/plugin-fs';
845
+ *
846
+ * const unwatch = await watch(
847
+ * 'app.conf',
848
+ * (event) => console.log(event.type, event.paths),
849
+ * { baseDir: BaseDirectory.AppConfig, delayMs: 500 }
850
+ * );
851
+ *
852
+ * // stop watching when you are done
853
+ * unwatch();
854
+ * ```
855
+ *
856
+ * @param paths The path or list of paths to watch. Each path can be a string or a `file://` URL.
857
+ * @param cb The callback executed for each batch of file system changes.
858
+ * @param options Options defining the base directory of the paths, the debounce delay and whether directories are watched recursively.
859
+ * @returns A promise resolving to a function that stops watching the given paths.
757
860
  * @since 2.0.0
758
861
  */
759
862
  async function watch(paths, cb, options) {
@@ -766,6 +869,27 @@ async function watch(paths, cb, options) {
766
869
  /**
767
870
  * Watch changes on files or directories.
768
871
  *
872
+ * Unlike `watch`, changes are reported as soon as they are detected, without a debounce delay.
873
+ * Requires the `watch` Cargo feature of the Rust plugin to be enabled.
874
+ *
875
+ * @example
876
+ * ```typescript
877
+ * import { watchImmediate, BaseDirectory } from '@tauri-apps/plugin-fs';
878
+ *
879
+ * const unwatch = await watchImmediate(
880
+ * 'logs',
881
+ * (event) => console.log(event.type, event.paths),
882
+ * { baseDir: BaseDirectory.AppLog, recursive: true }
883
+ * );
884
+ *
885
+ * // stop watching when you are done
886
+ * unwatch();
887
+ * ```
888
+ *
889
+ * @param paths The path or list of paths to watch. Each path can be a string or a `file://` URL.
890
+ * @param cb The callback executed for each file system change.
891
+ * @param options Options defining the base directory of the paths and whether directories are watched recursively.
892
+ * @returns A promise resolving to a function that stops watching the given paths.
769
893
  * @since 2.0.0
770
894
  */
771
895
  async function watchImmediate(paths, cb, options) {
@@ -787,14 +911,18 @@ async function watchImmediate(paths, cb, options) {
787
911
  * console.log(dirSize); // 1024
788
912
  * ```
789
913
  *
914
+ * @param path The path of the file or directory to measure.
915
+ * @param options Options defining the base directory of `path` (since 2.6.0).
916
+ * @returns A promise resolving to the size in bytes.
790
917
  * @since 2.1.0
791
918
  */
792
- async function size(path) {
919
+ async function size(path, options) {
793
920
  if (path instanceof URL && path.protocol !== 'file:') {
794
921
  throw new TypeError('Must be a file URL.');
795
922
  }
796
923
  return await core.invoke('plugin:fs|size', {
797
- path: path instanceof URL ? path.toString() : path
924
+ path: path instanceof URL ? path.toString() : path,
925
+ options
798
926
  });
799
927
  }
800
928
  /**
@@ -821,6 +949,7 @@ async function size(path) {
821
949
  * // ... use the resource ...
822
950
  * ```
823
951
  *
952
+ * @param path The path or `file://` URL of the resource to start accessing.
824
953
  * @since 2.5.0
825
954
  */
826
955
  async function startAccessingSecurityScopedResource(path) {
@@ -852,6 +981,7 @@ async function startAccessingSecurityScopedResource(path) {
852
981
  * await stopAccessingSecurityScopedResource(filePath);
853
982
  * ```
854
983
  *
984
+ * @param path The path or `file://` URL of the resource to stop accessing.
855
985
  * @since 2.5.0
856
986
  */
857
987
  async function stopAccessingSecurityScopedResource(path) {
@@ -864,8 +994,8 @@ async function stopAccessingSecurityScopedResource(path) {
864
994
  }
865
995
 
866
996
  Object.defineProperty(exports, "BaseDirectory", {
867
- enumerable: true,
868
- get: function () { return path.BaseDirectory; }
997
+ enumerable: true,
998
+ get: function () { return path.BaseDirectory; }
869
999
  });
870
1000
  exports.FileHandle = FileHandle;
871
1001
  exports.copyFile = copyFile;
@@ -82,9 +82,15 @@
82
82
  */
83
83
  import { BaseDirectory } from '@tauri-apps/api/path';
84
84
  import { Resource } from '@tauri-apps/api/core';
85
+ /**
86
+ * Defines how the offset given to {@linkcode FileHandle.seek} is interpreted.
87
+ */
85
88
  declare enum SeekMode {
89
+ /** The offset is relative to the start of the file. */
86
90
  Start = 0,
91
+ /** The offset is relative to the current cursor position. */
87
92
  Current = 1,
93
+ /** The offset is relative to the end of the file. */
88
94
  End = 2
89
95
  }
90
96
  /**
@@ -251,6 +257,8 @@ declare class FileHandle extends Resource {
251
257
  * await file.close();
252
258
  * ```
253
259
  *
260
+ * @param buffer The buffer the file contents are read into.
261
+ * @returns A promise resolving to the number of bytes read, or `null` when the end of the file was reached.
254
262
  * @since 2.0.0
255
263
  */
256
264
  read(buffer: Uint8Array): Promise<number | null>;
@@ -284,6 +292,9 @@ declare class FileHandle extends Resource {
284
292
  * await file.close();
285
293
  * ```
286
294
  *
295
+ * @param offset The number of bytes the cursor is moved by.
296
+ * @param whence Defines the position the `offset` is relative to.
297
+ * @returns A promise resolving to the new cursor position, relative to the start of the file.
287
298
  * @since 2.0.0
288
299
  */
289
300
  seek(offset: number, whence: SeekMode): Promise<number>;
@@ -299,6 +310,7 @@ declare class FileHandle extends Resource {
299
310
  * await file.close();
300
311
  * ```
301
312
  *
313
+ * @returns A promise resolving to the metadata of this file.
302
314
  * @since 2.0.0
303
315
  */
304
316
  stat(): Promise<FileInfo>;
@@ -324,6 +336,7 @@ declare class FileHandle extends Resource {
324
336
  * await file.close();
325
337
  * ```
326
338
  *
339
+ * @param len The length the file is truncated or extended to, in bytes. When not provided the entire file contents are truncated.
327
340
  * @since 2.0.0
328
341
  */
329
342
  truncate(len?: number): Promise<void>;
@@ -345,11 +358,15 @@ declare class FileHandle extends Resource {
345
358
  * await file.close();
346
359
  * ```
347
360
  *
361
+ * @param data The bytes written to the file.
362
+ * @returns A promise resolving to the number of bytes written.
348
363
  * @since 2.0.0
349
364
  */
350
365
  write(data: Uint8Array): Promise<number>;
351
366
  }
352
367
  /**
368
+ * Options for the `create` function, which creates or truncates a file.
369
+ *
353
370
  * @since 2.0.0
354
371
  */
355
372
  interface CreateOptions {
@@ -368,10 +385,15 @@ interface CreateOptions {
368
385
  * await file.close();
369
386
  * ```
370
387
  *
388
+ * @param path The path of the file, relative to `options.baseDir` when it is provided.
389
+ * @param options Options defining the base directory of `path`.
390
+ * @returns A promise resolving to the handle of the created file.
371
391
  * @since 2.0.0
372
392
  */
373
393
  declare function create(path: string | URL, options?: CreateOptions): Promise<FileHandle>;
374
394
  /**
395
+ * Options for the `open` function, defining how the file is opened and which operations are allowed on it.
396
+ *
375
397
  * @since 2.0.0
376
398
  */
377
399
  interface OpenOptions {
@@ -437,10 +459,15 @@ interface OpenOptions {
437
459
  * await file.close();
438
460
  * ```
439
461
  *
462
+ * @param path The path of the file, relative to `options.baseDir` when it is provided.
463
+ * @param options Options defining the base directory of `path` and how the file is opened.
464
+ * @returns A promise resolving to the handle of the open file.
440
465
  * @since 2.0.0
441
466
  */
442
467
  declare function open(path: string | URL, options?: OpenOptions): Promise<FileHandle>;
443
468
  /**
469
+ * Options for the `copyFile` function, defining the base directory of each path.
470
+ *
444
471
  * @since 2.0.0
445
472
  */
446
473
  interface CopyFileOptions {
@@ -457,10 +484,15 @@ interface CopyFileOptions {
457
484
  * await copyFile('app.conf', 'app.conf.bk', { fromPathBaseDir: BaseDirectory.AppConfig, toPathBaseDir: BaseDirectory.AppConfig });
458
485
  * ```
459
486
  *
487
+ * @param fromPath The path of the file to copy from.
488
+ * @param toPath The path of the file to copy to.
489
+ * @param options Options defining the base directory of each path.
460
490
  * @since 2.0.0
461
491
  */
462
492
  declare function copyFile(fromPath: string | URL, toPath: string | URL, options?: CopyFileOptions): Promise<void>;
463
493
  /**
494
+ * Options for the `mkdir` function, which creates a directory.
495
+ *
464
496
  * @since 2.0.0
465
497
  */
466
498
  interface MkdirOptions {
@@ -481,10 +513,14 @@ interface MkdirOptions {
481
513
  * await mkdir('users', { baseDir: BaseDirectory.AppLocalData });
482
514
  * ```
483
515
  *
516
+ * @param path The path of the directory to create.
517
+ * @param options Options defining the base directory of `path`, the directory permissions and whether intermediate directories are created.
484
518
  * @since 2.0.0
485
519
  */
486
520
  declare function mkdir(path: string | URL, options?: MkdirOptions): Promise<void>;
487
521
  /**
522
+ * Options for the `readDir` function, which lists the entries of a directory.
523
+ *
488
524
  * @since 2.0.0
489
525
  */
490
526
  interface ReadDirOptions {
@@ -514,24 +550,29 @@ interface DirEntry {
514
550
  * ```typescript
515
551
  * import { readDir, BaseDirectory } from '@tauri-apps/plugin-fs';
516
552
  * import { join } from '@tauri-apps/api/path';
517
- * const dir = "users"
518
- * const entries = await readDir('users', { baseDir: BaseDirectory.AppLocalData });
519
- * processEntriesRecursively(dir, entries);
553
+ * const dir = 'users';
554
+ * const entries = await readDir(dir, { baseDir: BaseDirectory.AppLocalData });
555
+ * await processEntriesRecursively(dir, entries);
520
556
  * async function processEntriesRecursively(parent, entries) {
521
557
  * for (const entry of entries) {
522
558
  * console.log(`Entry: ${entry.name}`);
523
559
  * if (entry.isDirectory) {
524
- * const dir = await join(parent, entry.name);
525
- * processEntriesRecursively(dir, await readDir(dir, { baseDir: BaseDirectory.AppLocalData }))
560
+ * const entryPath = await join(parent, entry.name);
561
+ * await processEntriesRecursively(entryPath, await readDir(entryPath, { baseDir: BaseDirectory.AppLocalData }));
526
562
  * }
527
563
  * }
528
564
  * }
529
565
  * ```
530
566
  *
567
+ * @param path The path of the directory to read.
568
+ * @param options Options defining the base directory of `path`.
569
+ * @returns A promise resolving to the list of entries in the directory.
531
570
  * @since 2.0.0
532
571
  */
533
572
  declare function readDir(path: string | URL, options?: ReadDirOptions): Promise<DirEntry[]>;
534
573
  /**
574
+ * Options for the functions that read a file, such as `readFile` and `readTextFile`.
575
+ *
535
576
  * @since 2.0.0
536
577
  */
537
578
  interface ReadFileOptions {
@@ -549,6 +590,9 @@ interface ReadFileOptions {
549
590
  * const contents = await readFile('avatar.png', { baseDir: BaseDirectory.Resource });
550
591
  * ```
551
592
  *
593
+ * @param path The path of the file to read.
594
+ * @param options Options defining the base directory of `path`.
595
+ * @returns A promise resolving to the contents of the file as bytes.
552
596
  * @since 2.0.0
553
597
  */
554
598
  declare function readFile(path: string | URL, options?: ReadFileOptions): Promise<Uint8Array<ArrayBuffer>>;
@@ -560,6 +604,9 @@ declare function readFile(path: string | URL, options?: ReadFileOptions): Promis
560
604
  * const contents = await readTextFile('app.conf', { baseDir: BaseDirectory.AppConfig });
561
605
  * ```
562
606
  *
607
+ * @param path The path of the file to read.
608
+ * @param options Options defining the base directory of `path` and the text encoding.
609
+ * @returns A promise resolving to the contents of the file as a string.
563
610
  * @since 2.0.0
564
611
  */
565
612
  declare function readTextFile(path: string | URL, options?: ReadFileOptions): Promise<string>;
@@ -576,10 +623,15 @@ declare function readTextFile(path: string | URL, options?: ReadFileOptions): Pr
576
623
  * You could also call {@linkcode AsyncIterableIterator.next} to advance the
577
624
  * iterator so you can lazily read the next line whenever you want.
578
625
  *
626
+ * @param path The path of the file to read.
627
+ * @param options Options defining the base directory of `path` and the text encoding.
628
+ * @returns A promise resolving to an iterator over the lines of the file.
579
629
  * @since 2.0.0
580
630
  */
581
631
  declare function readTextFileLines(path: string | URL, options?: ReadFileOptions): Promise<AsyncIterableIterator<string>>;
582
632
  /**
633
+ * Options for the `remove` function, which deletes a file or a directory.
634
+ *
583
635
  * @since 2.0.0
584
636
  */
585
637
  interface RemoveOptions {
@@ -598,10 +650,14 @@ interface RemoveOptions {
598
650
  * await remove('users', { baseDir: BaseDirectory.AppLocalData });
599
651
  * ```
600
652
  *
653
+ * @param path The path of the file or directory to remove.
654
+ * @param options Options defining the base directory of `path` and whether directories are removed recursively.
601
655
  * @since 2.0.0
602
656
  */
603
657
  declare function remove(path: string | URL, options?: RemoveOptions): Promise<void>;
604
658
  /**
659
+ * Options for the `rename` function, defining the base directory of each path.
660
+ *
605
661
  * @since 2.0.0
606
662
  */
607
663
  interface RenameOptions {
@@ -623,10 +679,15 @@ interface RenameOptions {
623
679
  * await rename('avatar.png', 'deleted.png', { oldPathBaseDir: BaseDirectory.App, newPathBaseDir: BaseDirectory.AppLocalData });
624
680
  * ```
625
681
  *
682
+ * @param oldPath The path of the file or directory to rename.
683
+ * @param newPath The path the file or directory is renamed to.
684
+ * @param options Options defining the base directory of each path.
626
685
  * @since 2.0.0
627
686
  */
628
687
  declare function rename(oldPath: string | URL, newPath: string | URL, options?: RenameOptions): Promise<void>;
629
688
  /**
689
+ * Options for the `stat` and `lstat` functions, which read the metadata of a path.
690
+ *
630
691
  * @since 2.0.0
631
692
  */
632
693
  interface StatOptions {
@@ -644,6 +705,9 @@ interface StatOptions {
644
705
  * console.log(fileInfo.isFile); // true
645
706
  * ```
646
707
  *
708
+ * @param path The path of the file or directory to inspect.
709
+ * @param options Options defining the base directory of `path`.
710
+ * @returns A promise resolving to the metadata of the file or directory.
647
711
  * @since 2.0.0
648
712
  */
649
713
  declare function stat(path: string | URL, options?: StatOptions): Promise<FileInfo>;
@@ -659,10 +723,15 @@ declare function stat(path: string | URL, options?: StatOptions): Promise<FileIn
659
723
  * console.log(fileInfo.isFile); // true
660
724
  * ```
661
725
  *
726
+ * @param path The path of the file, directory or symlink to inspect.
727
+ * @param options Options defining the base directory of `path`.
728
+ * @returns A promise resolving to the metadata of the path itself.
662
729
  * @since 2.0.0
663
730
  */
664
731
  declare function lstat(path: string | URL, options?: StatOptions): Promise<FileInfo>;
665
732
  /**
733
+ * Options for the `truncate` function, which truncates or extends a file.
734
+ *
666
735
  * @since 2.0.0
667
736
  */
668
737
  interface TruncateOptions {
@@ -687,10 +756,15 @@ interface TruncateOptions {
687
756
  * console.log(data); // "Hello W"
688
757
  * ```
689
758
  *
759
+ * @param path The path of the file to truncate or extend.
760
+ * @param len The length the file is resized to, in bytes. Defaults to `0`.
761
+ * @param options Options defining the base directory of `path`.
690
762
  * @since 2.0.0
691
763
  */
692
764
  declare function truncate(path: string | URL, len?: number, options?: TruncateOptions): Promise<void>;
693
765
  /**
766
+ * Options for the `writeFile` and `writeTextFile` functions, defining how the file is opened before writing to it.
767
+ *
694
768
  * @since 2.0.0
695
769
  */
696
770
  interface WriteFileOptions {
@@ -716,22 +790,31 @@ interface WriteFileOptions {
716
790
  * await writeFile('file.txt', data, { baseDir: BaseDirectory.AppLocalData });
717
791
  * ```
718
792
  *
793
+ * @param path The path of the file to write to.
794
+ * @param data The bytes written to the file, either as a buffer or as a stream of chunks.
795
+ * @param options Options defining the base directory of `path` and how the file is opened.
719
796
  * @since 2.0.0
720
797
  */
721
798
  declare function writeFile(path: string | URL, data: Uint8Array | ReadableStream<Uint8Array>, options?: WriteFileOptions): Promise<void>;
722
799
  /**
723
- * Writes UTF-8 string `data` to the given `path`, by default creating a new file if needed, else overwriting.
724
- @example
725
- * ```typescript
726
- * import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
727
- *
728
- * await writeTextFile('file.txt', "Hello world", { baseDir: BaseDirectory.AppLocalData });
729
- * ```
730
- *
731
- * @since 2.0.0
732
- */
800
+ * Writes UTF-8 string `data` to the given `path`, by default creating a new file if needed, else overwriting.
801
+ *
802
+ * @example
803
+ * ```typescript
804
+ * import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
805
+ *
806
+ * await writeTextFile('file.txt', "Hello world", { baseDir: BaseDirectory.AppLocalData });
807
+ * ```
808
+ *
809
+ * @param path The path of the file to write to.
810
+ * @param data The UTF-8 string written to the file.
811
+ * @param options Options defining the base directory of `path` and how the file is opened.
812
+ * @since 2.0.0
813
+ */
733
814
  declare function writeTextFile(path: string | URL, data: string, options?: WriteFileOptions): Promise<void>;
734
815
  /**
816
+ * Options for the `exists` function, which checks whether a path exists.
817
+ *
735
818
  * @since 2.0.0
736
819
  */
737
820
  interface ExistsOptions {
@@ -747,10 +830,15 @@ interface ExistsOptions {
747
830
  * await exists('avatar.png', { baseDir: BaseDirectory.AppData });
748
831
  * ```
749
832
  *
833
+ * @param path The path to check.
834
+ * @param options Options defining the base directory of `path`.
835
+ * @returns A promise resolving to `true` when the path exists, `false` otherwise.
750
836
  * @since 2.0.0
751
837
  */
752
838
  declare function exists(path: string | URL, options?: ExistsOptions): Promise<boolean>;
753
839
  /**
840
+ * Options for the `watchImmediate` function, which reports file system changes as they happen.
841
+ *
754
842
  * @since 2.0.0
755
843
  */
756
844
  interface WatchOptions {
@@ -760,21 +848,33 @@ interface WatchOptions {
760
848
  baseDir?: BaseDirectory;
761
849
  }
762
850
  /**
851
+ * Options for the `watch` function, which reports file system changes after a debounce delay.
852
+ *
763
853
  * @since 2.0.0
764
854
  */
765
855
  interface DebouncedWatchOptions extends WatchOptions {
766
- /** Debounce delay */
856
+ /**
857
+ * The debounce delay in milliseconds. Changes that happen within this
858
+ * window are grouped and reported together. Defaults to `2000`.
859
+ */
767
860
  delayMs?: number;
768
861
  }
769
862
  /**
863
+ * A file system change reported to the callback of `watch` or `watchImmediate`.
864
+ *
770
865
  * @since 2.0.0
771
866
  */
772
867
  interface WatchEvent {
868
+ /** The kind of change that was detected. */
773
869
  type: WatchEventKind;
870
+ /** The paths affected by the change. */
774
871
  paths: string[];
872
+ /** Additional attributes reported by the underlying file system watcher. */
775
873
  attrs: unknown;
776
874
  }
777
875
  /**
876
+ * The kind of file system change described by a `WatchEvent`.
877
+ *
778
878
  * @since 2.0.0
779
879
  */
780
880
  type WatchEventKind = 'any' | {
@@ -787,6 +887,8 @@ type WatchEventKind = 'any' | {
787
887
  remove: WatchEventKindRemove;
788
888
  } | 'other';
789
889
  /**
890
+ * Describes how a file or directory was accessed.
891
+ *
790
892
  * @since 2.0.0
791
893
  */
792
894
  type WatchEventKindAccess = {
@@ -801,6 +903,8 @@ type WatchEventKindAccess = {
801
903
  kind: 'other';
802
904
  };
803
905
  /**
906
+ * Describes which kind of entry was created.
907
+ *
804
908
  * @since 2.0.0
805
909
  */
806
910
  type WatchEventKindCreate = {
@@ -813,6 +917,8 @@ type WatchEventKindCreate = {
813
917
  kind: 'other';
814
918
  };
815
919
  /**
920
+ * Describes what was modified on a file or directory.
921
+ *
816
922
  * @since 2.0.0
817
923
  */
818
924
  type WatchEventKindModify = {
@@ -830,6 +936,8 @@ type WatchEventKindModify = {
830
936
  kind: 'other';
831
937
  };
832
938
  /**
939
+ * Describes which kind of entry was removed.
940
+ *
833
941
  * @since 2.0.0
834
942
  */
835
943
  type WatchEventKindRemove = {
@@ -842,21 +950,74 @@ type WatchEventKindRemove = {
842
950
  kind: 'other';
843
951
  };
844
952
  /**
953
+ * Stops watching the paths it was created for. Returned by `watch` and `watchImmediate`.
954
+ *
845
955
  * @since 2.0.0
846
956
  */
847
957
  type UnwatchFn = () => void;
848
958
  /**
849
959
  * Watch changes (after a delay) on files or directories.
850
960
  *
961
+ * Events that happen within the `delayMs` window are grouped and delivered in a single callback call.
962
+ * Requires the `watch` Cargo feature of the Rust plugin to be enabled.
963
+ *
964
+ * @example
965
+ * ```typescript
966
+ * import { watch, BaseDirectory } from '@tauri-apps/plugin-fs';
967
+ *
968
+ * const unwatch = await watch(
969
+ * 'app.conf',
970
+ * (event) => console.log(event.type, event.paths),
971
+ * { baseDir: BaseDirectory.AppConfig, delayMs: 500 }
972
+ * );
973
+ *
974
+ * // stop watching when you are done
975
+ * unwatch();
976
+ * ```
977
+ *
978
+ * @param paths The path or list of paths to watch. Each path can be a string or a `file://` URL.
979
+ * @param cb The callback executed for each batch of file system changes.
980
+ * @param options Options defining the base directory of the paths, the debounce delay and whether directories are watched recursively.
981
+ * @returns A promise resolving to a function that stops watching the given paths.
851
982
  * @since 2.0.0
852
983
  */
853
984
  declare function watch(paths: string | string[] | URL | URL[], cb: (event: WatchEvent) => void, options?: DebouncedWatchOptions): Promise<UnwatchFn>;
854
985
  /**
855
986
  * Watch changes on files or directories.
856
987
  *
988
+ * Unlike `watch`, changes are reported as soon as they are detected, without a debounce delay.
989
+ * Requires the `watch` Cargo feature of the Rust plugin to be enabled.
990
+ *
991
+ * @example
992
+ * ```typescript
993
+ * import { watchImmediate, BaseDirectory } from '@tauri-apps/plugin-fs';
994
+ *
995
+ * const unwatch = await watchImmediate(
996
+ * 'logs',
997
+ * (event) => console.log(event.type, event.paths),
998
+ * { baseDir: BaseDirectory.AppLog, recursive: true }
999
+ * );
1000
+ *
1001
+ * // stop watching when you are done
1002
+ * unwatch();
1003
+ * ```
1004
+ *
1005
+ * @param paths The path or list of paths to watch. Each path can be a string or a `file://` URL.
1006
+ * @param cb The callback executed for each file system change.
1007
+ * @param options Options defining the base directory of the paths and whether directories are watched recursively.
1008
+ * @returns A promise resolving to a function that stops watching the given paths.
857
1009
  * @since 2.0.0
858
1010
  */
859
1011
  declare function watchImmediate(paths: string | string[] | URL | URL[], cb: (event: WatchEvent) => void, options?: WatchOptions): Promise<UnwatchFn>;
1012
+ /**
1013
+ * Options for the `size` function.
1014
+ *
1015
+ * @since 2.6.0
1016
+ */
1017
+ interface SizeOptions {
1018
+ /** Base directory for `path`. */
1019
+ baseDir?: BaseDirectory;
1020
+ }
860
1021
  /**
861
1022
  * Get the size of a file or directory. For files, the `stat` functions can be used as well.
862
1023
  *
@@ -870,9 +1031,12 @@ declare function watchImmediate(paths: string | string[] | URL | URL[], cb: (eve
870
1031
  * console.log(dirSize); // 1024
871
1032
  * ```
872
1033
  *
1034
+ * @param path The path of the file or directory to measure.
1035
+ * @param options Options defining the base directory of `path` (since 2.6.0).
1036
+ * @returns A promise resolving to the size in bytes.
873
1037
  * @since 2.1.0
874
1038
  */
875
- declare function size(path: string | URL): Promise<number>;
1039
+ declare function size(path: string | URL, options?: SizeOptions): Promise<number>;
876
1040
  /**
877
1041
  * Starts accessing a security-scoped resource for the given file URL.
878
1042
  * This should be called when you're accessing a file that was opened
@@ -897,6 +1061,7 @@ declare function size(path: string | URL): Promise<number>;
897
1061
  * // ... use the resource ...
898
1062
  * ```
899
1063
  *
1064
+ * @param path The path or `file://` URL of the resource to start accessing.
900
1065
  * @since 2.5.0
901
1066
  */
902
1067
  declare function startAccessingSecurityScopedResource(path: string | URL): Promise<void>;
@@ -921,8 +1086,9 @@ declare function startAccessingSecurityScopedResource(path: string | URL): Promi
921
1086
  * await stopAccessingSecurityScopedResource(filePath);
922
1087
  * ```
923
1088
  *
1089
+ * @param path The path or `file://` URL of the resource to stop accessing.
924
1090
  * @since 2.5.0
925
1091
  */
926
1092
  declare function stopAccessingSecurityScopedResource(path: string | URL): Promise<void>;
927
- export type { CreateOptions, OpenOptions, CopyFileOptions, MkdirOptions, DirEntry, ReadDirOptions, ReadFileOptions, RemoveOptions, RenameOptions, StatOptions, TruncateOptions, WriteFileOptions, ExistsOptions, FileInfo, WatchOptions, DebouncedWatchOptions, WatchEvent, WatchEventKind, WatchEventKindAccess, WatchEventKindCreate, WatchEventKindModify, WatchEventKindRemove, UnwatchFn };
1093
+ export type { CreateOptions, OpenOptions, CopyFileOptions, MkdirOptions, DirEntry, ReadDirOptions, ReadFileOptions, RemoveOptions, RenameOptions, StatOptions, TruncateOptions, WriteFileOptions, ExistsOptions, SizeOptions, FileInfo, WatchOptions, DebouncedWatchOptions, WatchEvent, WatchEventKind, WatchEventKindAccess, WatchEventKindCreate, WatchEventKindModify, WatchEventKindRemove, UnwatchFn };
928
1094
  export { BaseDirectory, FileHandle, create, open, copyFile, mkdir, readDir, readFile, readTextFile, readTextFileLines, remove, rename, SeekMode, stat, lstat, truncate, writeFile, writeTextFile, exists, watch, watchImmediate, size, startAccessingSecurityScopedResource, stopAccessingSecurityScopedResource };
package/dist-js/index.js CHANGED
@@ -86,10 +86,16 @@ import { Resource, invoke, Channel } from '@tauri-apps/api/core';
86
86
  *
87
87
  * @module
88
88
  */
89
+ /**
90
+ * Defines how the offset given to {@linkcode FileHandle.seek} is interpreted.
91
+ */
89
92
  var SeekMode;
90
93
  (function (SeekMode) {
94
+ /** The offset is relative to the start of the file. */
91
95
  SeekMode[SeekMode["Start"] = 0] = "Start";
96
+ /** The offset is relative to the current cursor position. */
92
97
  SeekMode[SeekMode["Current"] = 1] = "Current";
98
+ /** The offset is relative to the end of the file. */
93
99
  SeekMode[SeekMode["End"] = 2] = "End";
94
100
  })(SeekMode || (SeekMode = {}));
95
101
  function parseFileInfo(r) {
@@ -163,6 +169,8 @@ class FileHandle extends Resource {
163
169
  * await file.close();
164
170
  * ```
165
171
  *
172
+ * @param buffer The buffer the file contents are read into.
173
+ * @returns A promise resolving to the number of bytes read, or `null` when the end of the file was reached.
166
174
  * @since 2.0.0
167
175
  */
168
176
  async read(buffer) {
@@ -213,6 +221,9 @@ class FileHandle extends Resource {
213
221
  * await file.close();
214
222
  * ```
215
223
  *
224
+ * @param offset The number of bytes the cursor is moved by.
225
+ * @param whence Defines the position the `offset` is relative to.
226
+ * @returns A promise resolving to the new cursor position, relative to the start of the file.
216
227
  * @since 2.0.0
217
228
  */
218
229
  async seek(offset, whence) {
@@ -234,6 +245,7 @@ class FileHandle extends Resource {
234
245
  * await file.close();
235
246
  * ```
236
247
  *
248
+ * @returns A promise resolving to the metadata of this file.
237
249
  * @since 2.0.0
238
250
  */
239
251
  async stat() {
@@ -264,6 +276,7 @@ class FileHandle extends Resource {
264
276
  * await file.close();
265
277
  * ```
266
278
  *
279
+ * @param len The length the file is truncated or extended to, in bytes. When not provided the entire file contents are truncated.
267
280
  * @since 2.0.0
268
281
  */
269
282
  async truncate(len) {
@@ -290,6 +303,8 @@ class FileHandle extends Resource {
290
303
  * await file.close();
291
304
  * ```
292
305
  *
306
+ * @param data The bytes written to the file.
307
+ * @returns A promise resolving to the number of bytes written.
293
308
  * @since 2.0.0
294
309
  */
295
310
  async write(data) {
@@ -311,6 +326,9 @@ class FileHandle extends Resource {
311
326
  * await file.close();
312
327
  * ```
313
328
  *
329
+ * @param path The path of the file, relative to `options.baseDir` when it is provided.
330
+ * @param options Options defining the base directory of `path`.
331
+ * @returns A promise resolving to the handle of the created file.
314
332
  * @since 2.0.0
315
333
  */
316
334
  async function create(path, options) {
@@ -337,6 +355,9 @@ async function create(path, options) {
337
355
  * await file.close();
338
356
  * ```
339
357
  *
358
+ * @param path The path of the file, relative to `options.baseDir` when it is provided.
359
+ * @param options Options defining the base directory of `path` and how the file is opened.
360
+ * @returns A promise resolving to the handle of the open file.
340
361
  * @since 2.0.0
341
362
  */
342
363
  async function open(path, options) {
@@ -357,6 +378,9 @@ async function open(path, options) {
357
378
  * await copyFile('app.conf', 'app.conf.bk', { fromPathBaseDir: BaseDirectory.AppConfig, toPathBaseDir: BaseDirectory.AppConfig });
358
379
  * ```
359
380
  *
381
+ * @param fromPath The path of the file to copy from.
382
+ * @param toPath The path of the file to copy to.
383
+ * @param options Options defining the base directory of each path.
360
384
  * @since 2.0.0
361
385
  */
362
386
  async function copyFile(fromPath, toPath, options) {
@@ -378,6 +402,8 @@ async function copyFile(fromPath, toPath, options) {
378
402
  * await mkdir('users', { baseDir: BaseDirectory.AppLocalData });
379
403
  * ```
380
404
  *
405
+ * @param path The path of the directory to create.
406
+ * @param options Options defining the base directory of `path`, the directory permissions and whether intermediate directories are created.
381
407
  * @since 2.0.0
382
408
  */
383
409
  async function mkdir(path, options) {
@@ -395,20 +421,23 @@ async function mkdir(path, options) {
395
421
  * ```typescript
396
422
  * import { readDir, BaseDirectory } from '@tauri-apps/plugin-fs';
397
423
  * import { join } from '@tauri-apps/api/path';
398
- * const dir = "users"
399
- * const entries = await readDir('users', { baseDir: BaseDirectory.AppLocalData });
400
- * processEntriesRecursively(dir, entries);
424
+ * const dir = 'users';
425
+ * const entries = await readDir(dir, { baseDir: BaseDirectory.AppLocalData });
426
+ * await processEntriesRecursively(dir, entries);
401
427
  * async function processEntriesRecursively(parent, entries) {
402
428
  * for (const entry of entries) {
403
429
  * console.log(`Entry: ${entry.name}`);
404
430
  * if (entry.isDirectory) {
405
- * const dir = await join(parent, entry.name);
406
- * processEntriesRecursively(dir, await readDir(dir, { baseDir: BaseDirectory.AppLocalData }))
431
+ * const entryPath = await join(parent, entry.name);
432
+ * await processEntriesRecursively(entryPath, await readDir(entryPath, { baseDir: BaseDirectory.AppLocalData }));
407
433
  * }
408
434
  * }
409
435
  * }
410
436
  * ```
411
437
  *
438
+ * @param path The path of the directory to read.
439
+ * @param options Options defining the base directory of `path`.
440
+ * @returns A promise resolving to the list of entries in the directory.
412
441
  * @since 2.0.0
413
442
  */
414
443
  async function readDir(path, options) {
@@ -429,6 +458,9 @@ async function readDir(path, options) {
429
458
  * const contents = await readFile('avatar.png', { baseDir: BaseDirectory.Resource });
430
459
  * ```
431
460
  *
461
+ * @param path The path of the file to read.
462
+ * @param options Options defining the base directory of `path`.
463
+ * @returns A promise resolving to the contents of the file as bytes.
432
464
  * @since 2.0.0
433
465
  */
434
466
  async function readFile(path, options) {
@@ -449,6 +481,9 @@ async function readFile(path, options) {
449
481
  * const contents = await readTextFile('app.conf', { baseDir: BaseDirectory.AppConfig });
450
482
  * ```
451
483
  *
484
+ * @param path The path of the file to read.
485
+ * @param options Options defining the base directory of `path` and the text encoding.
486
+ * @returns A promise resolving to the contents of the file as a string.
452
487
  * @since 2.0.0
453
488
  */
454
489
  async function readTextFile(path, options) {
@@ -475,6 +510,9 @@ async function readTextFile(path, options) {
475
510
  * You could also call {@linkcode AsyncIterableIterator.next} to advance the
476
511
  * iterator so you can lazily read the next line whenever you want.
477
512
  *
513
+ * @param path The path of the file to read.
514
+ * @param options Options defining the base directory of `path` and the text encoding.
515
+ * @returns A promise resolving to an iterator over the lines of the file.
478
516
  * @since 2.0.0
479
517
  */
480
518
  async function readTextFileLines(path, options) {
@@ -495,7 +533,15 @@ async function readTextFileLines(path, options) {
495
533
  options: options != null ? { ...options, encoding } : undefined
496
534
  });
497
535
  }
498
- const arr = await invoke('plugin:fs|read_text_file_lines_next', { rid: this.rid });
536
+ let arr;
537
+ try {
538
+ arr = await invoke('plugin:fs|read_text_file_lines_next', { rid: this.rid });
539
+ }
540
+ catch (error) {
541
+ // the resource is closed on errors, the next iteration starts over
542
+ this.rid = null;
543
+ throw error;
544
+ }
499
545
  const bytes = arr instanceof ArrayBuffer ? new Uint8Array(arr) : Uint8Array.from(arr);
500
546
  // Rust side will never return an empty array for this command and
501
547
  // ensure there is at least one elements there.
@@ -514,6 +560,16 @@ async function readTextFileLines(path, options) {
514
560
  done
515
561
  };
516
562
  },
563
+ // called when a `for await` loop exits early (`break`, `return` or `throw`)
564
+ async return() {
565
+ if (this.rid !== null) {
566
+ const rid = this.rid;
567
+ this.rid = null;
568
+ // close the file, otherwise it stays open until the webview is destroyed
569
+ await new Resource(rid).close();
570
+ }
571
+ return { value: null, done: true };
572
+ },
517
573
  [Symbol.asyncIterator]() {
518
574
  return this;
519
575
  }
@@ -529,6 +585,8 @@ async function readTextFileLines(path, options) {
529
585
  * await remove('users', { baseDir: BaseDirectory.AppLocalData });
530
586
  * ```
531
587
  *
588
+ * @param path The path of the file or directory to remove.
589
+ * @param options Options defining the base directory of `path` and whether directories are removed recursively.
532
590
  * @since 2.0.0
533
591
  */
534
592
  async function remove(path, options) {
@@ -553,6 +611,9 @@ async function remove(path, options) {
553
611
  * await rename('avatar.png', 'deleted.png', { oldPathBaseDir: BaseDirectory.App, newPathBaseDir: BaseDirectory.AppLocalData });
554
612
  * ```
555
613
  *
614
+ * @param oldPath The path of the file or directory to rename.
615
+ * @param newPath The path the file or directory is renamed to.
616
+ * @param options Options defining the base directory of each path.
556
617
  * @since 2.0.0
557
618
  */
558
619
  async function rename(oldPath, newPath, options) {
@@ -577,6 +638,9 @@ async function rename(oldPath, newPath, options) {
577
638
  * console.log(fileInfo.isFile); // true
578
639
  * ```
579
640
  *
641
+ * @param path The path of the file or directory to inspect.
642
+ * @param options Options defining the base directory of `path`.
643
+ * @returns A promise resolving to the metadata of the file or directory.
580
644
  * @since 2.0.0
581
645
  */
582
646
  async function stat(path, options) {
@@ -598,6 +662,9 @@ async function stat(path, options) {
598
662
  * console.log(fileInfo.isFile); // true
599
663
  * ```
600
664
  *
665
+ * @param path The path of the file, directory or symlink to inspect.
666
+ * @param options Options defining the base directory of `path`.
667
+ * @returns A promise resolving to the metadata of the path itself.
601
668
  * @since 2.0.0
602
669
  */
603
670
  async function lstat(path, options) {
@@ -625,6 +692,9 @@ async function lstat(path, options) {
625
692
  * console.log(data); // "Hello W"
626
693
  * ```
627
694
  *
695
+ * @param path The path of the file to truncate or extend.
696
+ * @param len The length the file is resized to, in bytes. Defaults to `0`.
697
+ * @param options Options defining the base directory of `path`.
628
698
  * @since 2.0.0
629
699
  */
630
700
  async function truncate(path, len, options) {
@@ -648,6 +718,9 @@ async function truncate(path, len, options) {
648
718
  * await writeFile('file.txt', data, { baseDir: BaseDirectory.AppLocalData });
649
719
  * ```
650
720
  *
721
+ * @param path The path of the file to write to.
722
+ * @param data The bytes written to the file, either as a buffer or as a stream of chunks.
723
+ * @param options Options defining the base directory of `path` and how the file is opened.
651
724
  * @since 2.0.0
652
725
  */
653
726
  async function writeFile(path, data, options) {
@@ -659,7 +732,9 @@ async function writeFile(path, data, options) {
659
732
  read: false,
660
733
  create: true,
661
734
  write: true,
662
- ...options
735
+ ...options,
736
+ // overwrite the previous contents, like when writing a buffer
737
+ truncate: !options?.append
663
738
  });
664
739
  const reader = data.getReader();
665
740
  try {
@@ -685,16 +760,20 @@ async function writeFile(path, data, options) {
685
760
  }
686
761
  }
687
762
  /**
688
- * Writes UTF-8 string `data` to the given `path`, by default creating a new file if needed, else overwriting.
689
- @example
690
- * ```typescript
691
- * import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
692
- *
693
- * await writeTextFile('file.txt', "Hello world", { baseDir: BaseDirectory.AppLocalData });
694
- * ```
695
- *
696
- * @since 2.0.0
697
- */
763
+ * Writes UTF-8 string `data` to the given `path`, by default creating a new file if needed, else overwriting.
764
+ *
765
+ * @example
766
+ * ```typescript
767
+ * import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
768
+ *
769
+ * await writeTextFile('file.txt', "Hello world", { baseDir: BaseDirectory.AppLocalData });
770
+ * ```
771
+ *
772
+ * @param path The path of the file to write to.
773
+ * @param data The UTF-8 string written to the file.
774
+ * @param options Options defining the base directory of `path` and how the file is opened.
775
+ * @since 2.0.0
776
+ */
698
777
  async function writeTextFile(path, data, options) {
699
778
  if (path instanceof URL && path.protocol !== 'file:') {
700
779
  throw new TypeError('Must be a file URL.');
@@ -716,6 +795,9 @@ async function writeTextFile(path, data, options) {
716
795
  * await exists('avatar.png', { baseDir: BaseDirectory.AppData });
717
796
  * ```
718
797
  *
798
+ * @param path The path to check.
799
+ * @param options Options defining the base directory of `path`.
800
+ * @returns A promise resolving to `true` when the path exists, `false` otherwise.
719
801
  * @since 2.0.0
720
802
  */
721
803
  async function exists(path, options) {
@@ -752,6 +834,27 @@ async function watchInternal(paths, cb, options) {
752
834
  /**
753
835
  * Watch changes (after a delay) on files or directories.
754
836
  *
837
+ * Events that happen within the `delayMs` window are grouped and delivered in a single callback call.
838
+ * Requires the `watch` Cargo feature of the Rust plugin to be enabled.
839
+ *
840
+ * @example
841
+ * ```typescript
842
+ * import { watch, BaseDirectory } from '@tauri-apps/plugin-fs';
843
+ *
844
+ * const unwatch = await watch(
845
+ * 'app.conf',
846
+ * (event) => console.log(event.type, event.paths),
847
+ * { baseDir: BaseDirectory.AppConfig, delayMs: 500 }
848
+ * );
849
+ *
850
+ * // stop watching when you are done
851
+ * unwatch();
852
+ * ```
853
+ *
854
+ * @param paths The path or list of paths to watch. Each path can be a string or a `file://` URL.
855
+ * @param cb The callback executed for each batch of file system changes.
856
+ * @param options Options defining the base directory of the paths, the debounce delay and whether directories are watched recursively.
857
+ * @returns A promise resolving to a function that stops watching the given paths.
755
858
  * @since 2.0.0
756
859
  */
757
860
  async function watch(paths, cb, options) {
@@ -764,6 +867,27 @@ async function watch(paths, cb, options) {
764
867
  /**
765
868
  * Watch changes on files or directories.
766
869
  *
870
+ * Unlike `watch`, changes are reported as soon as they are detected, without a debounce delay.
871
+ * Requires the `watch` Cargo feature of the Rust plugin to be enabled.
872
+ *
873
+ * @example
874
+ * ```typescript
875
+ * import { watchImmediate, BaseDirectory } from '@tauri-apps/plugin-fs';
876
+ *
877
+ * const unwatch = await watchImmediate(
878
+ * 'logs',
879
+ * (event) => console.log(event.type, event.paths),
880
+ * { baseDir: BaseDirectory.AppLog, recursive: true }
881
+ * );
882
+ *
883
+ * // stop watching when you are done
884
+ * unwatch();
885
+ * ```
886
+ *
887
+ * @param paths The path or list of paths to watch. Each path can be a string or a `file://` URL.
888
+ * @param cb The callback executed for each file system change.
889
+ * @param options Options defining the base directory of the paths and whether directories are watched recursively.
890
+ * @returns A promise resolving to a function that stops watching the given paths.
767
891
  * @since 2.0.0
768
892
  */
769
893
  async function watchImmediate(paths, cb, options) {
@@ -785,14 +909,18 @@ async function watchImmediate(paths, cb, options) {
785
909
  * console.log(dirSize); // 1024
786
910
  * ```
787
911
  *
912
+ * @param path The path of the file or directory to measure.
913
+ * @param options Options defining the base directory of `path` (since 2.6.0).
914
+ * @returns A promise resolving to the size in bytes.
788
915
  * @since 2.1.0
789
916
  */
790
- async function size(path) {
917
+ async function size(path, options) {
791
918
  if (path instanceof URL && path.protocol !== 'file:') {
792
919
  throw new TypeError('Must be a file URL.');
793
920
  }
794
921
  return await invoke('plugin:fs|size', {
795
- path: path instanceof URL ? path.toString() : path
922
+ path: path instanceof URL ? path.toString() : path,
923
+ options
796
924
  });
797
925
  }
798
926
  /**
@@ -819,6 +947,7 @@ async function size(path) {
819
947
  * // ... use the resource ...
820
948
  * ```
821
949
  *
950
+ * @param path The path or `file://` URL of the resource to start accessing.
822
951
  * @since 2.5.0
823
952
  */
824
953
  async function startAccessingSecurityScopedResource(path) {
@@ -850,6 +979,7 @@ async function startAccessingSecurityScopedResource(path) {
850
979
  * await stopAccessingSecurityScopedResource(filePath);
851
980
  * ```
852
981
  *
982
+ * @param path The path or `file://` URL of the resource to stop accessing.
853
983
  * @since 2.5.0
854
984
  */
855
985
  async function stopAccessingSecurityScopedResource(path) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tauri-apps/plugin-fs",
3
- "version": "2.5.2",
3
+ "version": "2.7.0",
4
4
  "description": "Access the file system.",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "authors": [
@@ -25,6 +25,6 @@
25
25
  "LICENSE"
26
26
  ],
27
27
  "dependencies": {
28
- "@tauri-apps/api": "^2.11.0"
28
+ "@tauri-apps/api": "^2.12.0"
29
29
  }
30
30
  }