Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): Add exists function to the fs module. #5060

Merged
merged 6 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -197,6 +197,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 @@ -206,6 +207,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 @@ -333,6 +339,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 @@ -468,4 +490,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
30 changes: 28 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 @@ -542,6 +543,30 @@ async function renameFile(
})
}

/**
* Check if a path exists.
lucasfernog marked this conversation as resolved.
Show resolved Hide resolved
* @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 });
* ```
*
* @param path A path of the file or directory to check.
* @param options Configuration object.
* @returns A promise indicating the success or failure of the operation.
*/
async function exists(path: string, options: FsOptions = {}): Promise<void> {
return invokeTauriCommand({
__tauriModule: 'Fs',
message: {
cmd: 'exists',
path,
options
}
})
}

export type {
FsOptions,
FsDirOptions,
Expand All @@ -563,5 +588,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