Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/path-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri.js": minor
"tauri": minor
---

Adds a path resolution API (e.g. getting the download directory or resolving a path to the home directory).
14 changes: 13 additions & 1 deletion cli/tauri.js/api-src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@ import * as cli from './cli'
import * as dialog from './dialog'
import * as event from './event'
import * as fs from './fs'
import * as path from './path'
import http from './http'
import * as process from './process'
import * as tauri from './tauri'
import * as window from './window'
import * as notification from './notification'

export { cli, dialog, event, fs, http, process, tauri, window, notification }
export {
cli,
dialog,
event,
fs,
path,
http,
process,
tauri,
window,
notification
}
274 changes: 274 additions & 0 deletions cli/tauri.js/api-src/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
import { promisified } from './tauri'
import { BaseDirectory } from './fs'

/**
* @name appDir
* @description Returns the path to the suggested directory for your app config files.
* @return {Promise<string>}
*/
async function appDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lucasfernog this will add a "dot" to the end of the path, I believe an empty string is better.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thing, if a base directory doesn't exist, the user will get an error like this Path Error:unable to determine base dir path. In my opinion this will confuse the user as he doesn't know the underlying api is just resolving a path using a base dir, instead it would be better to just return null like tauri's Path api in rust.

directory: BaseDirectory.App
})
}

/**
* @name audioDir
* @description Returns the path to the user's audio directory.
* @return {Promise<string>}
*/
async function audioDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',
directory: BaseDirectory.Audio
})
}

/**
* @name cacheDir
* @description Returns the path to the user's cache directory.
* @return {Promise<string>}
*/
async function cacheDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',
directory: BaseDirectory.Cache
})
}

/**
* @name configDir
* @description Returns the path to the user's config directory.
* @return {Promise<string>}
*/
async function configDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',
directory: BaseDirectory.Config
})
}

/**
* @name dataDir
* @description Returns the path to the user's data directory.
* @return {Promise<string>}
*/
async function dataDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',
directory: BaseDirectory.Data
})
}

/**
* @name desktopDir
* @description Returns the path to the user's desktop directory.
* @return {Promise<string>}
*/
async function desktopDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',
directory: BaseDirectory.Desktop
})
}

/**
* @name documentDir
* @description Returns the path to the user's document directory.
* @return {Promise<string>}
*/
async function documentDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',
directory: BaseDirectory.Document
})
}

/**
* @name downloadDir
* @description Returns the path to the user's download directory.
* @return {Promise<string>}
*/
async function downloadDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',
directory: BaseDirectory.Download
})
}

/**
* @name executableDir
* @description Returns the path to the user's executable directory.
* @return {Promise<string>}
*/
async function executableDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',
directory: BaseDirectory.Executable
})
}

/**
* @name fontDir
* @description Returns the path to the user's font directory.
* @return {Promise<string>}
*/
async function fontDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',
directory: BaseDirectory.Font
})
}

/**
* @name homeDir
* @description Returns the path to the user's home directory.
* @return {Promise<string>}
*/
async function homeDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',
directory: BaseDirectory.Home
})
}

/**
* @name localDataDir
* @description Returns the path to the user's local data directory.
* @return {Promise<string>}
*/
async function localDataDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',
directory: BaseDirectory.LocalData
})
}

/**
* @name pictureDir
* @description Returns the path to the user's picture directory.
* @return {Promise<string>}
*/
async function pictureDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',
directory: BaseDirectory.Picture
})
}

/**
* @name publicDir
* @description Returns the path to the user's public directory.
* @return {Promise<string>}
*/
async function publicDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',
directory: BaseDirectory.Public
})
}

/**
* @name resourceDir
* @description Returns the path to the user's resource directory.
* @return {Promise<string>}
*/
async function resourceDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',
directory: BaseDirectory.Resource
})
}

