feat: add current working directory to path api module (#1375)

This commit is contained in:
Amr Bashir 2021-03-23 03:13:12 +02:00 committed by GitHub
parent 5b3d9b2c07
commit 52c2baf940
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 2 deletions

5
.changes/api-path-cwd.md Normal file
View File

@ -0,0 +1,5 @@
---
"api": minor
"tauri-api": minor
---
Add current working directory to the path api module.

View File

@ -18,7 +18,8 @@ export enum BaseDirectory {
Template,
Video,
Resource,
App
App,
Current
}
export interface FsOptions {

View File

@ -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.
@ -327,5 +343,6 @@ export {
runtimeDir,
templateDir,
videoDir,
currentDir,
resolvePath
}

View File

@ -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<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);