@tauri-apps/plugin-fs 2.0.0-alpha.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/LICENSE.spdx ADDED
@@ -0,0 +1,20 @@
1
+ SPDXVersion: SPDX-2.1
2
+ DataLicense: CC0-1.0
3
+ PackageName: tauri
4
+ DataFormat: SPDXRef-1
5
+ PackageSupplier: Organization: The Tauri Programme in the Commons Conservancy
6
+ PackageHomePage: https://tauri.app
7
+ PackageLicenseDeclared: Apache-2.0
8
+ PackageLicenseDeclared: MIT
9
+ PackageCopyrightText: 2019-2022, The Tauri Programme in the Commons Conservancy
10
+ PackageSummary: <text>Tauri is a rust project that enables developers to make secure
11
+ and small desktop applications using a web frontend.
12
+ </text>
13
+ PackageComment: <text>The package includes the following libraries; see
14
+ Relationship information.
15
+ </text>
16
+ Created: 2019-05-20T09:00:00Z
17
+ PackageDownloadLocation: git://github.com/tauri-apps/tauri
18
+ PackageDownloadLocation: git+https://github.com/tauri-apps/tauri.git
19
+ PackageDownloadLocation: git+ssh://github.com/tauri-apps/tauri.git
20
+ Creator: Person: Daniel Thompson-Yvetot
package/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # File System
2
+
3
+ Access the file system.
4
+
5
+ ## Install
6
+
7
+ _This plugin requires a Rust version of at least **1.65**_
8
+
9
+ There are three general methods of installation that we can recommend.
10
+
11
+ 1. Use crates.io and npm (easiest, and requires you to trust that our publishing pipeline worked)
12
+ 2. Pull sources directly from Github using git tags / revision hashes (most secure)
13
+ 3. Git submodule install this repo in your tauri project and then use file protocol to ingest the source (most secure, but inconvenient to use)
14
+
15
+ Install the Core plugin by adding the following to your `Cargo.toml` file:
16
+
17
+ `src-tauri/Cargo.toml`
18
+
19
+ ```toml
20
+ [dependencies]
21
+ tauri-plugin-fs = "2.0.0-alpha"
22
+ # alternatively with Git:
23
+ tauri-plugin-fs = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" }
24
+ ```
25
+
26
+ You can install the JavaScript Guest bindings using your preferred JavaScript package manager:
27
+
28
+ > Note: Since most JavaScript package managers are unable to install packages from git monorepos we provide read-only mirrors of each plugin. This makes installation option 2 more ergonomic to use.
29
+
30
+ ```sh
31
+ pnpm add @tauri-apps/plugin-fs
32
+ # or
33
+ npm add @tauri-apps/plugin-fs
34
+ # or
35
+ yarn add @tauri-apps/plugin-fs
36
+
37
+ # alternatively with Git:
38
+ pnpm add https://github.com/tauri-apps/tauri-plugin-fs#v2
39
+ # or
40
+ npm add https://github.com/tauri-apps/tauri-plugin-fs#v2
41
+ # or
42
+ yarn add https://github.com/tauri-apps/tauri-plugin-fs#v2
43
+ ```
44
+
45
+ ## Usage
46
+
47
+ First you need to register the core plugin with Tauri:
48
+
49
+ `src-tauri/src/main.rs`
50
+
51
+ ```rust
52
+ fn main() {
53
+ tauri::Builder::default()
54
+ .plugin(tauri_plugin_fs::init())
55
+ .run(tauri::generate_context!())
56
+ .expect("error while running tauri application");
57
+ }
58
+ ```
59
+
60
+ Afterwards all the plugin's APIs are available through the JavaScript guest bindings:
61
+
62
+ ```javascript
63
+ import { metadata } from "@tauri-apps/plugin-fs";
64
+
65
+ await metadata("/path/to/file");
66
+ ```
67
+
68
+ ## Contributing
69
+
70
+ PRs accepted. Please make sure to read the Contributing Guide before making a pull request.
71
+
72
+ ## License
73
+
74
+ Code: (c) 2015 - Present - The Tauri Programme within The Commons Conservancy.
75
+
76
+ MIT or MIT/Apache 2.0 where applicable.
@@ -0,0 +1,376 @@
1
+ /**
2
+ * Access the file system.
3
+ *
4
+ * ## Security
5
+ *
6
+ * This module prevents path traversal, not allowing absolute paths or parent dir components
7
+ * (i.e. "/usr/path/to/file" or "../path/to/file" paths are not allowed).
8
+ * Paths accessed with this API must be relative to one of the {@link BaseDirectory | base directories}
9
+ * so if you need access to arbitrary filesystem paths, you must write such logic on the core layer instead.
10
+ *
11
+ * The API has a scope configuration that forces you to restrict the paths that can be accessed using glob patterns.
12
+ *
13
+ * The scope configuration is an array of glob patterns describing folder paths that are allowed.
14
+ * For instance, this scope configuration only allows accessing files on the
15
+ * *databases* folder of the {@link path.appDataDir | $APPDATA directory}:
16
+ * ```json
17
+ * {
18
+ * "plugins": {
19
+ * "fs": {
20
+ * "scope": ["$APPDATA/databases/*"]
21
+ * }
22
+ * }
23
+ * }
24
+ * ```
25
+ *
26
+ * Notice the use of the `$APPDATA` variable. The value is injected at runtime, resolving to the {@link path.appDataDir | app data directory}.
27
+ * 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`}.
36
+ *
37
+ * Trying to execute any API with a URL not configured on the scope results in a promise rejection due to denied access.
38
+ *
39
+ * Note that this scope applies to **all** APIs on this module.
40
+ *
41
+ * @module
42
+ */
43
+ import { BaseDirectory } from "@tauri-apps/api/path";
44
+ declare global {
45
+ interface Window {
46
+ __TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
47
+ }
48
+ }
49
+ interface Permissions {
50
+ /**
51
+ * `true` if these permissions describe a readonly (unwritable) file.
52
+ */
53
+ readonly: boolean;
54
+ /**
55
+ * The underlying raw `st_mode` bits that contain the standard Unix permissions for this file.
56
+ */
57
+ mode: number | undefined;
58
+ }
59
+ /**
60
+ * Metadata information about a file.
61
+ * This structure is returned from the `metadata` function or method
62
+ * and represents known metadata about a file such as its permissions, size, modification times, etc.
63
+ */
64
+ interface Metadata {
65
+ /**
66
+ * The last access time of this metadata.
67
+ */
68
+ accessedAt: Date;
69
+ /**
70
+ * The creation time listed in this metadata.
71
+ */
72
+ createdAt: Date;
73
+ /**
74
+ * The last modification time listed in this metadata.
75
+ */
76
+ modifiedAt: Date;
77
+ /**
78
+ * `true` if this metadata is for a directory.
79
+ */
80
+ isDir: boolean;
81
+ /**
82
+ * `true` if this metadata is for a regular file.
83
+ */
84
+ isFile: boolean;
85
+ /**
86
+ * `true` if this metadata is for a symbolic link.
87
+ */
88
+ isSymlink: boolean;
89
+ /**
90
+ * The size of the file, in bytes, this metadata is for.
91
+ */
92
+ size: number;
93
+ /**
94
+ * The permissions of the file this metadata is for.
95
+ */
96
+ permissions: Permissions;
97
+ /**
98
+ * The ID of the device containing the file. Only available on Unix.
99
+ */
100
+ dev: number | undefined;
101
+ /**
102
+ * The inode number. Only available on Unix.
103
+ */
104
+ ino: number | undefined;
105
+ /**
106
+ * The rights applied to this file. Only available on Unix.
107
+ */
108
+ mode: number | undefined;
109
+ /**
110
+ * The number of hard links pointing to this file. Only available on Unix.
111
+ */
112
+ nlink: number | undefined;
113
+ /**
114
+ * The user ID of the owner of this file. Only available on Unix.
115
+ */
116
+ uid: number | undefined;
117
+ /**
118
+ * The group ID of the owner of this file. Only available on Unix.
119
+ */
120
+ gid: number | undefined;
121
+ /**
122
+ * The device ID of this file (if it is a special one). Only available on Unix.
123
+ */
124
+ rdev: number | undefined;
125
+ /**
126
+ * The block size for filesystem I/O. Only available on Unix.
127
+ */
128
+ blksize: number | undefined;
129
+ /**
130
+ * The number of blocks allocated to the file, in 512-byte units. Only available on Unix.
131
+ */
132
+ blocks: number | undefined;
133
+ }
134
+ /**
135
+ * @since 2.0.0
136
+ */
137
+ interface FsOptions {
138
+ dir?: BaseDirectory;
139
+ }
140
+ /**
141
+ * @since 2.0.0
142
+ */
143
+ interface FsDirOptions {
144
+ dir?: BaseDirectory;
145
+ recursive?: boolean;
146
+ }
147
+ /**
148
+ * Options object used to write a UTF-8 string to a file.
149
+ *
150
+ * @since 2.0.0
151
+ */
152
+ interface FsTextFileOption {
153
+ /** Path to the file to write. */
154
+ path: string;
155
+ /** The UTF-8 string to write to the file. */
156
+ contents: string;
157
+ }
158
+ type BinaryFileContents = Iterable<number> | ArrayLike<number> | ArrayBuffer;
159
+ /**
160
+ * Options object used to write a binary data to a file.
161
+ *
162
+ * @since 2.0.0
163
+ */
164
+ interface FsBinaryFileOption {
165
+ /** Path to the file to write. */
166
+ path: string;
167
+ /** The byte array contents. */
168
+ contents: BinaryFileContents;
169
+ }
170
+ /**
171
+ * @since 2.0.0
172
+ */
173
+ interface FileEntry {
174
+ path: string;
175
+ /**
176
+ * Name of the directory/file
177
+ * can be null if the path terminates with `..`
178
+ */
179
+ name?: string;
180
+ /** Children of this entry if it's a directory; null otherwise */
181
+ children?: FileEntry[];
182
+ }
183
+ /**
184
+ * Reads a file as an UTF-8 encoded string.
185
+ * @example
186
+ * ```typescript
187
+ * import { readTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
188
+ * // Read the text file in the `$APPCONFIG/app.conf` path
189
+ * const contents = await readTextFile('app.conf', { dir: BaseDirectory.AppConfig });
190
+ * ```
191
+ *
192
+ * @since 2.0.0
193
+ */
194
+ declare function readTextFile(filePath: string, options?: FsOptions): Promise<string>;
195
+ /**
196
+ * Reads a file as byte array.
197
+ * @example
198
+ * ```typescript
199
+ * import { readBinaryFile, BaseDirectory } from '@tauri-apps/plugin-fs';
200
+ * // Read the image file in the `$RESOURCEDIR/avatar.png` path
201
+ * const contents = await readBinaryFile('avatar.png', { dir: BaseDirectory.Resource });
202
+ * ```
203
+ *
204
+ * @since 2.0.0
205
+ */
206
+ declare function readBinaryFile(filePath: string, options?: FsOptions): Promise<Uint8Array>;
207
+ /**
208
+ * Writes a UTF-8 text file.
209
+ * @example
210
+ * ```typescript
211
+ * import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
212
+ * // Write a text file to the `$APPCONFIG/app.conf` path
213
+ * await writeTextFile('app.conf', 'file contents', { dir: BaseDirectory.AppConfig });
214
+ * ```
215
+ *
216
+ * @since 2.0.0
217
+ */
218
+ declare function writeTextFile(path: string, contents: string, options?: FsOptions): Promise<void>;
219
+ /**
220
+ * Writes a UTF-8 text file.
221
+ * @example
222
+ * ```typescript
223
+ * import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
224
+ * // Write a text file to the `$APPCONFIG/app.conf` path
225
+ * await writeTextFile({ path: 'app.conf', contents: 'file contents' }, { dir: BaseDirectory.AppConfig });
226
+ * ```
227
+ * @returns A promise indicating the success or failure of the operation.
228
+ *
229
+ * @since 2.0.0
230
+ */
231
+ declare function writeTextFile(file: FsTextFileOption, options?: FsOptions): Promise<void>;
232
+ /**
233
+ * Writes a byte array content to a file.
234
+ * @example
235
+ * ```typescript
236
+ * import { writeBinaryFile, BaseDirectory } from '@tauri-apps/plugin-fs';
237
+ * // Write a binary file to the `$APPDATA/avatar.png` path
238
+ * await writeBinaryFile('avatar.png', new Uint8Array([]), { dir: BaseDirectory.AppData });
239
+ * ```
240
+ *
241
+ * @param options Configuration object.
242
+ * @returns A promise indicating the success or failure of the operation.
243
+ *
244
+ * @since 2.0.0
245
+ */
246
+ declare function writeBinaryFile(path: string, contents: BinaryFileContents, options?: FsOptions): Promise<void>;
247
+ /**
248
+ * Writes a byte array content to a file.
249
+ * @example
250
+ * ```typescript
251
+ * import { writeBinaryFile, BaseDirectory } from '@tauri-apps/plugin-fs';
252
+ * // Write a binary file to the `$APPDATA/avatar.png` path
253
+ * await writeBinaryFile({ path: 'avatar.png', contents: new Uint8Array([]) }, { dir: BaseDirectory.AppData });
254
+ * ```
255
+ *
256
+ * @param file The object containing the file path and contents.
257
+ * @param options Configuration object.
258
+ * @returns A promise indicating the success or failure of the operation.
259
+ *
260
+ * @since 2.0.0
261
+ */
262
+ declare function writeBinaryFile(file: FsBinaryFileOption, options?: FsOptions): Promise<void>;
263
+ /**
264
+ * List directory files.
265
+ * @example
266
+ * ```typescript
267
+ * import { readDir, BaseDirectory } from '@tauri-apps/plugin-fs';
268
+ * // Reads the `$APPDATA/users` directory recursively
269
+ * const entries = await readDir('users', { dir: BaseDirectory.AppData, recursive: true });
270
+ *
271
+ * function processEntries(entries) {
272
+ * for (const entry of entries) {
273
+ * console.log(`Entry: ${entry.path}`);
274
+ * if (entry.children) {
275
+ * processEntries(entry.children)
276
+ * }
277
+ * }
278
+ * }
279
+ * ```
280
+ *
281
+ * @since 2.0.0
282
+ */
283
+ declare function readDir(dir: string, options?: FsDirOptions): Promise<FileEntry[]>;
284
+ /**
285
+ * Creates a directory.
286
+ * If one of the path's parent components doesn't exist
287
+ * and the `recursive` option isn't set to true, the promise will be rejected.
288
+ * @example
289
+ * ```typescript
290
+ * import { createDir, BaseDirectory } from '@tauri-apps/plugin-fs';
291
+ * // Create the `$APPDATA/users` directory
292
+ * await createDir('users', { dir: BaseDirectory.AppData, recursive: true });
293
+ * ```
294
+ *
295
+ * @returns A promise indicating the success or failure of the operation.
296
+ *
297
+ * @since 2.0.0
298
+ */
299
+ declare function createDir(dir: string, options?: FsDirOptions): Promise<void>;
300
+ /**
301
+ * Removes a directory.
302
+ * If the directory is not empty and the `recursive` option isn't set to true, the promise will be rejected.
303
+ * @example
304
+ * ```typescript
305
+ * import { removeDir, BaseDirectory } from '@tauri-apps/plugin-fs';
306
+ * // Remove the directory `$APPDATA/users`
307
+ * await removeDir('users', { dir: BaseDirectory.AppData });
308
+ * ```
309
+ *
310
+ * @returns A promise indicating the success or failure of the operation.
311
+ *
312
+ * @since 2.0.0
313
+ */
314
+ declare function removeDir(dir: string, options?: FsDirOptions): Promise<void>;
315
+ /**
316
+ * Copies a file to a destination.
317
+ * @example
318
+ * ```typescript
319
+ * import { copyFile, BaseDirectory } from '@tauri-apps/plugin-fs';
320
+ * // Copy the `$APPCONFIG/app.conf` file to `$APPCONFIG/app.conf.bk`
321
+ * await copyFile('app.conf', 'app.conf.bk', { dir: BaseDirectory.AppConfig });
322
+ * ```
323
+ *
324
+ * @returns A promise indicating the success or failure of the operation.
325
+ *
326
+ * @since 2.0.0
327
+ */
328
+ declare function copyFile(source: string, destination: string, options?: FsOptions): Promise<void>;
329
+ /**
330
+ * Removes a file.
331
+ * @example
332
+ * ```typescript
333
+ * import { removeFile, BaseDirectory } from '@tauri-apps/plugin-fs';
334
+ * // Remove the `$APPConfig/app.conf` file
335
+ * await removeFile('app.conf', { dir: BaseDirectory.AppConfig });
336
+ * ```
337
+ *
338
+ * @returns A promise indicating the success or failure of the operation.
339
+ *
340
+ * @since 2.0.0
341
+ */
342
+ declare function removeFile(file: string, options?: FsOptions): Promise<void>;
343
+ /**
344
+ * Renames a file.
345
+ * @example
346
+ * ```typescript
347
+ * import { renameFile, BaseDirectory } from '@tauri-apps/plugin-fs';
348
+ * // Rename the `$APPDATA/avatar.png` file
349
+ * await renameFile('avatar.png', 'deleted.png', { dir: BaseDirectory.AppData });
350
+ * ```
351
+ *
352
+ * @returns A promise indicating the success or failure of the operation.
353
+ *
354
+ * @since 2.0.0
355
+ */
356
+ declare function renameFile(oldPath: string, newPath: string, options?: FsOptions): Promise<void>;
357
+ /**
358
+ * Check if a path exists.
359
+ * @example
360
+ * ```typescript
361
+ * import { exists, BaseDirectory } from '@tauri-apps/plugin-fs';
362
+ * // Check if the `$APPDATA/avatar.png` file exists
363
+ * await exists('avatar.png', { dir: BaseDirectory.AppData });
364
+ * ```
365
+ *
366
+ * @since 2.0.0
367
+ */
368
+ declare function exists(path: string): Promise<boolean>;
369
+ /**
370
+ * Returns the metadata for the given path.
371
+ *
372
+ * @since 2.0.0
373
+ */
374
+ declare function metadata(path: string): Promise<Metadata>;
375
+ export type { FsOptions, FsDirOptions, FsTextFileOption, BinaryFileContents, FsBinaryFileOption, FileEntry, Permissions, Metadata, };
376
+ export { BaseDirectory, BaseDirectory as Dir, readTextFile, readBinaryFile, writeTextFile, writeTextFile as writeFile, writeBinaryFile, readDir, createDir, removeDir, copyFile, removeFile, renameFile, exists, metadata, };
@@ -0,0 +1,278 @@
1
+ var f$2=Object.defineProperty;var g$1=(a,b)=>{for(var c in b)f$2(a,c,{get:b[c],enumerable:!0});};var e$1=(a,b,c)=>{if(!b.has(a))throw TypeError("Cannot "+c)};var h$2=(a,b,c)=>(e$1(a,b,"read from private field"),c?c.call(a):b.get(a)),i=(a,b,c)=>{if(b.has(a))throw TypeError("Cannot add the same private member more than once");b instanceof WeakSet?b.add(a):b.set(a,c);},j$1=(a,b,c,d)=>(e$1(a,b,"write to private field"),d?d.call(a,c):b.set(a,c),c);
2
+
3
+ var f$1={};g$1(f$1,{Channel:()=>a$1,PluginListener:()=>c$1,addPluginListener:()=>v$1,convertFileSrc:()=>m$1,invoke:()=>u$1,transformCallback:()=>o});function h$1(){return window.crypto.getRandomValues(new Uint32Array(1))[0]}function o(t,e=!1){let n=h$1(),r=`_${n}`;return Object.defineProperty(window,r,{value:i=>(e&&Reflect.deleteProperty(window,r),t?.(i)),writable:!1,configurable:!0}),n}var s,a$1=class a{constructor(){this.__TAURI_CHANNEL_MARKER__=!0;i(this,s,()=>{});this.id=o(e=>{h$2(this,s).call(this,e);});}set onmessage(e){j$1(this,s,e);}get onmessage(){return h$2(this,s)}toJSON(){return `__CHANNEL__:${this.id}`}};s=new WeakMap;var c$1=class c{constructor(e,n,r){this.plugin=e,this.event=n,this.channelId=r;}async unregister(){return u$1(`plugin:${this.plugin}|remove_listener`,{event:this.event,channelId:this.channelId})}};async function v$1(t,e,n){let r=new a$1;return r.onmessage=n,u$1(`plugin:${t}|register_listener`,{event:e,handler:r}).then(()=>new c$1(t,e,r.id))}async function u$1(t,e={}){return new Promise((n,r)=>{let i=o(d=>{n(d),Reflect.deleteProperty(window,`_${g}`);},!0),g=o(d=>{r(d),Reflect.deleteProperty(window,`_${i}`);},!0);window.__TAURI_IPC__({cmd:t,callback:i,error:g,...e});})}function m$1(t,e="asset"){let n=encodeURIComponent(t);return navigator.userAgent.includes("Windows")?`https://${e}.localhost/${n}`:`${e}://localhost/${n}`}
4
+
5
+ var O={};g$1(O,{BaseDirectory:()=>e,appCacheDir:()=>p,appConfigDir:()=>u,appDataDir:()=>c,appLocalDataDir:()=>a,appLogDir:()=>V,audioDir:()=>l,basename:()=>H,cacheDir:()=>g,configDir:()=>d,dataDir:()=>m,delimiter:()=>T,desktopDir:()=>y,dirname:()=>E,documentDir:()=>f,downloadDir:()=>h,executableDir:()=>P,extname:()=>F,fontDir:()=>v,homeDir:()=>_,isAbsolute:()=>M,join:()=>z,localDataDir:()=>D,normalize:()=>j,pictureDir:()=>b,publicDir:()=>A,resolve:()=>W,resolveResource:()=>L,resourceDir:()=>C,runtimeDir:()=>x,sep:()=>k,templateDir:()=>R,videoDir:()=>w});function t(){return navigator.appVersion.includes("Win")}var e=(r=>(r[r.Audio=1]="Audio",r[r.Cache=2]="Cache",r[r.Config=3]="Config",r[r.Data=4]="Data",r[r.LocalData=5]="LocalData",r[r.Document=6]="Document",r[r.Download=7]="Download",r[r.Picture=8]="Picture",r[r.Public=9]="Public",r[r.Video=10]="Video",r[r.Resource=11]="Resource",r[r.Temp=12]="Temp",r[r.AppConfig=13]="AppConfig",r[r.AppData=14]="AppData",r[r.AppLocalData=15]="AppLocalData",r[r.AppCache=16]="AppCache",r[r.AppLog=17]="AppLog",r[r.Desktop=18]="Desktop",r[r.Executable=19]="Executable",r[r.Font=20]="Font",r[r.Home=21]="Home",r[r.Runtime=22]="Runtime",r[r.Template=23]="Template",r))(e||{});async function u(){return u$1("plugin:path|resolve_directory",{directory:13})}async function c(){return u$1("plugin:path|resolve_directory",{directory:14})}async function a(){return u$1("plugin:path|resolve_directory",{directory:15})}async function p(){return u$1("plugin:path|resolve_directory",{directory:16})}async function l(){return u$1("plugin:path|resolve_directory",{directory:1})}async function g(){return u$1("plugin:path|resolve_directory",{directory:2})}async function d(){return u$1("plugin:path|resolve_directory",{directory:3})}async function m(){return u$1("plugin:path|resolve_directory",{directory:4})}async function y(){return u$1("plugin:path|resolve_directory",{directory:18})}async function f(){return u$1("plugin:path|resolve_directory",{directory:6})}async function h(){return u$1("plugin:path|resolve_directory",{directory:7})}async function P(){return u$1("plugin:path|resolve_directory",{directory:19})}async function v(){return u$1("plugin:path|resolve_directory",{directory:20})}async function _(){return u$1("plugin:path|resolve_directory",{directory:21})}async function D(){return u$1("plugin:path|resolve_directory",{directory:5})}async function b(){return u$1("plugin:path|resolve_directory",{directory:8})}async function A(){return u$1("plugin:path|resolve_directory",{directory:9})}async function C(){return u$1("plugin:path|resolve_directory",{directory:11})}async function L(i){return u$1("plugin:path|resolve_directory",{directory:11,path:i})}async function x(){return u$1("plugin:path|resolve_directory",{directory:22})}async function R(){return u$1("plugin:path|resolve_directory",{directory:23})}async function w(){return u$1("plugin:path|resolve_directory",{directory:10})}async function V(){return u$1("plugin:path|resolve_directory",{directory:17})}var k=t()?"\\":"/",T=t()?";":":";async function W(...i){return u$1("plugin:path|resolve",{paths:i})}async function j(i){return u$1("plugin:path|normalize",{path:i})}async function z(...i){return u$1("plugin:path|join",{paths:i})}async function E(i){return u$1("plugin:path|dirname",{path:i})}async function F(i){return u$1("plugin:path|extname",{path:i})}async function H(i,o){return u$1("plugin:path|basename",{path:i,ext:o})}async function M(i){return u$1("plugin:path|isAbsolute",{path:i})}
6
+
7
+ // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
8
+ /**
9
+ * Reads a file as an UTF-8 encoded string.
10
+ * @example
11
+ * ```typescript
12
+ * import { readTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
13
+ * // Read the text file in the `$APPCONFIG/app.conf` path
14
+ * const contents = await readTextFile('app.conf', { dir: BaseDirectory.AppConfig });
15
+ * ```
16
+ *
17
+ * @since 2.0.0
18
+ */
19
+ async function readTextFile(filePath, options = {}) {
20
+ return await window.__TAURI_INVOKE__("plugin:fs|read_text_file", {
21
+ path: filePath,
22
+ options,
23
+ });
24
+ }
25
+ /**
26
+ * Reads a file as byte array.
27
+ * @example
28
+ * ```typescript
29
+ * import { readBinaryFile, BaseDirectory } from '@tauri-apps/plugin-fs';
30
+ * // Read the image file in the `$RESOURCEDIR/avatar.png` path
31
+ * const contents = await readBinaryFile('avatar.png', { dir: BaseDirectory.Resource });
32
+ * ```
33
+ *
34
+ * @since 2.0.0
35
+ */
36
+ async function readBinaryFile(filePath, options = {}) {
37
+ const arr = await window.__TAURI_INVOKE__("plugin:fs|read_file", {
38
+ path: filePath,
39
+ options,
40
+ });
41
+ return Uint8Array.from(arr);
42
+ }
43
+ /**
44
+ * Writes a UTF-8 text file.
45
+ *
46
+ * @returns A promise indicating the success or failure of the operation.
47
+ *
48
+ * @since 2.0.0
49
+ */
50
+ async function writeTextFile(path, contents, options) {
51
+ if (typeof options === "object") {
52
+ Object.freeze(options);
53
+ }
54
+ if (typeof path === "object") {
55
+ Object.freeze(path);
56
+ }
57
+ const file = { path: "", contents: "" };
58
+ let fileOptions = options;
59
+ if (typeof path === "string") {
60
+ file.path = path;
61
+ }
62
+ else {
63
+ file.path = path.path;
64
+ file.contents = path.contents;
65
+ }
66
+ if (typeof contents === "string") {
67
+ file.contents = contents !== null && contents !== void 0 ? contents : "";
68
+ }
69
+ else {
70
+ fileOptions = contents;
71
+ }
72
+ return await window.__TAURI_INVOKE__("plugin:fs|write_file", {
73
+ path: file.path,
74
+ contents: Array.from(new TextEncoder().encode(file.contents)),
75
+ options: fileOptions,
76
+ });
77
+ }
78
+ /**
79
+ * Writes a byte array content to a file.
80
+ *
81
+ * @returns A promise indicating the success or failure of the operation.
82
+ *
83
+ * @since 2.0.0
84
+ */
85
+ async function writeBinaryFile(path, contents, options) {
86
+ if (typeof options === "object") {
87
+ Object.freeze(options);
88
+ }
89
+ if (typeof path === "object") {
90
+ Object.freeze(path);
91
+ }
92
+ const file = { path: "", contents: [] };
93
+ let fileOptions = options;
94
+ if (typeof path === "string") {
95
+ file.path = path;
96
+ }
97
+ else {
98
+ file.path = path.path;
99
+ file.contents = path.contents;
100
+ }
101
+ if (contents && "dir" in contents) {
102
+ fileOptions = contents;
103
+ }
104
+ else if (typeof path === "string") {
105
+ // @ts-expect-error in this case `contents` is always a BinaryFileContents
106
+ file.contents = contents !== null && contents !== void 0 ? contents : [];
107
+ }
108
+ return await window.__TAURI_INVOKE__("plugin:fs|write_binary_file", {
109
+ path: file.path,
110
+ contents: Array.from(file.contents instanceof ArrayBuffer
111
+ ? new Uint8Array(file.contents)
112
+ : file.contents),
113
+ options: fileOptions,
114
+ });
115
+ }
116
+ /**
117
+ * List directory files.
118
+ * @example
119
+ * ```typescript
120
+ * import { readDir, BaseDirectory } from '@tauri-apps/plugin-fs';
121
+ * // Reads the `$APPDATA/users` directory recursively
122
+ * const entries = await readDir('users', { dir: BaseDirectory.AppData, recursive: true });
123
+ *
124
+ * function processEntries(entries) {
125
+ * for (const entry of entries) {
126
+ * console.log(`Entry: ${entry.path}`);
127
+ * if (entry.children) {
128
+ * processEntries(entry.children)
129
+ * }
130
+ * }
131
+ * }
132
+ * ```
133
+ *
134
+ * @since 2.0.0
135
+ */
136
+ async function readDir(dir, options = {}) {
137
+ return await window.__TAURI_INVOKE__("plugin:fs|read_dir", {
138
+ path: dir,
139
+ options,
140
+ });
141
+ }
142
+ /**
143
+ * Creates a directory.
144
+ * If one of the path's parent components doesn't exist
145
+ * and the `recursive` option isn't set to true, the promise will be rejected.
146
+ * @example
147
+ * ```typescript
148
+ * import { createDir, BaseDirectory } from '@tauri-apps/plugin-fs';
149
+ * // Create the `$APPDATA/users` directory
150
+ * await createDir('users', { dir: BaseDirectory.AppData, recursive: true });
151
+ * ```
152
+ *
153
+ * @returns A promise indicating the success or failure of the operation.
154
+ *
155
+ * @since 2.0.0
156
+ */
157
+ async function createDir(dir, options = {}) {
158
+ return await window.__TAURI_INVOKE__("plugin:fs|create_dir", {
159
+ path: dir,
160
+ options,
161
+ });
162
+ }
163
+ /**
164
+ * Removes a directory.
165
+ * If the directory is not empty and the `recursive` option isn't set to true, the promise will be rejected.
166
+ * @example
167
+ * ```typescript
168
+ * import { removeDir, BaseDirectory } from '@tauri-apps/plugin-fs';
169
+ * // Remove the directory `$APPDATA/users`
170
+ * await removeDir('users', { dir: BaseDirectory.AppData });
171
+ * ```
172
+ *
173
+ * @returns A promise indicating the success or failure of the operation.
174
+ *
175
+ * @since 2.0.0
176
+ */
177
+ async function removeDir(dir, options = {}) {
178
+ return await window.__TAURI_INVOKE__("plugin:fs|remove_dir", {
179
+ path: dir,
180
+ options,
181
+ });
182
+ }
183
+ /**
184
+ * Copies a file to a destination.
185
+ * @example
186
+ * ```typescript
187
+ * import { copyFile, BaseDirectory } from '@tauri-apps/plugin-fs';
188
+ * // Copy the `$APPCONFIG/app.conf` file to `$APPCONFIG/app.conf.bk`
189
+ * await copyFile('app.conf', 'app.conf.bk', { dir: BaseDirectory.AppConfig });
190
+ * ```
191
+ *
192
+ * @returns A promise indicating the success or failure of the operation.
193
+ *
194
+ * @since 2.0.0
195
+ */
196
+ async function copyFile(source, destination, options = {}) {
197
+ return await window.__TAURI_INVOKE__("plugin:fs|copy_file", {
198
+ source,
199
+ destination,
200
+ options,
201
+ });
202
+ }
203
+ /**
204
+ * Removes a file.
205
+ * @example
206
+ * ```typescript
207
+ * import { removeFile, BaseDirectory } from '@tauri-apps/plugin-fs';
208
+ * // Remove the `$APPConfig/app.conf` file
209
+ * await removeFile('app.conf', { dir: BaseDirectory.AppConfig });
210
+ * ```
211
+ *
212
+ * @returns A promise indicating the success or failure of the operation.
213
+ *
214
+ * @since 2.0.0
215
+ */
216
+ async function removeFile(file, options = {}) {
217
+ return await window.__TAURI_INVOKE__("plugin:fs|remove_file", {
218
+ path: file,
219
+ options,
220
+ });
221
+ }
222
+ /**
223
+ * Renames a file.
224
+ * @example
225
+ * ```typescript
226
+ * import { renameFile, BaseDirectory } from '@tauri-apps/plugin-fs';
227
+ * // Rename the `$APPDATA/avatar.png` file
228
+ * await renameFile('avatar.png', 'deleted.png', { dir: BaseDirectory.AppData });
229
+ * ```
230
+ *
231
+ * @returns A promise indicating the success or failure of the operation.
232
+ *
233
+ * @since 2.0.0
234
+ */
235
+ async function renameFile(oldPath, newPath, options = {}) {
236
+ return await window.__TAURI_INVOKE__("plugin:fs|rename_file", {
237
+ oldPath,
238
+ newPath,
239
+ options,
240
+ });
241
+ }
242
+ /**
243
+ * Check if a path exists.
244
+ * @example
245
+ * ```typescript
246
+ * import { exists, BaseDirectory } from '@tauri-apps/plugin-fs';
247
+ * // Check if the `$APPDATA/avatar.png` file exists
248
+ * await exists('avatar.png', { dir: BaseDirectory.AppData });
249
+ * ```
250
+ *
251
+ * @since 2.0.0
252
+ */
253
+ async function exists(path) {
254
+ return await window.__TAURI_INVOKE__("plugin:fs|exists", { path });
255
+ }
256
+ /**
257
+ * Returns the metadata for the given path.
258
+ *
259
+ * @since 2.0.0
260
+ */
261
+ async function metadata(path) {
262
+ return await window
263
+ .__TAURI_INVOKE__("plugin:fs|metadata", {
264
+ path,
265
+ })
266
+ .then((metadata) => {
267
+ const { accessedAtMs, createdAtMs, modifiedAtMs, ...data } = metadata;
268
+ return {
269
+ accessedAt: new Date(accessedAtMs),
270
+ createdAt: new Date(createdAtMs),
271
+ modifiedAt: new Date(modifiedAtMs),
272
+ ...data,
273
+ };
274
+ });
275
+ }
276
+
277
+ export { e as BaseDirectory, e as Dir, copyFile, createDir, exists, metadata, readBinaryFile, readDir, readTextFile, removeDir, removeFile, renameFile, writeBinaryFile, writeTextFile as writeFile, writeTextFile };
278
+ //# sourceMappingURL=index.min.js.map
@@ -0,0 +1,274 @@
1
+ export { BaseDirectory, BaseDirectory as Dir } from '@tauri-apps/api/path';
2
+
3
+ // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
4
+ /**
5
+ * Reads a file as an UTF-8 encoded string.
6
+ * @example
7
+ * ```typescript
8
+ * import { readTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
9
+ * // Read the text file in the `$APPCONFIG/app.conf` path
10
+ * const contents = await readTextFile('app.conf', { dir: BaseDirectory.AppConfig });
11
+ * ```
12
+ *
13
+ * @since 2.0.0
14
+ */
15
+ async function readTextFile(filePath, options = {}) {
16
+ return await window.__TAURI_INVOKE__("plugin:fs|read_text_file", {
17
+ path: filePath,
18
+ options,
19
+ });
20
+ }
21
+ /**
22
+ * Reads a file as byte array.
23
+ * @example
24
+ * ```typescript
25
+ * import { readBinaryFile, BaseDirectory } from '@tauri-apps/plugin-fs';
26
+ * // Read the image file in the `$RESOURCEDIR/avatar.png` path
27
+ * const contents = await readBinaryFile('avatar.png', { dir: BaseDirectory.Resource });
28
+ * ```
29
+ *
30
+ * @since 2.0.0
31
+ */
32
+ async function readBinaryFile(filePath, options = {}) {
33
+ const arr = await window.__TAURI_INVOKE__("plugin:fs|read_file", {
34
+ path: filePath,
35
+ options,
36
+ });
37
+ return Uint8Array.from(arr);
38
+ }
39
+ /**
40
+ * Writes a UTF-8 text file.
41
+ *
42
+ * @returns A promise indicating the success or failure of the operation.
43
+ *
44
+ * @since 2.0.0
45
+ */
46
+ async function writeTextFile(path, contents, options) {
47
+ if (typeof options === "object") {
48
+ Object.freeze(options);
49
+ }
50
+ if (typeof path === "object") {
51
+ Object.freeze(path);
52
+ }
53
+ const file = { path: "", contents: "" };
54
+ let fileOptions = options;
55
+ if (typeof path === "string") {
56
+ file.path = path;
57
+ }
58
+ else {
59
+ file.path = path.path;
60
+ file.contents = path.contents;
61
+ }
62
+ if (typeof contents === "string") {
63
+ file.contents = contents !== null && contents !== void 0 ? contents : "";
64
+ }
65
+ else {
66
+ fileOptions = contents;
67
+ }
68
+ return await window.__TAURI_INVOKE__("plugin:fs|write_file", {
69
+ path: file.path,
70
+ contents: Array.from(new TextEncoder().encode(file.contents)),
71
+ options: fileOptions,
72
+ });
73
+ }
74
+ /**
75
+ * Writes a byte array content to a file.
76
+ *
77
+ * @returns A promise indicating the success or failure of the operation.
78
+ *
79
+ * @since 2.0.0
80
+ */
81
+ async function writeBinaryFile(path, contents, options) {
82
+ if (typeof options === "object") {
83
+ Object.freeze(options);
84
+ }
85
+ if (typeof path === "object") {
86
+ Object.freeze(path);
87
+ }
88
+ const file = { path: "", contents: [] };
89
+ let fileOptions = options;
90
+ if (typeof path === "string") {
91
+ file.path = path;
92
+ }
93
+ else {
94
+ file.path = path.path;
95
+ file.contents = path.contents;
96
+ }
97
+ if (contents && "dir" in contents) {
98
+ fileOptions = contents;
99
+ }
100
+ else if (typeof path === "string") {
101
+ // @ts-expect-error in this case `contents` is always a BinaryFileContents
102
+ file.contents = contents !== null && contents !== void 0 ? contents : [];
103
+ }
104
+ return await window.__TAURI_INVOKE__("plugin:fs|write_binary_file", {
105
+ path: file.path,
106
+ contents: Array.from(file.contents instanceof ArrayBuffer
107
+ ? new Uint8Array(file.contents)
108
+ : file.contents),
109
+ options: fileOptions,
110
+ });
111
+ }
112
+ /**
113
+ * List directory files.
114
+ * @example
115
+ * ```typescript
116
+ * import { readDir, BaseDirectory } from '@tauri-apps/plugin-fs';
117
+ * // Reads the `$APPDATA/users` directory recursively
118
+ * const entries = await readDir('users', { dir: BaseDirectory.AppData, recursive: true });
119
+ *
120
+ * function processEntries(entries) {
121
+ * for (const entry of entries) {
122
+ * console.log(`Entry: ${entry.path}`);
123
+ * if (entry.children) {
124
+ * processEntries(entry.children)
125
+ * }
126
+ * }
127
+ * }
128
+ * ```
129
+ *
130
+ * @since 2.0.0
131
+ */
132
+ async function readDir(dir, options = {}) {
133
+ return await window.__TAURI_INVOKE__("plugin:fs|read_dir", {
134
+ path: dir,
135
+ options,
136
+ });
137
+ }
138
+ /**
139
+ * Creates a directory.
140
+ * If one of the path's parent components doesn't exist
141
+ * and the `recursive` option isn't set to true, the promise will be rejected.
142
+ * @example
143
+ * ```typescript
144
+ * import { createDir, BaseDirectory } from '@tauri-apps/plugin-fs';
145
+ * // Create the `$APPDATA/users` directory
146
+ * await createDir('users', { dir: BaseDirectory.AppData, recursive: true });
147
+ * ```
148
+ *
149
+ * @returns A promise indicating the success or failure of the operation.
150
+ *
151
+ * @since 2.0.0
152
+ */
153
+ async function createDir(dir, options = {}) {
154
+ return await window.__TAURI_INVOKE__("plugin:fs|create_dir", {
155
+ path: dir,
156
+ options,
157
+ });
158
+ }
159
+ /**
160
+ * Removes a directory.
161
+ * If the directory is not empty and the `recursive` option isn't set to true, the promise will be rejected.
162
+ * @example
163
+ * ```typescript
164
+ * import { removeDir, BaseDirectory } from '@tauri-apps/plugin-fs';
165
+ * // Remove the directory `$APPDATA/users`
166
+ * await removeDir('users', { dir: BaseDirectory.AppData });
167
+ * ```
168
+ *
169
+ * @returns A promise indicating the success or failure of the operation.
170
+ *
171
+ * @since 2.0.0
172
+ */
173
+ async function removeDir(dir, options = {}) {
174
+ return await window.__TAURI_INVOKE__("plugin:fs|remove_dir", {
175
+ path: dir,
176
+ options,
177
+ });
178
+ }
179
+ /**
180
+ * Copies a file to a destination.
181
+ * @example
182
+ * ```typescript
183
+ * import { copyFile, BaseDirectory } from '@tauri-apps/plugin-fs';
184
+ * // Copy the `$APPCONFIG/app.conf` file to `$APPCONFIG/app.conf.bk`
185
+ * await copyFile('app.conf', 'app.conf.bk', { dir: BaseDirectory.AppConfig });
186
+ * ```
187
+ *
188
+ * @returns A promise indicating the success or failure of the operation.
189
+ *
190
+ * @since 2.0.0
191
+ */
192
+ async function copyFile(source, destination, options = {}) {
193
+ return await window.__TAURI_INVOKE__("plugin:fs|copy_file", {
194
+ source,
195
+ destination,
196
+ options,
197
+ });
198
+ }
199
+ /**
200
+ * Removes a file.
201
+ * @example
202
+ * ```typescript
203
+ * import { removeFile, BaseDirectory } from '@tauri-apps/plugin-fs';
204
+ * // Remove the `$APPConfig/app.conf` file
205
+ * await removeFile('app.conf', { dir: BaseDirectory.AppConfig });
206
+ * ```
207
+ *
208
+ * @returns A promise indicating the success or failure of the operation.
209
+ *
210
+ * @since 2.0.0
211
+ */
212
+ async function removeFile(file, options = {}) {
213
+ return await window.__TAURI_INVOKE__("plugin:fs|remove_file", {
214
+ path: file,
215
+ options,
216
+ });
217
+ }
218
+ /**
219
+ * Renames a file.
220
+ * @example
221
+ * ```typescript
222
+ * import { renameFile, BaseDirectory } from '@tauri-apps/plugin-fs';
223
+ * // Rename the `$APPDATA/avatar.png` file
224
+ * await renameFile('avatar.png', 'deleted.png', { dir: BaseDirectory.AppData });
225
+ * ```
226
+ *
227
+ * @returns A promise indicating the success or failure of the operation.
228
+ *
229
+ * @since 2.0.0
230
+ */
231
+ async function renameFile(oldPath, newPath, options = {}) {
232
+ return await window.__TAURI_INVOKE__("plugin:fs|rename_file", {
233
+ oldPath,
234
+ newPath,
235
+ options,
236
+ });
237
+ }
238
+ /**
239
+ * Check if a path exists.
240
+ * @example
241
+ * ```typescript
242
+ * import { exists, BaseDirectory } from '@tauri-apps/plugin-fs';
243
+ * // Check if the `$APPDATA/avatar.png` file exists
244
+ * await exists('avatar.png', { dir: BaseDirectory.AppData });
245
+ * ```
246
+ *
247
+ * @since 2.0.0
248
+ */
249
+ async function exists(path) {
250
+ return await window.__TAURI_INVOKE__("plugin:fs|exists", { path });
251
+ }
252
+ /**
253
+ * Returns the metadata for the given path.
254
+ *
255
+ * @since 2.0.0
256
+ */
257
+ async function metadata(path) {
258
+ return await window
259
+ .__TAURI_INVOKE__("plugin:fs|metadata", {
260
+ path,
261
+ })
262
+ .then((metadata) => {
263
+ const { accessedAtMs, createdAtMs, modifiedAtMs, ...data } = metadata;
264
+ return {
265
+ accessedAt: new Date(accessedAtMs),
266
+ createdAt: new Date(createdAtMs),
267
+ modifiedAt: new Date(modifiedAtMs),
268
+ ...data,
269
+ };
270
+ });
271
+ }
272
+
273
+ export { copyFile, createDir, exists, metadata, readBinaryFile, readDir, readTextFile, removeDir, removeFile, renameFile, writeBinaryFile, writeTextFile as writeFile, writeTextFile };
274
+ //# sourceMappingURL=index.mjs.map
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@tauri-apps/plugin-fs",
3
+ "version": "2.0.0-alpha.0",
4
+ "description": "Access the file system.",
5
+ "license": "MIT or APACHE-2.0",
6
+ "authors": [
7
+ "Tauri Programme within The Commons Conservancy"
8
+ ],
9
+ "type": "module",
10
+ "browser": "dist-js/index.min.js",
11
+ "module": "dist-js/index.mjs",
12
+ "types": "dist-js/index.d.ts",
13
+ "exports": {
14
+ "import": "./dist-js/index.mjs",
15
+ "types": "./dist-js/index.d.ts",
16
+ "browser": "./dist-js/index.min.js"
17
+ },
18
+ "files": [
19
+ "dist-js",
20
+ "!dist-js/**/*.map",
21
+ "README.md",
22
+ "LICENSE"
23
+ ],
24
+ "devDependencies": {
25
+ "tslib": "^2.4.1"
26
+ },
27
+ "dependencies": {
28
+ "@tauri-apps/api": "2.0.0-alpha.4"
29
+ },
30
+ "scripts": {
31
+ "build": "rollup -c"
32
+ }
33
+ }