/**
* @name runtimeDir
* @descriptionReturns Returns the path to the user's runtime directory.
* @return {Promise<string>}
*/
async function runtimeDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',
directory: BaseDirectory.Runtime
})
}

/**
* @name templateDir
* @descriptionReturns Returns the path to the user's template directory.
* @return {Promise<string>}
*/
async function templateDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',
directory: BaseDirectory.Template
})
}

/**
* @name videoDir
* @descriptionReturns Returns the path to the user's video dir.
* @return {Promise<string>}
*/
async function videoDir(): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path: '.',
directory: BaseDirectory.Video
})
}

/**
* @name resolvePath
* @descriptionReturns Resolves the path with the optional base directory.
* @return {Promise<string>}
*/
async function resolvePath(
path: string,
directory: BaseDirectory
): Promise<string> {
return await promisified<string>({
cmd: 'resolvePath',
path,
directory
})
}

export {
appDir,
audioDir,
cacheDir,
configDir,
dataDir,
desktopDir,
documentDir,
downloadDir,
executableDir,
fontDir,
homeDir,
localDataDir,
pictureDir,
publicDir,
resourceDir,
runtimeDir,
templateDir,
videoDir,
resolvePath
}
1 change: 1 addition & 0 deletions cli/tauri.js/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default [
{
input: {
fs: './api-src/fs.ts',
path: './api-src/path.ts',
dialog: './api-src/dialog.ts',
event: './api-src/event.ts',
http: './api-src/http.ts',
Expand Down
1 change: 1 addition & 0 deletions tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ create-dir = [ ]
remove-dir = [ ]
remove-file = [ ]
rename-file = [ ]
path-api = [ ]
set-title = [ ]
execute = [ ]
open = [ ]
Expand Down
4 changes: 4 additions & 0 deletions tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ fn shared() {
setup_env_aliases();
}

#[allow(clippy::cognitive_complexity)]
fn setup_env_aliases() {
cfg_aliases! {
embedded_server: { feature = "embedded-server" },
Expand All @@ -115,6 +116,9 @@ fn setup_env_aliases() {
remove_file: { any(all_api, feature = "remove-file") },
rename_file: { any(all_api, feature = "rename-file") },

// js path api
path_api: { any(all_api, feature = "path-api") },

// window
set_title: { any(all_api, feature = "set-title") },
open: { any(all_api, feature = "open") },
Expand Down
12 changes: 12 additions & 0 deletions tauri/src/endpoints.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod cmd;
#[allow(unused_imports)]
mod file_system;
mod path;
mod salt;

#[cfg(assets)]
Expand Down Expand Up @@ -138,6 +139,17 @@ pub(crate) fn handle(webview: &mut Webview<'_>, arg: &str) -> crate::Result<()>
#[cfg(not(rename_file))]
allowlist_error(webview, error, "renameFile");
}
ResolvePath {
path,
directory,
callback,
error,
} => {
#[cfg(path_api)]
path::resolve_path(webview, path, directory, callback, error);
#[cfg(not(path_api))]
allowlist_error(webview, error, "pathApi");
}
SetTitle { title } => {
#[cfg(set_title)]
webview.set_title(&title);
Expand Down
7 changes: 7 additions & 0 deletions tauri/src/endpoints/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ pub enum Cmd {
callback: String,
error: String,
},
/// The resolve path API
ResolvePath {
path: String,
directory: Option<BaseDirectory>,
callback: String,
error: String,
},
/// The set webview title API.
SetTitle {
title: String,
Expand Down
19 changes: 19 additions & 0 deletions tauri/src/endpoints/path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![cfg(path_api)]
use tauri_api::path;
use tauri_api::path::BaseDirectory;
use webview_official::Webview;

pub fn resolve_path(
webview: &mut Webview<'_>,
path: String,
directory: Option<BaseDirectory>,
callback: String,
error: String,
) {
crate::execute_promise(
webview,
move || path::resolve_path(path, directory),
callback,
error,
)
}