Skip to content

Commit

Permalink
feat: add current working directory to path api module (#1375)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Mar 23, 2021
1 parent 5b3d9b2 commit 52c2baf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/api-path-cwd.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"api": minor
"tauri-api": minor
---
Add current working directory to the path api module.
3 changes: 2 additions & 1 deletion api/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export enum BaseDirectory {
Template,
Video,
Resource,
App
App,
Current
}

export interface FsOptions {
Expand Down
17 changes: 17 additions & 0 deletions api/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,22 @@ async function videoDir(): Promise<string> {
})
}

/**
* @name currentDir
* @descriptionReturns Returns the path to the current working dir.
* @return {Promise<string>}
*/
async function currentDir(): Promise<string> {
return invokeTauriCommand<string>({
__tauriModule: 'Fs',
message: {
cmd: 'resolvePath',
path: '',
directory: BaseDirectory.Current
}
})
}

/**
* @name resolvePath
* @descriptionReturns Resolves the path with the optional base directory.
Expand Down Expand Up @@ -327,5 +343,6 @@ export {
runtimeDir,
templateDir,
videoDir,
currentDir,
resolvePath
}
8 changes: 7 additions & 1 deletion tauri-api/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::path::{Path, PathBuf};
use std::{
env,
path::{Path, PathBuf},
};

use serde_repr::{Deserialize_repr, Serialize_repr};

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -79,6 +84,7 @@ pub fn resolve_path<P: AsRef<Path>>(path: P, dir: Option<BaseDirectory>) -> 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);
Expand Down

0 comments on commit 52c2baf

Please sign in to comment.