@tauri-apps/plugin-fs 2.0.0-alpha.4 → 2.0.0-alpha.5

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.
@@ -12,7 +12,7 @@
12
12
  *
13
13
  * The scope configuration is an array of glob patterns describing folder paths that are allowed.
14
14
  * For instance, this scope configuration only allows accessing files on the
15
- * *databases* folder of the {@link path.appDataDir | $APPDATA directory}:
15
+ * *databases* folder of the {@link https://beta.tauri.app/2/reference/js/core/namespacepath/#appdatadir | `$APPDATA` directory}:
16
16
  * ```json
17
17
  * {
18
18
  * "plugins": {
@@ -23,16 +23,32 @@
23
23
  * }
24
24
  * ```
25
25
  *
26
- * Notice the use of the `$APPDATA` variable. The value is injected at runtime, resolving to the {@link path.appDataDir | app data directory}.
26
+ * Notice the use of the `$APPDATA` variable. The value is injected at runtime, resolving to the {@link https://beta.tauri.app/2/reference/js/core/namespacepath/#appdatadir | app data directory}.
27
+ *
27
28
  * The available variables are:
28
- * {@link path.appConfigDir | `$APPCONFIG`}, {@link path.appDataDir | `$APPDATA`}, {@link path.appLocalDataDir | `$APPLOCALDATA`},
29
- * {@link path.appCacheDir | `$APPCACHE`}, {@link path.appLogDir | `$APPLOG`},
30
- * {@link path.audioDir | `$AUDIO`}, {@link path.cacheDir | `$CACHE`}, {@link path.configDir | `$CONFIG`}, {@link path.dataDir | `$DATA`},
31
- * {@link path.localDataDir | `$LOCALDATA`}, {@link path.desktopDir | `$DESKTOP`}, {@link path.documentDir | `$DOCUMENT`},
32
- * {@link path.downloadDir | `$DOWNLOAD`}, {@link path.executableDir | `$EXE`}, {@link path.fontDir | `$FONT`}, {@link path.homeDir | `$HOME`},
33
- * {@link path.pictureDir | `$PICTURE`}, {@link path.publicDir | `$PUBLIC`}, {@link path.runtimeDir | `$RUNTIME`},
34
- * {@link path.templateDir | `$TEMPLATE`}, {@link path.videoDir | `$VIDEO`}, {@link path.resourceDir | `$RESOURCE`},
35
- * {@link os.tempdir | `$TEMP`}.
29
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#appconfigdir | $APPCONFIG},
30
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#appdatadir | $APPDATA},
31
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#appLocaldatadir | $APPLOCALDATA},
32
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#appcachedir | $APPCACHE},
33
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#applogdir | $APPLOG},
34
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#audiodir | $AUDIO},
35
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#cachedir | $CACHE},
36
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#configdir | $CONFIG},
37
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#datadir | $DATA},
38
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#localdatadir | $LOCALDATA},
39
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#desktopdir | $DESKTOP},
40
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#documentdir | $DOCUMENT},
41
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#downloaddir | $DOWNLOAD},
42
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#executabledir | $EXE},
43
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#fontdir | $FONT},
44
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#homedir | $HOME},
45
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#picturedir | $PICTURE},
46
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#publicdir | $PUBLIC},
47
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#runtimedir | $RUNTIME},
48
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#templatedir | $TEMPLATE},
49
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#videodir | $VIDEO},
50
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#resourcedir | $RESOURCE},
51
+ * {@linkcode https://beta.tauri.app/2/reference/js/core/namespacepath/#tempdir | $TEMP}.
36
52
  *
37
53
  * Trying to execute any API with a URL not configured on the scope results in a promise rejection due to denied access.
38
54
  *
@@ -41,314 +57,653 @@
41
57
  * @module
42
58
  */
43
59
  import { BaseDirectory } from "@tauri-apps/api/path";
44
- interface Permissions {
45
- /**
46
- * `true` if these permissions describe a readonly (unwritable) file.
47
- */
48
- readonly: boolean;
49
- /**
50
- * The underlying raw `st_mode` bits that contain the standard Unix permissions for this file.
51
- */
52
- mode: number | undefined;
60
+ import { Resource } from "@tauri-apps/api/core";
61
+ declare enum SeekMode {
62
+ Start = 0,
63
+ Current = 1,
64
+ End = 2
53
65
  }
54
66
  /**
55
- * Metadata information about a file.
56
- * This structure is returned from the `metadata` function or method
57
- * and represents known metadata about a file such as its permissions, size, modification times, etc.
67
+ * A FileInfo describes a file and is returned by `stat`, `lstat` or `fstat`.
68
+ *
69
+ * @since 2.0.0
58
70
  */
59
- interface Metadata {
71
+ interface FileInfo {
60
72
  /**
61
- * The last access time of this metadata.
73
+ * True if this is info for a regular file. Mutually exclusive to
74
+ * `FileInfo.isDirectory` and `FileInfo.isSymlink`.
62
75
  */
63
- accessedAt: Date;
76
+ isFile: boolean;
64
77
  /**
65
- * The creation time listed in this metadata.
78
+ * True if this is info for a regular directory. Mutually exclusive to
79
+ * `FileInfo.isFile` and `FileInfo.isSymlink`.
66
80
  */
67
- createdAt: Date;
81
+ isDirectory: boolean;
68
82
  /**
69
- * The last modification time listed in this metadata.
83
+ * True if this is info for a symlink. Mutually exclusive to
84
+ * `FileInfo.isFile` and `FileInfo.isDirectory`.
70
85
  */
71
- modifiedAt: Date;
86
+ isSymlink: boolean;
72
87
  /**
73
- * `true` if this metadata is for a directory.
88
+ * The size of the file, in bytes.
74
89
  */
75
- isDir: boolean;
90
+ size: number;
76
91
  /**
77
- * `true` if this metadata is for a regular file.
92
+ * The last modification time of the file. This corresponds to the `mtime`
93
+ * field from `stat` on Linux/Mac OS and `ftLastWriteTime` on Windows. This
94
+ * may not be available on all platforms.
78
95
  */
79
- isFile: boolean;
96
+ mtime: Date | null;
80
97
  /**
81
- * `true` if this metadata is for a symbolic link.
98
+ * The last access time of the file. This corresponds to the `atime`
99
+ * field from `stat` on Unix and `ftLastAccessTime` on Windows. This may not
100
+ * be available on all platforms.
82
101
  */
83
- isSymlink: boolean;
102
+ atime: Date | null;
84
103
  /**
85
- * The size of the file, in bytes, this metadata is for.
104
+ * The creation time of the file. This corresponds to the `birthtime`
105
+ * field from `stat` on Mac/BSD and `ftCreationTime` on Windows. This may
106
+ * not be available on all platforms.
86
107
  */
87
- size: number;
108
+ birthtime: Date | null;
109
+ /** Whether this is a readonly (unwritable) file. */
110
+ readonly: boolean;
88
111
  /**
89
- * The permissions of the file this metadata is for.
112
+ * This field contains the file system attribute information for a file
113
+ * or directory. For possible values and their descriptions, see
114
+ * {@link https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants | File Attribute Constants} in the Windows Dev Center
115
+ *
116
+ * #### Platform-specific
117
+ *
118
+ * - **macOS / Linux / Android / iOS:** Unsupported.
90
119
  */
91
- permissions: Permissions;
120
+ fileAttributes: number | null;
92
121
  /**
93
- * The ID of the device containing the file. Only available on Unix.
122
+ * ID of the device containing the file.
123
+ *
124
+ * #### Platform-specific
125
+ *
126
+ * - **Windows:** Unsupported.
94
127
  */
95
- dev: number | undefined;
128
+ dev: number | null;
96
129
  /**
97
- * The inode number. Only available on Unix.
130
+ * Inode number.
131
+ *
132
+ * #### Platform-specific
133
+ *
134
+ * - **Windows:** Unsupported.
98
135
  */
99
- ino: number | undefined;
136
+ ino: number | null;
100
137
  /**
101
- * The rights applied to this file. Only available on Unix.
138
+ * The underlying raw `st_mode` bits that contain the standard Unix
139
+ * permissions for this file/directory.
140
+ *
141
+ * #### Platform-specific
142
+ *
143
+ * - **Windows:** Unsupported.
102
144
  */
103
- mode: number | undefined;
145
+ mode: number | null;
104
146
  /**
105
- * The number of hard links pointing to this file. Only available on Unix.
147
+ * Number of hard links pointing to this file.
148
+ *
149
+ * #### Platform-specific
150
+ *
151
+ * - **Windows:** Unsupported.
106
152
  */
107
- nlink: number | undefined;
153
+ nlink: number | null;
108
154
  /**
109
- * The user ID of the owner of this file. Only available on Unix.
155
+ * User ID of the owner of this file.
156
+ *
157
+ * #### Platform-specific
158
+ *
159
+ * - **Windows:** Unsupported.
110
160
  */
111
- uid: number | undefined;
161
+ uid: number | null;
112
162
  /**
113
- * The group ID of the owner of this file. Only available on Unix.
163
+ * Group ID of the owner of this file.
164
+ *
165
+ * #### Platform-specific
166
+ *
167
+ * - **Windows:** Unsupported.
114
168
  */
115
- gid: number | undefined;
169
+ gid: number | null;
116
170
  /**
117
- * The device ID of this file (if it is a special one). Only available on Unix.
171
+ * Device ID of this file.
172
+ *
173
+ * #### Platform-specific
174
+ *
175
+ * - **Windows:** Unsupported.
118
176
  */
119
- rdev: number | undefined;
177
+ rdev: number | null;
120
178
  /**
121
- * The block size for filesystem I/O. Only available on Unix.
179
+ * Blocksize for filesystem I/O.
180
+ *
181
+ * #### Platform-specific
182
+ *
183
+ * - **Windows:** Unsupported.
122
184
  */
123
- blksize: number | undefined;
185
+ blksize: number | null;
124
186
  /**
125
- * The number of blocks allocated to the file, in 512-byte units. Only available on Unix.
187
+ * Number of blocks allocated to the file, in 512-byte units.
188
+ *
189
+ * #### Platform-specific
190
+ *
191
+ * - **Windows:** Unsupported.
126
192
  */
127
- blocks: number | undefined;
193
+ blocks: number | null;
128
194
  }
129
195
  /**
196
+ * The Tauri abstraction for reading and writing files.
197
+ *
130
198
  * @since 2.0.0
131
199
  */
132
- interface FsOptions {
133
- dir?: BaseDirectory;
200
+ declare class FileHandle extends Resource {
201
+ constructor(rid: number);
202
+ /**
203
+ * Reads up to `p.byteLength` bytes into `p`. It resolves to the number of
204
+ * bytes read (`0` < `n` <= `p.byteLength`) and rejects if any error
205
+ * encountered. Even if `read()` resolves to `n` < `p.byteLength`, it may
206
+ * use all of `p` as scratch space during the call. If some data is
207
+ * available but not `p.byteLength` bytes, `read()` conventionally resolves
208
+ * to what is available instead of waiting for more.
209
+ *
210
+ * When `read()` encounters end-of-file condition, it resolves to EOF
211
+ * (`null`).
212
+ *
213
+ * When `read()` encounters an error, it rejects with an error.
214
+ *
215
+ * Callers should always process the `n` > `0` bytes returned before
216
+ * considering the EOF (`null`). Doing so correctly handles I/O errors that
217
+ * happen after reading some bytes and also both of the allowed EOF
218
+ * behaviors.
219
+ *
220
+ * @example
221
+ * ```typescript
222
+ * import { open, read, close, BaseDirectory } from "@tauri-apps/plugin-fs"
223
+ * // if "$APP/foo/bar.txt" contains the text "hello world":
224
+ * const file = await open("foo/bar.txt", { dir: BaseDirectory.App });
225
+ * const buf = new Uint8Array(100);
226
+ * const numberOfBytesRead = await file.read(buf); // 11 bytes
227
+ * const text = new TextDecoder().decode(buf); // "hello world"
228
+ * await close(file.rid);
229
+ * ```
230
+ *
231
+ * @since 2.0.0
232
+ */
233
+ read(buffer: Uint8Array): Promise<number | null>;
234
+ /**
235
+ * Seek sets the offset for the next `read()` or `write()` to offset,
236
+ * interpreted according to `whence`: `Start` means relative to the
237
+ * start of the file, `Current` means relative to the current offset,
238
+ * and `End` means relative to the end. Seek resolves to the new offset
239
+ * relative to the start of the file.
240
+ *
241
+ * Seeking to an offset before the start of the file is an error. Seeking to
242
+ * any positive offset is legal, but the behavior of subsequent I/O
243
+ * operations on the underlying object is implementation-dependent.
244
+ * It returns the number of cursor position.
245
+ *
246
+ * @example
247
+ * ```typescript
248
+ * import { open, seek, write, SeekMode, BaseDirectory } from '@tauri-apps/plugin-fs';
249
+ *
250
+ * // Given hello.txt pointing to file with "Hello world", which is 11 bytes long:
251
+ * const file = await open('hello.txt', { read: true, write: true, truncate: true, create: true, dir: BaseDirectory.App });
252
+ * await file.write(new TextEncoder().encode("Hello world"), { dir: BaseDirectory.App });
253
+ *
254
+ * // Seek 6 bytes from the start of the file
255
+ * console.log(await file.seek(6, SeekMode.Start)); // "6"
256
+ * // Seek 2 more bytes from the current position
257
+ * console.log(await file.seek(2, SeekMode.Current)); // "8"
258
+ * // Seek backwards 2 bytes from the end of the file
259
+ * console.log(await file.seek(-2, SeekMode.End)); // "9" (e.g. 11-2)
260
+ * ```
261
+ *
262
+ * @since 2.0.0
263
+ */
264
+ seek(offset: number, whence: SeekMode): Promise<number>;
265
+ /**
266
+ * Returns a {@linkcode FileInfo } for this file.
267
+ *
268
+ * @example
269
+ * ```typescript
270
+ * import { open, fstat, BaseDirectory } from '@tauri-apps/plugin-fs';
271
+ * const file = await open("file.txt", { read: true, dir: BaseDirectory.App });
272
+ * const fileInfo = await fstat(file.rid);
273
+ * console.log(fileInfo.isFile); // true
274
+ * ```
275
+ *
276
+ * @since 2.0.0
277
+ */
278
+ stat(): Promise<FileInfo>;
279
+ /**
280
+ * Truncates or extends this file, to reach the specified `len`.
281
+ * If `len` is not specified then the entire file contents are truncated.
282
+ *
283
+ * @example
284
+ * ```typescript
285
+ * import { ftruncate, open, write, read, BaseDirectory } from '@tauri-apps/plugin-fs';
286
+ *
287
+ * // truncate the entire file
288
+ * const file = await open("my_file.txt", { read: true, write: true, create: true, dir: BaseDirectory.App });
289
+ * await ftruncate(file.rid);
290
+ *
291
+ * // truncate part of the file
292
+ * const file = await open("my_file.txt", { read: true, write: true, create: true, dir: BaseDirectory.App });
293
+ * await write(file.rid, new TextEncoder().encode("Hello World"));
294
+ * await ftruncate(file.rid, 7);
295
+ * const data = new Uint8Array(32);
296
+ * await read(file.rid, data);
297
+ * console.log(new TextDecoder().decode(data)); // Hello W
298
+ * ```
299
+ *
300
+ * @since 2.0.0
301
+ */
302
+ truncate(len?: number): Promise<void>;
303
+ /**
304
+ * Writes `p.byteLength` bytes from `p` to the underlying data stream. It
305
+ * resolves to the number of bytes written from `p` (`0` <= `n` <=
306
+ * `p.byteLength`) or reject with the error encountered that caused the
307
+ * write to stop early. `write()` must reject with a non-null error if
308
+ * would resolve to `n` < `p.byteLength`. `write()` must not modify the
309
+ * slice data, even temporarily.
310
+ *
311
+ * @example
312
+ * ```typescript
313
+ * import { open, write, close, BaseDirectory } from '@tauri-apps/plugin-fs';
314
+ * const encoder = new TextEncoder();
315
+ * const data = encoder.encode("Hello world");
316
+ * const file = await open("bar.txt", { write: true, dir: BaseDirectory.App });
317
+ * const bytesWritten = await write(file.rid, data); // 11
318
+ * await close(file.rid);
319
+ * ```
320
+ *
321
+ * @since 2.0.0
322
+ */
323
+ write(data: Uint8Array): Promise<number>;
134
324
  }
135
325
  /**
136
326
  * @since 2.0.0
137
327
  */
138
- interface FsDirOptions {
139
- dir?: BaseDirectory;
140
- recursive?: boolean;
328
+ interface CreateOptions {
329
+ /** Base directory for `path` */
330
+ baseDir?: BaseDirectory;
141
331
  }
142
332
  /**
143
- * Options object used to write a UTF-8 string to a file.
333
+ * Creates a file if none exists or truncates an existing file and resolves to
334
+ * an instance of {@linkcode FileHandle }.
144
335
  *
145
- * @since 2.0.0
146
- */
147
- interface FsTextFileOption {
148
- /** Path to the file to write. */
149
- path: string;
150
- /** The UTF-8 string to write to the file. */
151
- contents: string;
152
- }
153
- type BinaryFileContents = Iterable<number> | ArrayLike<number> | ArrayBuffer;
154
- /**
155
- * Options object used to write a binary data to a file.
336
+ * @example
337
+ * ```typescript
338
+ * import { create, BaseDirectory } from "@tauri-apps/plugin-fs"
339
+ * const file = await create("foo/bar.txt", { dir: BaseDirectory.App });
340
+ * ```
156
341
  *
157
342
  * @since 2.0.0
158
343
  */
159
- interface FsBinaryFileOption {
160
- /** Path to the file to write. */
161
- path: string;
162
- /** The byte array contents. */
163
- contents: BinaryFileContents;
164
- }
344
+ declare function create(path: string | URL, options?: CreateOptions): Promise<FileHandle>;
165
345
  /**
166
346
  * @since 2.0.0
167
347
  */
168
- interface FileEntry {
169
- path: string;
348
+ interface OpenOptions {
349
+ /**
350
+ * Sets the option for read access. This option, when `true`, means that the
351
+ * file should be read-able if opened.
352
+ */
353
+ read?: boolean;
354
+ /**
355
+ * Sets the option for write access. This option, when `true`, means that
356
+ * the file should be write-able if opened. If the file already exists,
357
+ * any write calls on it will overwrite its contents, by default without
358
+ * truncating it.
359
+ */
360
+ write?: boolean;
361
+ /**
362
+ * Sets the option for the append mode. This option, when `true`, means that
363
+ * writes will append to a file instead of overwriting previous contents.
364
+ * Note that setting `{ write: true, append: true }` has the same effect as
365
+ * setting only `{ append: true }`.
366
+ */
367
+ append?: boolean;
170
368
  /**
171
- * Name of the directory/file
172
- * can be null if the path terminates with `..`
369
+ * Sets the option for truncating a previous file. If a file is
370
+ * successfully opened with this option set it will truncate the file to `0`
371
+ * size if it already exists. The file must be opened with write access
372
+ * for truncate to work.
173
373
  */
174
- name?: string;
175
- /** Children of this entry if it's a directory; null otherwise */
176
- children?: FileEntry[];
374
+ truncate?: boolean;
375
+ /**
376
+ * Sets the option to allow creating a new file, if one doesn't already
377
+ * exist at the specified path. Requires write or append access to be
378
+ * used.
379
+ */
380
+ create?: boolean;
381
+ /**
382
+ * Defaults to `false`. If set to `true`, no file, directory, or symlink is
383
+ * allowed to exist at the target location. Requires write or append
384
+ * access to be used. When createNew is set to `true`, create and truncate
385
+ * are ignored.
386
+ */
387
+ createNew?: boolean;
388
+ /**
389
+ * Permissions to use if creating the file (defaults to `0o666`, before
390
+ * the process's umask).
391
+ * Ignored on Windows.
392
+ */
393
+ mode?: number;
394
+ /** Base directory for `path` */
395
+ baseDir?: BaseDirectory;
177
396
  }
178
397
  /**
179
- * Reads a file as an UTF-8 encoded string.
398
+ * Open a file and resolve to an instance of {@linkcode FileHandle}. The
399
+ * file does not need to previously exist if using the `create` or `createNew`
400
+ * open options. It is the callers responsibility to close the file when finished
401
+ * with it.
402
+ *
180
403
  * @example
181
404
  * ```typescript
182
- * import { readTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
183
- * // Read the text file in the `$APPCONFIG/app.conf` path
184
- * const contents = await readTextFile('app.conf', { dir: BaseDirectory.AppConfig });
405
+ * import { open, BaseDirectory } from "@tauri-apps/plugin-fs"
406
+ * const file = await open("foo/bar.txt", { read: true, write: true, dir: BaseDirectory.App });
407
+ * // Do work with file
408
+ * await close(file.rid);
185
409
  * ```
186
410
  *
187
411
  * @since 2.0.0
188
412
  */
189
- declare function readTextFile(filePath: string, options?: FsOptions): Promise<string>;
413
+ declare function open(path: string | URL, options?: OpenOptions): Promise<FileHandle>;
190
414
  /**
191
- * Reads a file as byte array.
415
+ * @since 2.0.0
416
+ */
417
+ interface CopyFileOptions {
418
+ /** Base directory for `fromPath`. */
419
+ fromPathBaseDir?: BaseDirectory;
420
+ /** Base directory for `toPath`. */
421
+ toPathBaseDir?: BaseDirectory;
422
+ }
423
+ /**
424
+ * Copies the contents and permissions of one file to another specified path, by default creating a new file if needed, else overwriting.
192
425
  * @example
193
426
  * ```typescript
194
- * import { readBinaryFile, BaseDirectory } from '@tauri-apps/plugin-fs';
195
- * // Read the image file in the `$RESOURCEDIR/avatar.png` path
196
- * const contents = await readBinaryFile('avatar.png', { dir: BaseDirectory.Resource });
427
+ * import { copyFile, BaseDirectory } from '@tauri-apps/plugin-fs';
428
+ * await copyFile('app.conf', 'app.conf.bk', { dir: BaseDirectory.App });
197
429
  * ```
198
430
  *
199
431
  * @since 2.0.0
200
432
  */
201
- declare function readBinaryFile(filePath: string, options?: FsOptions): Promise<Uint8Array>;
433
+ declare function copyFile(fromPath: string | URL, toPath: string | URL, options?: CopyFileOptions): Promise<void>;
202
434
  /**
203
- * Writes a UTF-8 text file.
435
+ * @since 2.0.0
436
+ */
437
+ interface MkdirOptions {
438
+ /** Permissions to use when creating the directory (defaults to `0o777`, before the process's umask). Ignored on Windows. */
439
+ mode?: number;
440
+ /**
441
+ * Defaults to `false`. If set to `true`, means that any intermediate directories will also be created (as with the shell command `mkdir -p`).
442
+ * */
443
+ recursive?: boolean;
444
+ /** Base directory for `path` */
445
+ baseDir?: BaseDirectory;
446
+ }
447
+ /**
448
+ * Creates a new directory with the specified path.
204
449
  * @example
205
450
  * ```typescript
206
- * import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
207
- * // Write a text file to the `$APPCONFIG/app.conf` path
208
- * await writeTextFile('app.conf', 'file contents', { dir: BaseDirectory.AppConfig });
451
+ * import { mkdir, BaseDirectory } from '@tauri-apps/plugin-fs';
452
+ * await mkdir('users', { dir: BaseDirectory.App });
209
453
  * ```
210
454
  *
211
455
  * @since 2.0.0
212
456
  */
213
- declare function writeTextFile(path: string, contents: string, options?: FsOptions): Promise<void>;
457
+ declare function mkdir(path: string | URL, options?: MkdirOptions): Promise<void>;
458
+ /**
459
+ * @since 2.0.0
460
+ */
461
+ interface ReadDirOptions {
462
+ /** Base directory for `path` */
463
+ baseDir?: BaseDirectory;
464
+ }
465
+ /**
466
+ * A disk entry which is either a file, a directory or a symlink.
467
+ *
468
+ * This is the result of the {@linkcode readDir}.
469
+ *
470
+ * @since 2.0.0
471
+ */
472
+ interface DirEntry {
473
+ /** The name of the entry (file name with extension or directory name). */
474
+ name: string;
475
+ /** Specifies whether this entry is a directory or not. */
476
+ isDirectory: boolean;
477
+ /** Specifies whether this entry is a file or not. */
478
+ isFile: boolean;
479
+ /** Specifies whether this entry is a symlink or not. */
480
+ isSymlink: boolean;
481
+ }
214
482
  /**
215
- * Writes a UTF-8 text file.
483
+ * Reads the directory given by path and returns an array of `DirEntry`.
216
484
  * @example
217
485
  * ```typescript
218
- * import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
219
- * // Write a text file to the `$APPCONFIG/app.conf` path
220
- * await writeTextFile({ path: 'app.conf', contents: 'file contents' }, { dir: BaseDirectory.AppConfig });
486
+ * import { readDir, BaseDirectory } from '@tauri-apps/plugin-fs';
487
+ * const dir = "users"
488
+ * const entries = await readDir('users', { dir: BaseDirectory.App });
489
+ * processEntriesRecursive(dir, entries);
490
+ * async function processEntriesRecursive(parent, entries) {
491
+ * for (const entry of entries) {
492
+ * console.log(`Entry: ${entry.name}`);
493
+ * if (entry.isDirectory) {
494
+ * const dir = parent + entry.name;
495
+ * processEntriesRecursive(dir, await readDir(dir, { dir: BaseDirectory.App }))
496
+ * }
497
+ * }
498
+ * }
221
499
  * ```
222
- * @returns A promise indicating the success or failure of the operation.
223
500
  *
224
501
  * @since 2.0.0
225
502
  */
226
- declare function writeTextFile(file: FsTextFileOption, options?: FsOptions): Promise<void>;
503
+ declare function readDir(path: string | URL, options?: ReadDirOptions): Promise<DirEntry[]>;
504
+ /**
505
+ * @since 2.0.0
506
+ */
507
+ interface ReadFileOptions {
508
+ /** Base directory for `path` */
509
+ baseDir?: BaseDirectory;
510
+ }
227
511
  /**
228
- * Writes a byte array content to a file.
512
+ * Reads and resolves to the entire contents of a file as an array of bytes.
513
+ * TextDecoder can be used to transform the bytes to string if required.
229
514
  * @example
230
515
  * ```typescript
231
- * import { writeBinaryFile, BaseDirectory } from '@tauri-apps/plugin-fs';
232
- * // Write a binary file to the `$APPDATA/avatar.png` path
233
- * await writeBinaryFile('avatar.png', new Uint8Array([]), { dir: BaseDirectory.AppData });
516
+ * import { readFile, BaseDirectory } from '@tauri-apps/plugin-fs';
517
+ * const contents = await readFile('avatar.png', { dir: BaseDirectory.Resource });
234
518
  * ```
235
519
  *
236
- * @param options Configuration object.
237
- * @returns A promise indicating the success or failure of the operation.
238
- *
239
520
  * @since 2.0.0
240
521
  */
241
- declare function writeBinaryFile(path: string, contents: BinaryFileContents, options?: FsOptions): Promise<void>;
522
+ declare function readFile(path: string | URL, options?: ReadFileOptions): Promise<Uint8Array>;
242
523
  /**
243
- * Writes a byte array content to a file.
524
+ * Reads and returns the entire contents of a file as UTF-8 string.
244
525
  * @example
245
526
  * ```typescript
246
- * import { writeBinaryFile, BaseDirectory } from '@tauri-apps/plugin-fs';
247
- * // Write a binary file to the `$APPDATA/avatar.png` path
248
- * await writeBinaryFile({ path: 'avatar.png', contents: new Uint8Array([]) }, { dir: BaseDirectory.AppData });
527
+ * import { readTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
528
+ * const contents = await readTextFile('app.conf', { dir: BaseDirectory.App });
249
529
  * ```
250
530
  *
251
- * @param file The object containing the file path and contents.
252
- * @param options Configuration object.
253
- * @returns A promise indicating the success or failure of the operation.
254
- *
255
531
  * @since 2.0.0
256
532
  */
257
- declare function writeBinaryFile(file: FsBinaryFileOption, options?: FsOptions): Promise<void>;
533
+ declare function readTextFile(path: string | URL, options?: ReadFileOptions): Promise<string>;
258
534
  /**
259
- * List directory files.
535
+ * Returns an async {@linkcode AsyncIterableIterator} over the lines of a file as UTF-8 string.
260
536
  * @example
261
537
  * ```typescript
262
- * import { readDir, BaseDirectory } from '@tauri-apps/plugin-fs';
263
- * // Reads the `$APPDATA/users` directory recursively
264
- * const entries = await readDir('users', { dir: BaseDirectory.AppData, recursive: true });
265
- *
266
- * function processEntries(entries) {
267
- * for (const entry of entries) {
268
- * console.log(`Entry: ${entry.path}`);
269
- * if (entry.children) {
270
- * processEntries(entry.children)
271
- * }
272
- * }
538
+ * import { readTextFileLines, BaseDirectory } from '@tauri-apps/plugin-fs';
539
+ * const lines = await readTextFileLines('app.conf', { dir: BaseDirectory.App });
540
+ * for await (const line of lines) {
541
+ * console.log(line);
273
542
  * }
274
543
  * ```
544
+ * You could also call {@linkcode AsyncIterableIterator.next} to advance the
545
+ * iterator so you can lazily read the next line whenever you want.
275
546
  *
276
547
  * @since 2.0.0
277
548
  */
278
- declare function readDir(dir: string, options?: FsDirOptions): Promise<FileEntry[]>;
549
+ declare function readTextFileLines(path: string | URL, options?: ReadFileOptions): Promise<AsyncIterableIterator<string>>;
279
550
  /**
280
- * Creates a directory.
281
- * If one of the path's parent components doesn't exist
282
- * and the `recursive` option isn't set to true, the promise will be rejected.
551
+ * @since 2.0.0
552
+ */
553
+ interface RemoveOptions {
554
+ /** Defaults to `false`. If set to `true`, path will be removed even if it's a non-empty directory. */
555
+ recursive?: boolean;
556
+ /** Base directory for `path` */
557
+ baseDir?: BaseDirectory;
558
+ }
559
+ /**
560
+ * Removes the named file or directory.
561
+ * If the directory is not empty and the `recursive` option isn't set to true, the promise will be rejected.
283
562
  * @example
284
563
  * ```typescript
285
- * import { createDir, BaseDirectory } from '@tauri-apps/plugin-fs';
286
- * // Create the `$APPDATA/users` directory
287
- * await createDir('users', { dir: BaseDirectory.AppData, recursive: true });
564
+ * import { remove, BaseDirectory } from '@tauri-apps/plugin-fs';
565
+ * await remove('users/file.txt', { dir: BaseDirectory.App });
566
+ * await remove('users', { dir: BaseDirectory.App });
288
567
  * ```
289
568
  *
290
- * @returns A promise indicating the success or failure of the operation.
291
- *
292
569
  * @since 2.0.0
293
570
  */
294
- declare function createDir(dir: string, options?: FsDirOptions): Promise<void>;
571
+ declare function remove(path: string | URL, options?: RemoveOptions): Promise<void>;
295
572
  /**
296
- * Removes a directory.
297
- * If the directory is not empty and the `recursive` option isn't set to true, the promise will be rejected.
573
+ * @since 2.0.0
574
+ */
575
+ interface RenameOptions {
576
+ /** Base directory for `oldPath`. */
577
+ oldPathBaseDir?: BaseDirectory;
578
+ /** Base directory for `newPath`. */
579
+ newPathBaseDir?: BaseDirectory;
580
+ }
581
+ /**
582
+ * Renames (moves) oldpath to newpath. Paths may be files or directories.
583
+ * If newpath already exists and is not a directory, rename() replaces it.
584
+ * OS-specific restrictions may apply when oldpath and newpath are in different directories.
585
+ *
586
+ * On Unix, this operation does not follow symlinks at either path.
587
+ *
298
588
  * @example
299
589
  * ```typescript
300
- * import { removeDir, BaseDirectory } from '@tauri-apps/plugin-fs';
301
- * // Remove the directory `$APPDATA/users`
302
- * await removeDir('users', { dir: BaseDirectory.AppData });
590
+ * import { rename, BaseDirectory } from '@tauri-apps/plugin-fs';
591
+ * await rename('avatar.png', 'deleted.png', { dir: BaseDirectory.App });
303
592
  * ```
304
593
  *
305
- * @returns A promise indicating the success or failure of the operation.
306
- *
307
594
  * @since 2.0.0
308
595
  */
309
- declare function removeDir(dir: string, options?: FsDirOptions): Promise<void>;
596
+ declare function rename(oldPath: string | URL, newPath: string | URL, options: RenameOptions): Promise<void>;
597
+ /**
598
+ * @since 2.0.0
599
+ */
600
+ interface StatOptions {
601
+ /** Base directory for `path`. */
602
+ baseDir?: BaseDirectory;
603
+ }
310
604
  /**
311
- * Copies a file to a destination.
605
+ * Resolves to a {@linkcode FileInfo} for the specified `path`. Will always
606
+ * follow symlinks but will reject if the symlink points to a path outside of the scope.
607
+ *
312
608
  * @example
313
609
  * ```typescript
314
- * import { copyFile, BaseDirectory } from '@tauri-apps/plugin-fs';
315
- * // Copy the `$APPCONFIG/app.conf` file to `$APPCONFIG/app.conf.bk`
316
- * await copyFile('app.conf', 'app.conf.bk', { dir: BaseDirectory.AppConfig });
610
+ * import { stat, BaseDirectory } from '@tauri-apps/plugin-fs';
611
+ * const fileInfo = await stat("hello.txt", { dir: BaseDirectory.App });
612
+ * console.log(fileInfo.isFile); // true
317
613
  * ```
318
614
  *
319
- * @returns A promise indicating the success or failure of the operation.
320
- *
321
615
  * @since 2.0.0
322
616
  */
323
- declare function copyFile(source: string, destination: string, options?: FsOptions): Promise<void>;
617
+ declare function stat(path: string | URL, options?: StatOptions): Promise<FileInfo>;
324
618
  /**
325
- * Removes a file.
619
+ * Resolves to a {@linkcode FileInfo} for the specified `path`. If `path` is a
620
+ * symlink, information for the symlink will be returned instead of what it
621
+ * points to.
622
+ *
326
623
  * @example
327
624
  * ```typescript
328
- * import { removeFile, BaseDirectory } from '@tauri-apps/plugin-fs';
329
- * // Remove the `$APPConfig/app.conf` file
330
- * await removeFile('app.conf', { dir: BaseDirectory.AppConfig });
625
+ * import { lstat, BaseDirectory } from '@tauri-apps/plugin-fs';
626
+ * const fileInfo = await lstat("hello.txt", { dir: BaseDirectory.App });
627
+ * console.log(fileInfo.isFile); // true
331
628
  * ```
332
629
  *
333
- * @returns A promise indicating the success or failure of the operation.
334
- *
335
630
  * @since 2.0.0
336
631
  */
337
- declare function removeFile(file: string, options?: FsOptions): Promise<void>;
632
+ declare function lstat(path: string | URL, options?: StatOptions): Promise<FileInfo>;
633
+ /**
634
+ * @since 2.0.0
635
+ */
636
+ interface TruncateOptions {
637
+ /** Base directory for `path`. */
638
+ baseDir?: BaseDirectory;
639
+ }
338
640
  /**
339
- * Renames a file.
641
+ * Truncates or extends the specified file, to reach the specified `len`.
642
+ * If `len` is `0` or not specified, then the entire file contents are truncated.
643
+ *
340
644
  * @example
341
645
  * ```typescript
342
- * import { renameFile, BaseDirectory } from '@tauri-apps/plugin-fs';
343
- * // Rename the `$APPDATA/avatar.png` file
344
- * await renameFile('avatar.png', 'deleted.png', { dir: BaseDirectory.AppData });
646
+ * import { truncate, readFile, writeFile, BaseDirectory } from '@tauri-apps/plugin-fs';
647
+ * // truncate the entire file
648
+ * await truncate("my_file.txt", 0, { dir: BaseDirectory.App });
649
+ *
650
+ * // truncate part of the file
651
+ * let file = "file.txt";
652
+ * await writeFile(file, new TextEncoder().encode("Hello World"), { dir: BaseDirectory.App });
653
+ * await truncate(file, 7);
654
+ * const data = await readFile(file, { dir: BaseDirectory.App });
655
+ * console.log(new TextDecoder().decode(data)); // "Hello W"
345
656
  * ```
346
657
  *
347
- * @returns A promise indicating the success or failure of the operation.
658
+ * @since 2.0.0
659
+ */
660
+ declare function truncate(path: string | URL, len?: number, options?: TruncateOptions): Promise<void>;
661
+ /**
662
+ * @since 2.0.0
663
+ */
664
+ interface WriteFileOptions {
665
+ /** Defaults to `false`. If set to `true`, will append to a file instead of overwriting previous contents. */
666
+ append?: boolean;
667
+ /** Sets the option to allow creating a new file, if one doesn't already exist at the specified path (defaults to `true`). */
668
+ create?: boolean;
669
+ /** File permissions. Ignored on Windows. */
670
+ mode?: number;
671
+ /** Base directory for `path` */
672
+ baseDir?: BaseDirectory;
673
+ }
674
+ /**
675
+ * Write `data` to the given `path`, by default creating a new file if needed, else overwriting.
676
+ * @example
677
+ * ```typescript
678
+ * import { writeFile, BaseDirectory } from '@tauri-apps/plugin-fs';
679
+ *
680
+ * let encoder = new TextEncoder();
681
+ * let data = encoder.encode("Hello World");
682
+ * await writeFile('file.txt', data, { dir: BaseDirectory.App });
683
+ * ```
348
684
  *
349
685
  * @since 2.0.0
350
686
  */
351
- declare function renameFile(oldPath: string, newPath: string, options?: FsOptions): Promise<void>;
687
+ declare function writeFile(path: string | URL, data: Uint8Array, options?: WriteFileOptions): Promise<void>;
688
+ /**
689
+ * Writes UTF-8 string `data` to the given `path`, by default creating a new file if needed, else overwriting.
690
+ @example
691
+ * ```typescript
692
+ * import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
693
+ *
694
+ * await writeTextFile('file.txt', "Hello world", { dir: BaseDirectory.App });
695
+ * ```
696
+ *
697
+ * @since 2.0.0
698
+ */
699
+ declare function writeTextFile(path: string | URL, data: string, options?: WriteFileOptions): Promise<void>;
700
+ /**
701
+ * @since 2.0.0
702
+ */
703
+ interface ExistsOptions {
704
+ /** Base directory for `path`. */
705
+ baseDir?: BaseDirectory;
706
+ }
352
707
  /**
353
708
  * Check if a path exists.
354
709
  * @example
@@ -360,12 +715,68 @@ declare function renameFile(oldPath: string, newPath: string, options?: FsOption
360
715
  *
361
716
  * @since 2.0.0
362
717
  */
363
- declare function exists(path: string, options?: FsOptions): Promise<boolean>;
718
+ declare function exists(path: string | URL, options?: ExistsOptions): Promise<boolean>;
719
+ /**
720
+ * @since 2.0.0
721
+ */
722
+ interface WatchOptions {
723
+ /** Watch a directory recursively */
724
+ recursive?: boolean;
725
+ /** Base directory for `path` */
726
+ baseDir?: BaseDirectory;
727
+ }
728
+ /**
729
+ * @since 2.0.0
730
+ */
731
+ interface DebouncedWatchOptions extends WatchOptions {
732
+ /** Debounce delay */
733
+ delayMs?: number;
734
+ }
735
+ /**
736
+ * @since 2.0.0
737
+ */
738
+ type RawEvent = {
739
+ type: RawEventKind;
740
+ paths: string[];
741
+ attrs: unknown;
742
+ };
743
+ /**
744
+ * @since 2.0.0
745
+ */
746
+ type RawEventKind = "any " | {
747
+ access?: unknown;
748
+ } | {
749
+ create?: unknown;
750
+ } | {
751
+ modify?: unknown;
752
+ } | {
753
+ remove?: unknown;
754
+ } | "other";
755
+ /**
756
+ * @since 2.0.0
757
+ */
758
+ type DebouncedEvent = {
759
+ kind: "any";
760
+ path: string;
761
+ } | {
762
+ kind: "AnyContinous";
763
+ path: string;
764
+ };
765
+ /**
766
+ * @since 2.0.0
767
+ */
768
+ type UnwatchFn = () => void;
769
+ /**
770
+ * Watch changes (after a delay) on files or directories.
771
+ *
772
+ * @since 2.0.0
773
+ */
774
+ declare function watch(paths: string | string[] | URL | URL[], cb: (event: DebouncedEvent) => void, options?: DebouncedWatchOptions): Promise<UnwatchFn>;
364
775
  /**
365
- * Returns the metadata for the given path.
776
+ * Watch changes on files or directories.
366
777
  *
367
778
  * @since 2.0.0
368
779
  */
369
- declare function metadata(path: string): Promise<Metadata>;
370
- export type { FsOptions, FsDirOptions, FsTextFileOption, BinaryFileContents, FsBinaryFileOption, FileEntry, Permissions, Metadata, };
371
- export { BaseDirectory, BaseDirectory as Dir, readTextFile, readBinaryFile, writeTextFile, writeTextFile as writeFile, writeBinaryFile, readDir, createDir, removeDir, copyFile, removeFile, renameFile, exists, metadata, };
780
+ declare function watchImmediate(paths: string | string[] | URL | URL[], cb: (event: RawEvent) => void, options?: WatchOptions): Promise<UnwatchFn>;
781
+ export type { CreateOptions, OpenOptions, CopyFileOptions, MkdirOptions, DirEntry, ReadDirOptions, ReadFileOptions, RemoveOptions, RenameOptions, StatOptions, TruncateOptions, WriteFileOptions, ExistsOptions, FileInfo, WatchOptions, DebouncedWatchOptions, DebouncedEvent, RawEvent, UnwatchFn, };
782
+ export { BaseDirectory, FileHandle, create, open, copyFile, mkdir, readDir, readFile, readTextFile, readTextFileLines, remove, rename, SeekMode, stat, lstat, truncate, writeFile, writeTextFile, exists, watch, watchImmediate, };