diff --git a/.changes/api-path-cwd.md b/.changes/api-path-cwd.md new file mode 100644 index 00000000000..7bf8dde2902 --- /dev/null +++ b/.changes/api-path-cwd.md @@ -0,0 +1,5 @@ +--- +"api": minor +"tauri-api": minor +--- +Add current working directory to the path api module. diff --git a/api/src/fs.ts b/api/src/fs.ts index 253dc210b4b..569fc38fe84 100644 --- a/api/src/fs.ts +++ b/api/src/fs.ts @@ -18,7 +18,8 @@ export enum BaseDirectory { Template, Video, Resource, - App + App, + Current } export interface FsOptions { diff --git a/api/src/path.ts b/api/src/path.ts index 48966cc1106..78d1f3de894 100644 --- a/api/src/path.ts +++ b/api/src/path.ts @@ -289,6 +289,22 @@ async function videoDir(): Promise { }) } +/** + * @name currentDir + * @descriptionReturns Returns the path to the current working dir. + * @return {Promise} + */ +async function currentDir(): Promise { + return invokeTauriCommand({ + __tauriModule: 'Fs', + message: { + cmd: 'resolvePath', + path: '', + directory: BaseDirectory.Current + } + }) +} + /** * @name resolvePath * @descriptionReturns Resolves the path with the optional base directory. @@ -327,5 +343,6 @@ export { runtimeDir, templateDir, videoDir, + currentDir, resolvePath } diff --git a/tauri-api/src/path.rs b/tauri-api/src/path.rs index 73471b56989..9d0fa1005e1 100644 --- a/tauri-api/src/path.rs +++ b/tauri-api/src/path.rs @@ -1,4 +1,7 @@ -use std::path::{Path, PathBuf}; +use std::{ + env, + path::{Path, PathBuf}, +}; use serde_repr::{Deserialize_repr, Serialize_repr}; @@ -47,6 +50,8 @@ pub enum BaseDirectory { /// The default App config directory. /// Resolves to ${CONFIG_DIR}/${APP_NAME} App, + /// The current working directory. + Current, } /// Resolves the path with the optional base directory. @@ -79,6 +84,7 @@ pub fn resolve_path>(path: P, dir: Option) -> crat BaseDirectory::Video => video_dir(), BaseDirectory::Resource => resource_dir(), BaseDirectory::App => app_dir(), + BaseDirectory::Current => Some(env::current_dir()?), }; if let Some(mut base_dir_path_value) = base_dir_path { base_dir_path_value.push(path);