@tauri-apps/plugin-fs 3.0.0-alpha.1 → 3.0.0-alpha.3

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/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,17 +481,13 @@ 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) {
455
- if (path instanceof URL && path.protocol !== 'file:') {
456
- throw new TypeError('Must be a file URL.');
457
- }
458
- const arr = await invoke('plugin:fs|read_text_file', {
459
- path: path instanceof URL ? path.toString() : path,
460
- options
461
- });
462
- const bytes = arr instanceof ArrayBuffer ? arr : Uint8Array.from(arr);
490
+ const bytes = await readFile(path, options);
463
491
  return new TextDecoder(options?.encoding ?? 'utf-8').decode(bytes);
464
492
  }
465
493
  /**
@@ -475,6 +503,9 @@ async function readTextFile(path, options) {
475
503
  * You could also call {@linkcode AsyncIterableIterator.next} to advance the
476
504
  * iterator so you can lazily read the next line whenever you want.
477
505
  *
506
+ * @param path The path of the file to read.
507
+ * @param options Options defining the base directory of `path` and the text encoding.
508
+ * @returns A promise resolving to an iterator over the lines of the file.
478
509
  * @since 2.0.0
479
510
  */
480
511
  async function readTextFileLines(path, options) {
@@ -495,7 +526,15 @@ async function readTextFileLines(path, options) {
495
526
  options: options != null ? { ...options, encoding } : undefined
496
527
  });
497
528
  }
498
- const arr = await invoke('plugin:fs|read_text_file_lines_next', { rid: this.rid });
529
+ let arr;
530
+ try {
531
+ arr = await invoke('plugin:fs|read_text_file_lines_next', { rid: this.rid });
532
+ }
533
+ catch (error) {
534
+ // the resource is closed on errors, the next iteration starts over
535
+ this.rid = null;
536
+ throw error;
537
+ }
499
538
  const bytes = arr instanceof ArrayBuffer ? new Uint8Array(arr) : Uint8Array.from(arr);
500
539
  // Rust side will never return an empty array for this command and
501
540
  // ensure there is at least one elements there.
@@ -514,6 +553,16 @@ async function readTextFileLines(path, options) {
514
553
  done
515
554
  };
516
555
  },
556
+ // called when a `for await` loop exits early (`break`, `return` or `throw`)
557
+ async return() {
558
+ if (this.rid !== null) {
559
+ const rid = this.rid;
560
+ this.rid = null;
561
+ // close the file, otherwise it stays open until the webview is destroyed
562
+ await new Resource(rid).close();
563
+ }
564
+ return { value: null, done: true };
565
+ },
517
566
  [Symbol.asyncIterator]() {
518
567
  return this;
519
568
  }
