2022-08-28 21:13:21 +03:00
// Copyright 2019-2022 Tauri Programme within The Commons Conservancy
2021-04-11 01:09:09 +03:00
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
2021-05-05 20:36:40 +03:00
/ * *
* Access the file system .
2021-05-18 04:33:09 +03:00
*
2022-07-11 16:14:31 +03:00
* This package is also accessible with ` window.__TAURI__.fs ` when [ ` build.withGlobalTauri ` ] ( https : //tauri.app/v1/api/config/#buildconfig.withglobaltauri) in `tauri.conf.json` is set to `true`.
2021-05-18 04:33:09 +03:00
*
2022-07-11 16:14:31 +03:00
* The APIs must be added to [ ` tauri.allowlist.fs ` ] ( https : //tauri.app/v1/api/config/#allowlistconfig.fs) in `tauri.conf.json`:
2021-05-18 04:33:09 +03:00
* ` ` ` json
* {
* "tauri" : {
* "allowlist" : {
* "fs" : {
* "all" : true , // enable all FS APIs
2022-01-09 22:24:44 +03:00
* "readFile" : true ,
2021-05-18 04:33:09 +03:00
* "writeFile" : true ,
* "readDir" : true ,
* "copyFile" : true ,
* "createDir" : true ,
* "removeDir" : true ,
* "removeFile" : true ,
2022-09-15 17:28:52 +03:00
* "renameFile" : true ,
* "exists" : true
2021-05-18 04:33:09 +03:00
* }
* }
* }
* }
* ` ` `
* It is recommended to allowlist only the APIs you use for optimal bundle size and security .
2022-02-11 21:40:13 +03:00
*
* # # Security
*
* This module prevents path traversal , not allowing absolute paths or parent dir components
* ( i . e . "/usr/path/to/file" or "../path/to/file" paths are not allowed ) .
2022-06-29 06:58:07 +03:00
* Paths accessed with this API must be relative to one of the { @link BaseDirectory | base directories }
2022-02-11 21:40:13 +03:00
* so if you need access to arbitrary filesystem paths , you must write such logic on the core layer instead .
*
* The API has a scope configuration that forces you to restrict the paths that can be accessed using glob patterns .
*
* The scope configuration is an array of glob patterns describing folder paths that are allowed .
* For instance , this scope configuration only allows accessing files on the
2022-09-29 01:34:09 +03:00
* * databases * folder of the { @link path . appDataDir | $APPDATA directory } :
2022-02-11 21:40:13 +03:00
* ` ` ` json
* {
* "tauri" : {
* "allowlist" : {
* "fs" : {
2022-09-29 01:34:09 +03:00
* "scope" : [ "$APPDATA/databases/*" ]
2022-02-11 21:40:13 +03:00
* }
* }
* }
* }
* ` ` `
*
2022-09-29 01:34:09 +03:00
* Notice the use of the ` $ APPDATA ` variable . The value is injected at runtime , resolving to the { @link path . appDataDir | app data directory } .
2022-02-11 21:40:13 +03:00
* The available variables are :
2022-09-29 01:34:09 +03:00
* { @link path . appConfigDir | ` $ APPCONFIG ` } , { @link path . appDataDir | ` $ APPDATA ` } , { @link path . appLocalDataDir | ` $ APPLOCALDATA ` } ,
* { @link path . appCacheDir | ` $ APPCACHE ` } , { @link path . appLogDir | ` $ APPLOG ` } ,
2022-06-29 06:58:07 +03:00
* { @link path . audioDir | ` $ AUDIO ` } , { @link path . cacheDir | ` $ CACHE ` } , { @link path . configDir | ` $ CONFIG ` } , { @link path . dataDir | ` $ DATA ` } ,
* { @link path . localDataDir | ` $ LOCALDATA ` } , { @link path . desktopDir | ` $ DESKTOP ` } , { @link path . documentDir | ` $ DOCUMENT ` } ,
* { @link path . downloadDir | ` $ DOWNLOAD ` } , { @link path . executableDir | ` $ EXE ` } , { @link path . fontDir | ` $ FONT ` } , { @link path . homeDir | ` $ HOME ` } ,
* { @link path . pictureDir | ` $ PICTURE ` } , { @link path . publicDir | ` $ PUBLIC ` } , { @link path . runtimeDir | ` $ RUNTIME ` } ,
* { @link path . templateDir | ` $ TEMPLATE ` } , { @link path . videoDir | ` $ VIDEO ` } , { @link path . resourceDir | ` $ RESOURCE ` } , { @link path . appDir | ` $ APP ` } ,
* { @link path . logDir | ` $ LOG ` } , { @link os . tempdir | ` $ TEMP ` } .
2022-02-11 21:40:13 +03:00
*
* Trying to execute any API with a URL not configured on the scope results in a promise rejection due to denied access .
*
* Note that this scope applies to * * all * * APIs on this module .
*
2021-07-21 07:23:16 +03:00
* @module
2021-05-05 20:36:40 +03:00
* /
2021-03-07 05:19:12 +03:00
import { invokeTauriCommand } from './helpers/tauri'
2020-07-13 01:34:44 +03:00
2022-09-12 22:45:57 +03:00
/ * *
* @since 1.0 . 0
* /
2020-07-13 01:34:44 +03:00
export enum BaseDirectory {
Audio = 1 ,
Cache ,
Config ,
Data ,
LocalData ,
Desktop ,
Document ,
Download ,
Executable ,
Font ,
Home ,
Picture ,
Public ,
Runtime ,
Template ,
Video ,
Resource ,
2021-03-23 04:13:12 +03:00
App ,
2022-03-24 20:12:17 +03:00
Log ,
2022-09-29 01:34:09 +03:00
Temp ,
AppConfig ,
AppData ,
AppLocalData ,
AppCache ,
AppLog
2020-07-13 01:34:44 +03:00
}
2022-09-12 22:45:57 +03:00
/ * *
* @since 1.0 . 0
* /
2021-05-17 18:54:56 +03:00
interface FsOptions {
2020-07-13 01:34:44 +03:00
dir? : BaseDirectory
2022-05-29 16:10:41 +03:00
// note that adding fields here needs a change in the writeBinaryFile check
2020-07-13 01:34:44 +03:00
}
2022-09-12 22:45:57 +03:00
/ * *
* @since 1.0 . 0
* /
2021-05-17 18:54:56 +03:00
interface FsDirOptions {
2021-01-12 10:16:45 +03:00
dir? : BaseDirectory
recursive? : boolean
}
2022-09-12 22:45:57 +03:00
/ * *
* Options object used to write a UTF - 8 string to a file .
*
* @since 1.0 . 0
* /
2021-05-17 18:54:56 +03:00
interface FsTextFileOption {
2022-01-09 22:24:44 +03:00
/** Path to the file to write. */
2020-07-13 01:34:44 +03:00
path : string
2022-01-09 22:24:44 +03:00
/** The UTF-8 string to write to the file. */
2020-07-13 01:34:44 +03:00
contents : string
}
2022-07-05 23:40:36 +03:00
type BinaryFileContents = Iterable < number > | ArrayLike < number > | ArrayBuffer
2022-05-29 16:10:41 +03:00
2022-09-12 22:45:57 +03:00
/ * *
* Options object used to write a binary data to a file .
*
* @since 1.0 . 0
* /
2021-05-17 18:54:56 +03:00
interface FsBinaryFileOption {
2022-01-09 22:24:44 +03:00
/** Path to the file to write. */
2020-07-13 01:34:44 +03:00
path : string
2022-01-09 22:24:44 +03:00
/** The byte array contents. */
2022-05-29 16:10:41 +03:00
contents : BinaryFileContents
2020-07-13 01:34:44 +03:00
}
2022-09-12 22:45:57 +03:00
/ * *
* @since 1.0 . 0
* /
2021-05-17 18:54:56 +03:00
interface FileEntry {
2020-07-13 01:34:44 +03:00
path : string
2021-04-13 04:44:50 +03:00
/ * *
* Name of the directory / file
* can be null if the path terminates with ` .. `
* /
2020-07-13 01:34:44 +03:00
name? : string
2021-04-13 04:44:50 +03:00
/** Children of this entry if it's a directory; null otherwise */
2020-07-13 01:34:44 +03:00
children? : FileEntry [ ]
}
2020-06-27 18:20:00 +03:00
/ * *
2022-01-09 22:24:44 +03:00
* Reads a file as an UTF - 8 encoded string .
2022-06-30 23:52:43 +03:00
* @example
2022-06-13 03:44:33 +03:00
* ` ` ` typescript
* import { readTextFile , BaseDirectory } from '@tauri-apps/api/fs' ;
2022-09-29 01:34:09 +03:00
* // Read the text file in the `$APPCONFIG/app.conf` path
* const contents = await readTextFile ( 'app.conf' , { dir : BaseDirectory.AppConfig } ) ;
2022-06-13 03:44:33 +03:00
* ` ` `
2021-04-13 04:44:50 +03:00
*
2022-09-12 22:45:57 +03:00
* @since 1.0 . 0
2020-06-27 18:20:00 +03:00
* /
2020-08-08 03:56:29 +03:00
async function readTextFile (
filePath : string ,
options : FsOptions = { }
) : Promise < string > {
2022-03-07 17:34:33 +03:00
return invokeTauriCommand < string > ( {
2021-02-16 07:23:15 +03:00
__tauriModule : 'Fs' ,
2021-02-12 08:42:40 +03:00
message : {
2022-03-07 17:34:33 +03:00
cmd : 'readTextFile' ,
2021-02-12 08:42:40 +03:00
path : filePath ,
options
}
2022-03-07 17:34:33 +03:00
} )
2020-06-27 18:20:00 +03:00
}
/ * *
2021-05-05 20:36:40 +03:00
* Reads a file as byte array .
2022-06-30 23:52:43 +03:00
* @example
2022-06-13 03:44:33 +03:00
* ` ` ` typescript
* import { readBinaryFile , BaseDirectory } from '@tauri-apps/api/fs' ;
2022-06-30 23:52:43 +03:00
* // Read the image file in the `$RESOURCEDIR/avatar.png` path
2022-06-13 03:44:33 +03:00
* const contents = await readBinaryFile ( 'avatar.png' , { dir : BaseDirectory.Resource } ) ;
* ` ` `
2021-04-13 04:44:50 +03:00
*
2022-09-12 22:45:57 +03:00
* @since 1.0 . 0
2020-06-27 18:20:00 +03:00
* /
2020-08-08 03:56:29 +03:00
async function readBinaryFile (
filePath : string ,
options : FsOptions = { }
2022-02-05 04:18:23 +03:00
) : Promise < Uint8Array > {
const arr = await invokeTauriCommand < number [ ] > ( {
2021-02-16 07:23:15 +03:00
__tauriModule : 'Fs' ,
2021-02-12 08:42:40 +03:00
message : {
2022-01-09 22:24:44 +03:00
cmd : 'readFile' ,
2021-02-12 08:42:40 +03:00
path : filePath ,
options
}
2020-06-27 18:20:00 +03:00
} )
2022-02-05 04:18:23 +03:00
return Uint8Array . from ( arr )
2020-06-27 18:20:00 +03:00
}
/ * *
2022-01-09 22:24:44 +03:00
* Writes a UTF - 8 text file .
2022-06-30 23:52:43 +03:00
* @example
2022-06-13 03:44:33 +03:00
* ` ` ` typescript
* import { writeTextFile , BaseDirectory } from '@tauri-apps/api/fs' ;
2022-09-29 01:34:09 +03:00
* // Write a text file to the `$APPCONFIG/app.conf` path
* await writeTextFile ( 'app.conf' , 'file contents' , { dir : BaseDirectory.AppConfig } ) ;
2022-06-13 03:44:33 +03:00
* ` ` `
2020-06-27 18:20:00 +03:00
*
2022-09-12 22:45:57 +03:00
* @since 1.0 . 0
2022-05-29 16:10:41 +03:00
* /
async function writeTextFile (
path : string ,
contents : string ,
options? : FsOptions
) : Promise < void >
/ * *
* Writes a UTF - 8 text file .
2022-06-30 23:52:43 +03:00
* @example
2022-06-13 03:44:33 +03:00
* ` ` ` typescript
* import { writeTextFile , BaseDirectory } from '@tauri-apps/api/fs' ;
2022-09-29 01:34:09 +03:00
* // Write a text file to the `$APPCONFIG/app.conf` path
* await writeTextFile ( { path : 'app.conf' , contents : 'file contents' } , { dir : BaseDirectory.AppConfig } ) ;
2022-06-13 03:44:33 +03:00
* ` ` `
2021-05-05 20:36:40 +03:00
* @returns A promise indicating the success or failure of the operation .
2022-09-12 22:45:57 +03:00
*
* @since 1.0 . 0
2020-06-27 18:20:00 +03:00
* /
2022-05-29 16:10:41 +03:00
async function writeTextFile (
2020-08-19 05:36:46 +03:00
file : FsTextFileOption ,
2022-05-29 16:10:41 +03:00
options? : FsOptions
) : Promise < void >
/ * *
* Writes a UTF - 8 text file .
*
* @returns A promise indicating the success or failure of the operation .
2022-09-12 22:45:57 +03:00
*
* @since 1.0 . 0
2022-05-29 16:10:41 +03:00
* /
async function writeTextFile (
path : string | FsTextFileOption ,
contents? : string | FsOptions ,
options? : FsOptions
2020-08-19 05:36:46 +03:00
) : Promise < void > {
2020-06-27 18:20:00 +03:00
if ( typeof options === 'object' ) {
Object . freeze ( options )
}
2022-05-29 16:10:41 +03:00
if ( typeof path === 'object' ) {
Object . freeze ( path )
}
const file : FsTextFileOption = { path : '' , contents : '' }
let fileOptions : FsOptions | undefined = options
if ( typeof path === 'string' ) {
file . path = path
} else {
file . path = path . path
file . contents = path . contents
}
if ( typeof contents === 'string' ) {
file . contents = contents ? ? ''
} else {
fileOptions = contents
2020-06-27 18:20:00 +03:00
}
2021-03-07 05:19:12 +03:00
return invokeTauriCommand ( {
2021-02-16 07:23:15 +03:00
__tauriModule : 'Fs' ,
2021-02-12 08:42:40 +03:00
message : {
cmd : 'writeFile' ,
path : file.path ,
2022-01-09 22:24:44 +03:00
contents : Array.from ( new TextEncoder ( ) . encode ( file . contents ) ) ,
2022-05-29 16:10:41 +03:00
options : fileOptions
2021-02-12 08:42:40 +03:00
}
2020-06-27 18:20:00 +03:00
} )
}
/ * *
2022-01-09 22:24:44 +03:00
* Writes a byte array content to a file .
2022-06-30 23:52:43 +03:00
* @example
2022-06-13 03:44:33 +03:00
* ` ` ` typescript
* import { writeBinaryFile , BaseDirectory } from '@tauri-apps/api/fs' ;
2022-09-29 01:34:09 +03:00
* // Write a binary file to the `$APPDATA/avatar.png` path
* await writeBinaryFile ( 'avatar.png' , new Uint8Array ( [ ] ) , { dir : BaseDirectory.AppData } ) ;
2022-06-13 03:44:33 +03:00
* ` ` `
2020-06-27 18:20:00 +03:00
*
2022-05-29 16:10:41 +03:00
* @param options Configuration object .
* @returns A promise indicating the success or failure of the operation .
2022-09-12 22:45:57 +03:00
*
* @since 1.0 . 0
2022-05-29 16:10:41 +03:00
* /
async function writeBinaryFile (
path : string ,
contents : BinaryFileContents ,
options? : FsOptions
) : Promise < void >
/ * *
* Writes a byte array content to a file .
2022-06-30 23:52:43 +03:00
* @example
2022-06-13 03:44:33 +03:00
* ` ` ` typescript
* import { writeBinaryFile , BaseDirectory } from '@tauri-apps/api/fs' ;
2022-09-29 01:34:09 +03:00
* // Write a binary file to the `$APPDATA/avatar.png` path
* await writeBinaryFile ( { path : 'avatar.png' , contents : new Uint8Array ( [ ] ) } , { dir : BaseDirectory.AppData } ) ;
2022-06-13 03:44:33 +03:00
* ` ` `
2022-05-29 16:10:41 +03:00
*
* @param file The object containing the file path and contents .
2021-05-05 20:36:40 +03:00
* @param options Configuration object .
* @returns A promise indicating the success or failure of the operation .
2022-09-12 22:45:57 +03:00
*
* @since 1.0 . 0
2020-06-27 18:20:00 +03:00
* /
2020-08-19 05:36:46 +03:00
async function writeBinaryFile (
file : FsBinaryFileOption ,
2022-05-29 16:10:41 +03:00
options? : FsOptions
) : Promise < void >
/ * *
* Writes a byte array content to a file .
*
* @returns A promise indicating the success or failure of the operation .
2022-09-12 22:45:57 +03:00
*
* @since 1.0 . 0
2022-05-29 16:10:41 +03:00
* /
async function writeBinaryFile (
path : string | FsBinaryFileOption ,
contents? : BinaryFileContents | FsOptions ,
options? : FsOptions
2020-08-19 05:36:46 +03:00
) : Promise < void > {
2020-06-27 18:20:00 +03:00
if ( typeof options === 'object' ) {
Object . freeze ( options )
}
2022-05-29 16:10:41 +03:00
if ( typeof path === 'object' ) {
Object . freeze ( path )
}
const file : FsBinaryFileOption = { path : '' , contents : [ ] }
let fileOptions : FsOptions | undefined = options
if ( typeof path === 'string' ) {
file . path = path
} else {
file . path = path . path
file . contents = path . contents
}
if ( contents && 'dir' in contents ) {
fileOptions = contents
2022-06-16 17:56:13 +03:00
} else if ( typeof path === 'string' ) {
2022-05-29 16:10:41 +03:00
// @ts-expect-error
file . contents = contents ? ? [ ]
2020-06-27 18:20:00 +03:00
}
2021-03-07 05:19:12 +03:00
return invokeTauriCommand ( {
2021-02-16 07:23:15 +03:00
__tauriModule : 'Fs' ,
2021-02-12 08:42:40 +03:00
message : {
2022-01-09 22:24:44 +03:00
cmd : 'writeFile' ,
2021-02-12 08:42:40 +03:00
path : file.path ,
2022-07-05 23:40:36 +03:00
contents : Array.from (
file . contents instanceof ArrayBuffer
? new Uint8Array ( file . contents )
: file . contents
) ,
2022-05-29 16:10:41 +03:00
options : fileOptions
2021-02-12 08:42:40 +03:00
}
2020-06-27 18:20:00 +03:00
} )
}
/ * *
2021-04-13 04:44:50 +03:00
* List directory files .
2022-06-30 23:52:43 +03:00
* @example
2022-06-13 03:44:33 +03:00
* ` ` ` typescript
* import { readDir , BaseDirectory } from '@tauri-apps/api/fs' ;
2022-09-29 01:34:09 +03:00
* // Reads the `$APPDATA/users` directory recursively
* const entries = await readDir ( 'users' , { dir : BaseDirectory.AppData , recursive : true } ) ;
2022-06-13 03:44:33 +03:00
*
* function processEntries ( entries ) {
* for ( const entry of entries ) {
* console . log ( ` Entry: ${ entry . path } ` ) ;
2022-06-28 23:56:44 +03:00
* if ( entry . children ) {
2022-06-13 03:44:33 +03:00
* processEntries ( entry . children )
* }
* }
* }
* ` ` `
2020-06-27 18:20:00 +03:00
*
2022-09-12 22:45:57 +03:00
* @since 1.0 . 0
2020-06-27 18:20:00 +03:00
* /
2020-08-19 05:36:46 +03:00
async function readDir (
dir : string ,
2021-01-12 10:16:45 +03:00
options : FsDirOptions = { }
2020-08-19 05:36:46 +03:00
) : Promise < FileEntry [ ] > {
2021-03-07 05:19:12 +03:00
return invokeTauriCommand ( {
2021-02-16 07:23:15 +03:00
__tauriModule : 'Fs' ,
2021-02-12 08:42:40 +03:00
message : {
cmd : 'readDir' ,
path : dir ,
options
}
2020-06-27 18:20:00 +03:00
} )
}
/ * *
2021-04-13 04:44:50 +03:00
* Creates a directory .
2020-06-27 18:20:00 +03:00
* If one of the path 's parent components doesn' t exist
2021-05-05 20:36:40 +03:00
* and the ` recursive ` option isn ' t set to true , the promise will be rejected .
2022-06-30 23:52:43 +03:00
* @example
2022-06-13 03:44:33 +03:00
* ` ` ` typescript
* import { createDir , BaseDirectory } from '@tauri-apps/api/fs' ;
2022-09-29 01:34:09 +03:00
* // Create the `$APPDATA/users` directory
* await createDir ( 'users' , { dir : BaseDirectory.AppData , recursive : true } ) ;
2022-06-13 03:44:33 +03:00
* ` ` `
2020-06-27 18:20:00 +03:00
*
2021-05-05 20:36:40 +03:00
* @returns A promise indicating the success or failure of the operation .
2022-09-12 22:45:57 +03:00
*
* @since 1.0 . 0
2020-06-27 18:20:00 +03:00
* /
2021-01-12 10:16:45 +03:00
async function createDir (
dir : string ,
options : FsDirOptions = { }
) : Promise < void > {
2021-03-07 05:19:12 +03:00
return invokeTauriCommand ( {
2021-02-16 07:23:15 +03:00
__tauriModule : 'Fs' ,
2021-02-12 08:42:40 +03:00
message : {
cmd : 'createDir' ,
path : dir ,
options
}
2020-06-27 18:20:00 +03:00
} )
}
/ * *
2021-04-13 04:44:50 +03:00
* Removes a directory .
2021-05-05 20:36:40 +03:00
* If the directory is not empty and the ` recursive ` option isn ' t set to true , the promise will be rejected .
2022-06-30 23:52:43 +03:00
* @example
2022-06-13 03:44:33 +03:00
* ` ` ` typescript
* import { removeDir , BaseDirectory } from '@tauri-apps/api/fs' ;
2022-09-29 01:34:09 +03:00
* // Remove the directory `$APPDATA/users`
* await removeDir ( 'users' , { dir : BaseDirectory.AppData } ) ;
2022-06-13 03:44:33 +03:00
* ` ` `
2020-06-27 18:20:00 +03:00
*
2021-05-05 20:36:40 +03:00
* @returns A promise indicating the success or failure of the operation .
2022-09-12 22:45:57 +03:00
*
* @since 1.0 . 0
2020-06-27 18:20:00 +03:00
* /
2021-01-12 10:16:45 +03:00
async function removeDir (
dir : string ,
options : FsDirOptions = { }
) : Promise < void > {
2021-03-07 05:19:12 +03:00
return invokeTauriCommand ( {
2021-02-16 07:23:15 +03:00
__tauriModule : 'Fs' ,
2021-02-12 08:42:40 +03:00
message : {
cmd : 'removeDir' ,
path : dir ,
options
}
2020-06-27 18:20:00 +03:00
} )
}
/ * *
2022-09-03 07:03:02 +03:00
* Copies a file to a destination .
2022-06-30 23:52:43 +03:00
* @example
2022-06-13 03:44:33 +03:00
* ` ` ` typescript
* import { copyFile , BaseDirectory } from '@tauri-apps/api/fs' ;
2022-09-29 01:34:09 +03:00
* // Copy the `$APPCONFIG/app.conf` file to `$APPCONFIG/app.conf.bk`
* await copyFile ( 'app.conf' , 'app.conf.bk' , { dir : BaseDirectory.AppConfig } ) ;
2022-06-13 03:44:33 +03:00
* ` ` `
2020-06-27 18:20:00 +03:00
*
2021-05-05 20:36:40 +03:00
* @returns A promise indicating the success or failure of the operation .
2022-09-12 22:45:57 +03:00
*
* @since 1.0 . 0
2020-06-27 18:20:00 +03:00
* /
2020-08-19 05:36:46 +03:00
async function copyFile (
source : string ,
destination : string ,
options : FsOptions = { }
) : Promise < void > {
2021-03-07 05:19:12 +03:00
return invokeTauriCommand ( {
2021-02-16 07:23:15 +03:00
__tauriModule : 'Fs' ,
2021-02-12 08:42:40 +03:00
message : {
cmd : 'copyFile' ,
source ,
destination ,
options
}
2020-06-27 18:20:00 +03:00
} )
}
/ * *
2021-04-13 04:44:50 +03:00
* Removes a file .
2022-06-30 23:52:43 +03:00
* @example
2022-06-13 03:44:33 +03:00
* ` ` ` typescript
* import { removeFile , BaseDirectory } from '@tauri-apps/api/fs' ;
2022-09-29 01:34:09 +03:00
* // Remove the `$APPConfig/app.conf` file
* await removeFile ( 'app.conf' , { dir : BaseDirectory.AppConfig } ) ;
2022-06-13 03:44:33 +03:00
* ` ` `
2020-06-27 18:20:00 +03:00
*
2021-05-05 20:36:40 +03:00
* @returns A promise indicating the success or failure of the operation .
2022-09-12 22:45:57 +03:00
*
* @since 1.0 . 0
2020-06-27 18:20:00 +03:00
* /
2020-08-19 05:36:46 +03:00
async function removeFile (
file : string ,
options : FsOptions = { }
) : Promise < void > {
2021-03-07 05:19:12 +03:00
return invokeTauriCommand ( {
2021-02-16 07:23:15 +03:00
__tauriModule : 'Fs' ,
2021-02-12 08:42:40 +03:00
message : {
cmd : 'removeFile' ,
path : file ,
2022-09-03 15:38:47 +03:00
options
2021-02-12 08:42:40 +03:00
}
2020-06-27 18:20:00 +03:00
} )
}
/ * *
2021-05-05 20:36:40 +03:00
* Renames a file .
2022-06-30 23:52:43 +03:00
* @example
2022-06-13 03:44:33 +03:00
* ` ` ` typescript
* import { renameFile , BaseDirectory } from '@tauri-apps/api/fs' ;
2022-09-29 01:34:09 +03:00
* // Rename the `$APPDATA/avatar.png` file
* await renameFile ( 'avatar.png' , 'deleted.png' , { dir : BaseDirectory.AppData } ) ;
2022-06-13 03:44:33 +03:00
* ` ` `
2020-06-27 18:20:00 +03:00
*
2021-05-05 20:36:40 +03:00
* @returns A promise indicating the success or failure of the operation .
2022-09-12 22:45:57 +03:00
*
* @since 1.0 . 0
2020-06-27 18:20:00 +03:00
* /
2020-08-19 05:36:46 +03:00
async function renameFile (
oldPath : string ,
newPath : string ,
options : FsOptions = { }
) : Promise < void > {
2021-03-07 05:19:12 +03:00
return invokeTauriCommand ( {
2021-02-16 07:23:15 +03:00
__tauriModule : 'Fs' ,
2021-02-12 08:42:40 +03:00
message : {
cmd : 'renameFile' ,
oldPath ,
newPath ,
options
}
2020-06-27 18:20:00 +03:00
} )
}
2022-09-15 17:28:52 +03:00
/ * *
* Check if a path exists .
* @example
* ` ` ` typescript
* import { exists , BaseDirectory } from '@tauri-apps/api/fs' ;
2022-09-29 01:34:09 +03:00
* // Check if the `$APPDATA/avatar.png` file exists
* await exists ( 'avatar.png' , { dir : BaseDirectory.AppData } ) ;
2022-09-15 17:28:52 +03:00
* ` ` `
*
* @since 1.1 . 0
* /
2022-09-29 21:55:45 +03:00
async function exists ( path : string , options : FsOptions = { } ) : Promise < boolean > {
2022-09-15 17:28:52 +03:00
return invokeTauriCommand ( {
__tauriModule : 'Fs' ,
message : {
cmd : 'exists' ,
path ,
options
}
} )
}
2021-05-17 18:54:56 +03:00
export type {
FsOptions ,
FsDirOptions ,
FsTextFileOption ,
2022-05-29 16:10:41 +03:00
BinaryFileContents ,
2021-05-17 18:54:56 +03:00
FsBinaryFileOption ,
FileEntry
}
2020-06-27 18:20:00 +03:00
export {
BaseDirectory as Dir ,
readTextFile ,
readBinaryFile ,
2022-05-29 16:10:41 +03:00
writeTextFile ,
writeTextFile as writeFile ,
2020-06-27 18:20:00 +03:00
writeBinaryFile ,
readDir ,
createDir ,
removeDir ,
copyFile ,
removeFile ,
2022-09-15 17:28:52 +03:00
renameFile ,
exists
2020-06-27 18:20:00 +03:00
}