@tauri-apps/plugin-fs 2.0.0-alpha.3 → 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.
package/dist-js/index.mjs DELETED
@@ -1,317 +0,0 @@
1
- export { BaseDirectory, BaseDirectory as Dir } from '@tauri-apps/api/path';
2
- import { invoke } from '@tauri-apps/api/primitives';
3
-
4
- // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
5
- // SPDX-License-Identifier: Apache-2.0
6
- // SPDX-License-Identifier: MIT
7
- /**
8
- * Access the file system.
9
- *
10
- * ## Security
11
- *
12
- * This module prevents path traversal, not allowing absolute paths or parent dir components
13
- * (i.e. "/usr/path/to/file" or "../path/to/file" paths are not allowed).
14
- * Paths accessed with this API must be relative to one of the {@link BaseDirectory | base directories}
15
- * so if you need access to arbitrary filesystem paths, you must write such logic on the core layer instead.
16
- *
17
- * The API has a scope configuration that forces you to restrict the paths that can be accessed using glob patterns.
18
- *
19
- * The scope configuration is an array of glob patterns describing folder paths that are allowed.
20
- * For instance, this scope configuration only allows accessing files on the
21
- * *databases* folder of the {@link path.appDataDir | $APPDATA directory}:
22
- * ```json
23
- * {
24
- * "plugins": {
25
- * "fs": {
26
- * "scope": ["$APPDATA/databases/*"]
27
- * }
28
- * }
29
- * }
30
- * ```
31
- *
32
- * Notice the use of the `$APPDATA` variable. The value is injected at runtime, resolving to the {@link path.appDataDir | app data directory}.
33
- * The available variables are:
34
- * {@link path.appConfigDir | `$APPCONFIG`}, {@link path.appDataDir | `$APPDATA`}, {@link path.appLocalDataDir | `$APPLOCALDATA`},
35
- * {@link path.appCacheDir | `$APPCACHE`}, {@link path.appLogDir | `$APPLOG`},
36
- * {@link path.audioDir | `$AUDIO`}, {@link path.cacheDir | `$CACHE`}, {@link path.configDir | `$CONFIG`}, {@link path.dataDir | `$DATA`},
37
- * {@link path.localDataDir | `$LOCALDATA`}, {@link path.desktopDir | `$DESKTOP`}, {@link path.documentDir | `$DOCUMENT`},
38
- * {@link path.downloadDir | `$DOWNLOAD`}, {@link path.executableDir | `$EXE`}, {@link path.fontDir | `$FONT`}, {@link path.homeDir | `$HOME`},
39
- * {@link path.pictureDir | `$PICTURE`}, {@link path.publicDir | `$PUBLIC`}, {@link path.runtimeDir | `$RUNTIME`},
40
- * {@link path.templateDir | `$TEMPLATE`}, {@link path.videoDir | `$VIDEO`}, {@link path.resourceDir | `$RESOURCE`},
41
- * {@link os.tempdir | `$TEMP`}.
42
- *
43
- * Trying to execute any API with a URL not configured on the scope results in a promise rejection due to denied access.
44
- *
45
- * Note that this scope applies to **all** APIs on this module.
46
- *
47
- * @module
48
- */
49
- /**
50
- * Reads a file as an UTF-8 encoded string.
51
- * @example
52
- * ```typescript
53
- * import { readTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
54
- * // Read the text file in the `$APPCONFIG/app.conf` path
55
- * const contents = await readTextFile('app.conf', { dir: BaseDirectory.AppConfig });
56
- * ```
57
- *
58
- * @since 2.0.0
59
- */
60
- async function readTextFile(filePath, options = {}) {
61
- return await invoke("plugin:fs|read_text_file", {
62
- path: filePath,
63
- options,
64
- });
65
- }
66
- /**
67
- * Reads a file as byte array.
68
- * @example
69
- * ```typescript
70
- * import { readBinaryFile, BaseDirectory } from '@tauri-apps/plugin-fs';
71
- * // Read the image file in the `$RESOURCEDIR/avatar.png` path
72
- * const contents = await readBinaryFile('avatar.png', { dir: BaseDirectory.Resource });
73
- * ```
74
- *
75
- * @since 2.0.0
76
- */
77
- async function readBinaryFile(filePath, options = {}) {
78
- const arr = await invoke("plugin:fs|read_file", {
79
- path: filePath,
80
- options,
81
- });
82
- return Uint8Array.from(arr);
83
- }
84
- /**
85
- * Writes a UTF-8 text file.
86
- *
87
- * @returns A promise indicating the success or failure of the operation.
88
- *
89
- * @since 2.0.0
90
- */
91
- async function writeTextFile(path, contents, options) {
92
- if (typeof options === "object") {
93
- Object.freeze(options);
94
- }
95
- if (typeof path === "object") {
96
- Object.freeze(path);
97
- }
98
- const file = { path: "", contents: "" };
99
- let fileOptions = options;
100
- if (typeof path === "string") {
101
- file.path = path;
102
- }
103
- else {
104
- file.path = path.path;
105
- file.contents = path.contents;
106
- }
107
- if (typeof contents === "string") {
108
- file.contents = contents !== null && contents !== void 0 ? contents : "";
109
- }
110
- else {
111
- fileOptions = contents;
112
- }
113
- return await invoke("plugin:fs|write_file", {
114
- path: file.path,
115
- contents: Array.from(new TextEncoder().encode(file.contents)),
116
- options: fileOptions,
117
- });
118
- }
119
- /**
120
- * Writes a byte array content to a file.
121
- *
122
- * @returns A promise indicating the success or failure of the operation.
123
- *
124
- * @since 2.0.0
125
- */
126
- async function writeBinaryFile(path, contents, options) {
127
- if (typeof options === "object") {
128
- Object.freeze(options);
129
- }
130
- if (typeof path === "object") {
131
- Object.freeze(path);
132
- }
133
- const file = { path: "", contents: [] };
134
- let fileOptions = options;
135
- if (typeof path === "string") {
136
- file.path = path;
137
- }
138
- else {
139
- file.path = path.path;
140
- file.contents = path.contents;
141
- }
142
- if (contents && "dir" in contents) {
143
- fileOptions = contents;
144
- }
145
- else if (typeof path === "string") {
146
- // @ts-expect-error in this case `contents` is always a BinaryFileContents
147
- file.contents = contents !== null && contents !== void 0 ? contents : [];
148
- }
149
- return await invoke("plugin:fs|write_file", {
150
- path: file.path,
151
- contents: Array.from(file.contents instanceof ArrayBuffer
152
- ? new Uint8Array(file.contents)
153
- : file.contents),
154
- options: fileOptions,
155
- });
156
- }
157
- /**
158
- * List directory files.
159
- * @example
160
- * ```typescript
161
- * import { readDir, BaseDirectory } from '@tauri-apps/plugin-fs';
162
- * // Reads the `$APPDATA/users` directory recursively
163
- * const entries = await readDir('users', { dir: BaseDirectory.AppData, recursive: true });
164
- *
165
- * function processEntries(entries) {
166
- * for (const entry of entries) {
167
- * console.log(`Entry: ${entry.path}`);
168
- * if (entry.children) {
169
- * processEntries(entry.children)
170
- * }
171
- * }
172
- * }
173
- * ```
174
- *
175
- * @since 2.0.0
176
- */
177
- async function readDir(dir, options = {}) {
178
- return await invoke("plugin:fs|read_dir", {
179
- path: dir,
180
- options,
181
- });
182
- }
183
- /**
184
- * Creates a directory.
185
- * If one of the path's parent components doesn't exist
186
- * and the `recursive` option isn't set to true, the promise will be rejected.
187
- * @example
188
- * ```typescript
189
- * import { createDir, BaseDirectory } from '@tauri-apps/plugin-fs';
190
- * // Create the `$APPDATA/users` directory
191
- * await createDir('users', { dir: BaseDirectory.AppData, recursive: true });
192
- * ```
193
- *
194
- * @returns A promise indicating the success or failure of the operation.
195
- *
196
- * @since 2.0.0
197
- */
198
- async function createDir(dir, options = {}) {
199
- return await invoke("plugin:fs|create_dir", {
200
- path: dir,
201
- options,
202
- });
203
- }
204
- /**
205
- * Removes a directory.
206
- * If the directory is not empty and the `recursive` option isn't set to true, the promise will be rejected.
207
- * @example
208
- * ```typescript
209
- * import { removeDir, BaseDirectory } from '@tauri-apps/plugin-fs';
210
- * // Remove the directory `$APPDATA/users`
211
- * await removeDir('users', { dir: BaseDirectory.AppData });
212
- * ```
213
- *
214
- * @returns A promise indicating the success or failure of the operation.
215
- *
216
- * @since 2.0.0
217
- */
218
- async function removeDir(dir, options = {}) {
219
- return await invoke("plugin:fs|remove_dir", {
220
- path: dir,
221
- options,
222
- });
223
- }
224
- /**
225
- * Copies a file to a destination.
226
- * @example
227
- * ```typescript
228
- * import { copyFile, BaseDirectory } from '@tauri-apps/plugin-fs';
229
- * // Copy the `$APPCONFIG/app.conf` file to `$APPCONFIG/app.conf.bk`
230
- * await copyFile('app.conf', 'app.conf.bk', { dir: BaseDirectory.AppConfig });
231
- * ```
232
- *
233
- * @returns A promise indicating the success or failure of the operation.
234
- *
235
- * @since 2.0.0
236
- */
237
- async function copyFile(source, destination, options = {}) {
238
- return await invoke("plugin:fs|copy_file", {
239
- source,
240
- destination,
241
- options,
242
- });
243
- }
244
- /**
245
- * Removes a file.
246
- * @example
247
- * ```typescript
248
- * import { removeFile, BaseDirectory } from '@tauri-apps/plugin-fs';
249
- * // Remove the `$APPConfig/app.conf` file
250
- * await removeFile('app.conf', { dir: BaseDirectory.AppConfig });
251
- * ```
252
- *
253
- * @returns A promise indicating the success or failure of the operation.
254
- *
255
- * @since 2.0.0
256
- */
257
- async function removeFile(file, options = {}) {
258
- return await invoke("plugin:fs|remove_file", {
259
- path: file,
260
- options,
261
- });
262
- }
263
- /**
264
- * Renames a file.
265
- * @example
266
- * ```typescript
267
- * import { renameFile, BaseDirectory } from '@tauri-apps/plugin-fs';
268
- * // Rename the `$APPDATA/avatar.png` file
269
- * await renameFile('avatar.png', 'deleted.png', { dir: BaseDirectory.AppData });
270
- * ```
271
- *
272
- * @returns A promise indicating the success or failure of the operation.
273
- *
274
- * @since 2.0.0
275
- */
276
- async function renameFile(oldPath, newPath, options = {}) {
277
- return await invoke("plugin:fs|rename_file", {
278
- oldPath,
279
- newPath,
280
- options,
281
- });
282
- }
283
- /**
284
- * Check if a path exists.
285
- * @example
286
- * ```typescript
287
- * import { exists, BaseDirectory } from '@tauri-apps/plugin-fs';
288
- * // Check if the `$APPDATA/avatar.png` file exists
289
- * await exists('avatar.png', { dir: BaseDirectory.AppData });
290
- * ```
291
- *
292
- * @since 2.0.0
293
- */
294
- async function exists(path) {
295
- return await invoke("plugin:fs|exists", { path });
296
- }
297
- /**
298
- * Returns the metadata for the given path.
299
- *
300
- * @since 2.0.0
301
- */
302
- async function metadata(path) {
303
- return await invoke("plugin:fs|metadata", {
304
- path,
305
- }).then((metadata) => {
306
- const { accessedAtMs, createdAtMs, modifiedAtMs, ...data } = metadata;
307
- return {
308
- accessedAt: new Date(accessedAtMs),
309
- createdAt: new Date(createdAtMs),
310
- modifiedAt: new Date(modifiedAtMs),
311
- ...data,
312
- };
313
- });
314
- }
315
-
316
- export { copyFile, createDir, exists, metadata, readBinaryFile, readDir, readTextFile, removeDir, removeFile, renameFile, writeBinaryFile, writeTextFile as writeFile, writeTextFile };
317
- //# sourceMappingURL=index.mjs.map