Skip to content

Commit

Permalink
feat(api): Add exists function to the fs module. (#5060)
Browse files Browse the repository at this point in the history
Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.studio>
  • Loading branch information
3 people authored Sep 15, 2022
1 parent 255ebcb commit 3c62dbc
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 40 deletions.
6 changes: 6 additions & 0 deletions .changes/feat-exists-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"api": minor
"tauri": minor
---

Add `exists` function to the fs module.
5 changes: 5 additions & 0 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,9 @@ pub struct FsAllowlistConfig {
/// Rename file from local filesystem.
#[serde(default, alias = "rename-file")]
pub rename_file: bool,
/// Check if path exists on the local filesystem.
#[serde(default)]
pub exists: bool,
}

impl Allowlist for FsAllowlistConfig {
Expand All @@ -1219,6 +1222,7 @@ impl Allowlist for FsAllowlistConfig {
remove_dir: true,
remove_file: true,
rename_file: true,
exists: true,
};
let mut features = allowlist.to_features();
features.push("fs-all");
Expand All @@ -1238,6 +1242,7 @@ impl Allowlist for FsAllowlistConfig {
check_feature!(self, features, remove_dir, "fs-remove-dir");
check_feature!(self, features, remove_file, "fs-remove-file");
check_feature!(self, features, rename_file, "fs-rename-file");
check_feature!(self, features, exists, "fs-exists");
features
}
}
Expand Down
2 changes: 2 additions & 0 deletions core/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ dialog-save = [ "dialog" ]
fs-all = [
"fs-copy-file",
"fs-create-dir",
"fs-exists",
"fs-read-file",
"fs-read-dir",
"fs-remove-dir",
Expand All @@ -204,6 +205,7 @@ fs-all = [
]
fs-copy-file = [ ]
fs-create-dir = [ ]
fs-exists = [ ]
fs-read-file = [ ]
fs-read-dir = [ ]
fs-remove-dir = [ ]
Expand Down
1 change: 1 addition & 0 deletions core/tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ fn main() {
"remove-dir",
"remove-file",
"rename-file",
"exists",
],
api_all,
);
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.js

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions core/tauri/src/endpoints/file_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ pub(crate) enum Cmd {
new_path: SafePathBuf,
options: Option<FileOperationOptions>,
},
/// The exists API.
#[cmd(fs_exists, "fs > exists")]
Exists {
path: SafePathBuf,
options: Option<FileOperationOptions>,
},
}

impl Cmd {
Expand Down Expand Up @@ -339,6 +345,22 @@ impl Cmd {
.with_context(|| format!("old: {}, new: {}", old.display(), new.display()))
.map_err(Into::into)
}

#[module_command_handler(fs_exists)]
fn exists<R: Runtime>(
context: InvokeContext<R>,
path: SafePathBuf,
options: Option<FileOperationOptions>,
) -> super::Result<bool> {
let resolved_path = resolve_path(
&context.config,
&context.package_info,
&context.window,
path,
options.and_then(|o| o.dir),
)?;
Ok(resolved_path.as_ref().exists())
}
}

#[allow(dead_code)]
Expand Down Expand Up @@ -474,4 +496,11 @@ mod tests {
);
crate::test_utils::assert_not_allowlist_error(res);
}

#[tauri_macros::module_command_test(fs_exists, "fs > exists")]
#[quickcheck_macros::quickcheck]
fn exists(path: SafePathBuf, options: Option<FileOperationOptions>) {
let res = super::Cmd::exists(crate::test::mock_invoke_context(), path, options);
crate::test_utils::assert_not_allowlist_error(res);
}
}
1 change: 1 addition & 0 deletions core/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
//! - **fs-all**: Enables all [Filesystem APIs](https://tauri.app/en/docs/api/js/modules/fs).
//! - **fs-copy-file**: Enables the [`copyFile` API](https://tauri.app/en/docs/api/js/modules/fs#copyfile).
//! - **fs-create-dir**: Enables the [`createDir` API](https://tauri.app/en/docs/api/js/modules/fs#createdir).
//! - **fs-exists**: Enables the [`exists` API](https://tauri.app/en/docs/api/js/modules/fs#exists).
//! - **fs-read-dir**: Enables the [`readDir` API](https://tauri.app/en/docs/api/js/modules/fs#readdir).
//! - **fs-read-file**: Enables the [`readTextFile` API](https://tauri.app/en/docs/api/js/modules/fs#readtextfile) and the [`readBinaryFile` API](https://tauri.app/en/docs/api/js/modules/fs#readbinaryfile).
//! - **fs-remove-dir**: Enables the [`removeDir` API](https://tauri.app/en/docs/api/js/modules/fs#removedir).
Expand Down
74 changes: 37 additions & 37 deletions examples/api/dist/assets/index.js

Large diffs are not rendered by default.

28 changes: 26 additions & 2 deletions tooling/api/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
* "createDir": true,
* "removeDir": true,
* "removeFile": true,
* "renameFile": true
* "renameFile": true,
* "exists": true
* }
* }
* }
Expand Down Expand Up @@ -550,6 +551,28 @@ async function renameFile(
})
}

/**
* Check if a path exists.
* @example
* ```typescript
* import { exists, BaseDirectory } from '@tauri-apps/api/fs';
* // Check if the `$APPDIR/avatar.png` file exists
* await exists('avatar.png', { dir: BaseDirectory.App });
* ```
*
* @since 1.1.0
*/
async function exists(path: string, options: FsOptions = {}): Promise<void> {
return invokeTauriCommand({
__tauriModule: 'Fs',
message: {
cmd: 'exists',
path,
options
}
})
}

export type {
FsOptions,
FsDirOptions,
Expand All @@ -571,5 +594,6 @@ export {
removeDir,
copyFile,
removeFile,
renameFile
renameFile,
exists
}
8 changes: 8 additions & 0 deletions tooling/cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"all": false,
"copyFile": false,
"createDir": false,
"exists": false,
"readDir": false,
"readFile": false,
"removeDir": false,
Expand Down Expand Up @@ -306,6 +307,7 @@
"all": false,
"copyFile": false,
"createDir": false,
"exists": false,
"readDir": false,
"readFile": false,
"removeDir": false,
Expand Down Expand Up @@ -1498,6 +1500,7 @@
"all": false,
"copyFile": false,
"createDir": false,
"exists": false,
"readDir": false,
"readFile": false,
"removeDir": false,
Expand Down Expand Up @@ -1735,6 +1738,11 @@
"description": "Rename file from local filesystem.",
"default": false,
"type": "boolean"
},
"exists": {
"description": "Check if path exists on the local filesystem.",
"default": false,
"type": "boolean"
}
},
"additionalProperties": false
Expand Down

0 comments on commit 3c62dbc

Please sign in to comment.