@@ -529,6 +578,8 @@ async function readTextFileLines(path, options) {
529
578
  * await remove('users', { baseDir: BaseDirectory.AppLocalData });
530
579
  * ```
531
580
  *
581
+ * @param path The path of the file or directory to remove.
582
+ * @param options Options defining the base directory of `path` and whether directories are removed recursively.
532
583
  * @since 2.0.0
533
584
  */
534
585
  async function remove(path, options) {
@@ -553,6 +604,9 @@ async function remove(path, options) {
553
604
  * await rename('avatar.png', 'deleted.png', { oldPathBaseDir: BaseDirectory.App, newPathBaseDir: BaseDirectory.AppLocalData });
554
605
  * ```
555
606
  *
607
+ * @param oldPath The path of the file or directory to rename.
608
+ * @param newPath The path the file or directory is renamed to.
609
+ * @param options Options defining the base directory of each path.
556
610
  * @since 2.0.0
557
611
  */
558
612
  async function rename(oldPath, newPath, options) {
@@ -577,6 +631,9 @@ async function rename(oldPath, newPath, options) {
577
631
  * console.log(fileInfo.isFile); // true
578
632
  * ```
579
633
  *
634
+ * @param path The path of the file or directory to inspect.
635
+ * @param options Options defining the base directory of `path`.
636
+ * @returns A promise resolving to the metadata of the file or directory.
580
637
  * @since 2.0.0
581
638
  */
582
639
  async function stat(path, options) {
@@ -598,6 +655,9 @@ async function stat(path, options) {
598
655
  * console.log(fileInfo.isFile); // true
599
656
  * ```
600
657
  *
658
+ * @param path The path of the file, directory or symlink to inspect.
659
+ * @param options Options defining the base directory of `path`.
660
+ * @returns A promise resolving to the metadata of the path itself.
601
661
  * @since 2.0.0
602
662
  */
603
663
  async function lstat(path, options) {
@@ -625,6 +685,9 @@ async function lstat(path, options) {
625
685
  * console.log(data); // "Hello W"
626
686
  * ```
627
687
  *
688
+ * @param path The path of the file to truncate or extend.
689
+ * @param len The length the file is resized to, in bytes. Defaults to `0`.
690
+ * @param options Options defining the base directory of `path`.
628
691
  * @since 2.0.0
629
692
  */
630
693
  async function truncate(path, len, options) {
@@ -648,6 +711,9 @@ async function truncate(path, len, options) {
648
711
  * await writeFile('file.txt', data, { baseDir: BaseDirectory.AppLocalData });
649
712
  * ```
650
713
  *
714
+ * @param path The path of the file to write to.
715
+ * @param data The bytes written to the file, either as a buffer or as a stream of chunks.
716
+ * @param options Options defining the base directory of `path` and how the file is opened.
651
717
  * @since 2.0.0
652
718
  */
653
719
  async function writeFile(path, data, options) {
@@ -659,7 +725,9 @@ async function writeFile(path, data, options) {
659
725
  read: false,
660
726
  create: true,
661
727
  write: true,
662
- ...options
728
+ ...options,
729
+ // overwrite the previous contents, like when writing a buffer
730
+ truncate: !options?.append
663
731
  });
664
732
  const reader = data.getReader();
665
733
  try {
@@ -685,27 +753,22 @@ async function writeFile(path, data, options) {
685
753
  }
686
754
  }
687
755
  /**
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
- */
756
+ * Writes UTF-8 string `data` to the given `path`, by default creating a new file if needed, else overwriting.
757
+ *
758
+ * @example
759
+ * ```typescript
760
+ * import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
761
+ *
762
+ * await writeTextFile('file.txt', "Hello world", { baseDir: BaseDirectory.AppLocalData });
763
+ * ```
764
+ *
765
+ * @param path The path of the file to write to.
766
+ * @param data The UTF-8 string written to the file.
767
+ * @param options Options defining the base directory of `path` and how the file is opened.
768
+ * @since 2.0.0
769
+ */
698
770
  async function writeTextFile(path, data, options) {
699
- if (path instanceof URL && path.protocol !== 'file:') {
700
- throw new TypeError('Must be a file URL.');
701
- }
702
- const encoder = new TextEncoder();
703
- await invoke('plugin:fs|write_text_file', encoder.encode(data), {
704
- headers: {
705
- path: encodeURIComponent(path instanceof URL ? path.toString() : path),
706
- options: JSON.stringify(options)
707
- }
708
- });
771
+ await writeFile(path, new TextEncoder().encode(data), options);
709
772
  }
710
773
  /**
711
774
  * Check if a path exists.
@@ -716,6 +779,9 @@ async function writeTextFile(path, data, options) {
716
779
  * await exists('avatar.png', { baseDir: BaseDirectory.AppData });
717
780
  * ```
718
781
  *
782
+ * @param path The path to check.
783
+ * @param options Options defining the base directory of `path`.
784
+ * @returns A promise resolving to `true` when the path exists, `false` otherwise.
719
785
  * @since 2.0.0
720
786
  */
721
787
  async function exists(path, options) {
@@ -727,6 +793,11 @@ async function exists(path, options) {
727
793
  options
728
794
  });
729
795
  }
796
+ /**
797
+ * A file system watcher. Call {@linkcode Watcher.close} to stop watching.
798
+ *
799
+ * @since 3.0.0
800
+ */
730
801
  class Watcher extends Resource {
731
802
  }
732
803
  async function watchInternal(paths, cb, options) {
@@ -743,15 +814,19 @@ async function watchInternal(paths, cb, options) {
743
814
  options,
744
815
  onEvent
745
816
  });
746
- const watcher = new Watcher(rid);
747
- return () => {
748
- void watcher.close();
749
- };
817
+ return new Watcher(rid);
750
818
  }
751
- // TODO: Return `Watcher` instead in v3
752
819
  /**
753
820
  * Watch changes (after a delay) on files or directories.
754
821
  *
822
+ * @example
823
+ * ```typescript
824
+ * import { watch, BaseDirectory } from '@tauri-apps/plugin-fs';
825
+ * const watcher = await watch('app.conf', (event) => console.log(event), { baseDir: BaseDirectory.AppConfig });
826
+ * // when you're done watching:
827
+ * await watcher.close();
828
+ * ```
829
+ *
755
830
  * @since 2.0.0
756
831
  */
757
832
  async function watch(paths, cb, options) {
@@ -760,10 +835,17 @@ async function watch(paths, cb, options) {
760
835
  ...options
761
836
  });
762
837
  }
763
- // TODO: Return `Watcher` instead in v3
764
838
  /**
765
839
  * Watch changes on files or directories.
766
840
  *
841
+ * @example
842
+ * ```typescript
843
+ * import { watchImmediate, BaseDirectory } from '@tauri-apps/plugin-fs';
844
+ * const watcher = await watchImmediate('app.conf', (event) => console.log(event), { baseDir: BaseDirectory.AppConfig });
845
+ * // when you're done watching:
846
+ * await watcher.close();
847
+ * ```
848
+ *
767
849
  * @since 2.0.0
768
850
  */
769
851
  async function watchImmediate(paths, cb, options) {
@@ -785,14 +867,18 @@ async function watchImmediate(paths, cb, options) {
785
867
  * console.log(dirSize); // 1024
786
868
  * ```
787
869
  *
870
+ * @param path The path of the file or directory to measure.
871
+ * @param options Options defining the base directory of `path` (since 2.6.0).
872
+ * @returns A promise resolving to the size in bytes.
788
873
  * @since 2.1.0
789
874
  */
790
- async function size(path) {
875
+ async function size(path, options) {
791
876
  if (path instanceof URL && path.protocol !== 'file:') {
792
877
  throw new TypeError('Must be a file URL.');
793
878
  }
794
879
  return await invoke('plugin:fs|size', {
795
- path: path instanceof URL ? path.toString() : path
880
+ path: path instanceof URL ? path.toString() : path,
881
+ options
796
882
  });
797
883
  }
798
884
  /**
@@ -819,6 +905,7 @@ async function size(path) {
819
905
  * // ... use the resource ...
820
906
  * ```
821
907
  *
908
+ * @param path The path or `file://` URL of the resource to start accessing.
822
909
  * @since 2.5.0
823
910
  */
824
911
  async function startAccessingSecurityScopedResource(path) {
@@ -850,6 +937,7 @@ async function startAccessingSecurityScopedResource(path) {
850
937
  * await stopAccessingSecurityScopedResource(filePath);
851
938
  * ```
852
939
  *
940
+ * @param path The path or `file://` URL of the resource to stop accessing.
853
941
  * @since 2.5.0
854
942
  */
855
943
  async function stopAccessingSecurityScopedResource(path) {
@@ -861,4 +949,4 @@ async function stopAccessingSecurityScopedResource(path) {
861
949
  });
862
950
  }
863
951
 
864
- export { FileHandle, SeekMode, copyFile, create, exists, lstat, mkdir, open, readDir, readFile, readTextFile, readTextFileLines, remove, rename, size, startAccessingSecurityScopedResource, stat, stopAccessingSecurityScopedResource, truncate, watch, watchImmediate, writeFile, writeTextFile };
952
+ export { FileHandle, SeekMode, Watcher, copyFile, create, exists, lstat, mkdir, open, readDir, readFile, readTextFile, readTextFileLines, remove, rename, size, startAccessingSecurityScopedResource, stat, stopAccessingSecurityScopedResource, truncate, watch, watchImmediate, writeFile, writeTextFile };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tauri-apps/plugin-fs",
3
- "version": "3.0.0-alpha.1",
3
+ "version": "3.0.0-alpha.3",
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": "^3.0.0-alpha.1"
28
+ "@tauri-apps/api": "^3.0.0-alpha.2"
29
29
  }
30
30
  }