Skip to content

Commit 771e401

Browse files
feat: Port path api to js (#1006)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent e760331 commit 771e401

9 files changed

Lines changed: 337 additions & 1 deletion

File tree

.changes/path-api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri.js": minor
3+
"tauri": minor
4+
---
5+
6+
Adds a path resolution API (e.g. getting the download directory or resolving a path to the home directory).

cli/tauri.js/api-src/bundle.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@ import * as cli from './cli'
33
import * as dialog from './dialog'
44
import * as event from './event'
55
import * as fs from './fs'
6+
import * as path from './path'
67
import http from './http'
78
import * as process from './process'
89
import * as tauri from './tauri'
910
import * as window from './window'
1011
import * as notification from './notification'
1112

12-
export { cli, dialog, event, fs, http, process, tauri, window, notification }
13+
export {
14+
cli,
15+
dialog,
16+
event,
17+
fs,
18+
path,
19+
http,
20+
process,
21+
tauri,
22+
window,
23+
notification
24+
}

cli/tauri.js/api-src/path.ts

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
import { promisified } from './tauri'
2+
import { BaseDirectory } from './fs'
3+
4+
/**
5+
* @name appDir
6+
* @description Returns the path to the suggested directory for your app config files.
7+
* @return {Promise<string>}
8+
*/
9+
async function appDir(): Promise<string> {
10+
return await promisified<string>({
11+
cmd: 'resolvePath',
12+
path: '.',
13+
directory: BaseDirectory.App
14+
})
15+
}
16+
17+
/**
18+
* @name audioDir
19+
* @description Returns the path to the user's audio directory.
20+
* @return {Promise<string>}
21+
*/
22+
async function audioDir(): Promise<string> {
23+
return await promisified<string>({
24+
cmd: 'resolvePath',
25+
path: '.',
26+
directory: BaseDirectory.Audio
27+
})
28+
}
29+
30+
/**
31+
* @name cacheDir
32+
* @description Returns the path to the user's cache directory.
33+
* @return {Promise<string>}
34+
*/
35+
async function cacheDir(): Promise<string> {
36+
return await promisified<string>({
37+
cmd: 'resolvePath',
38+
path: '.',
39+
directory: BaseDirectory.Cache
40+
})
41+
}
42+
43+
/**
44+
* @name configDir
45+
* @description Returns the path to the user's config directory.
46+
* @return {Promise<string>}
47+
*/
48+
async function configDir(): Promise<string> {
49+
return await promisified<string>({
50+
cmd: 'resolvePath',
51+
path: '.',
52+
directory: BaseDirectory.Config
53+
})
54+
}
55+
56+
/**
57+
* @name dataDir
58+
* @description Returns the path to the user's data directory.
59+
* @return {Promise<string>}
60+
*/
61+
async function dataDir(): Promise<string> {
62+
return await promisified<string>({
63+
cmd: 'resolvePath',
64+
path: '.',
65+
directory: BaseDirectory.Data
66+
})
67+
}
68+
69+
/**
70+
* @name desktopDir
71+
* @description Returns the path to the user's desktop directory.
72+
* @return {Promise<string>}
73+
*/
74+
async function desktopDir(): Promise<string> {
75+
return await promisified<string>({
76+
cmd: 'resolvePath',
77+
path: '.',
78+
directory: BaseDirectory.Desktop
79+
})
80+
}
81+
82+
/**
83+
* @name documentDir
84+
* @description Returns the path to the user's document directory.
85+
* @return {Promise<string>}
86+
*/
87+
async function documentDir(): Promise<string> {
88+
return await promisified<string>({
89+
cmd: 'resolvePath',
90+
path: '.',
91+
directory: BaseDirectory.Document
92+
})
93+
}
94+
95+
/**
96+
* @name downloadDir
97+
* @description Returns the path to the user's download directory.
98+
* @return {Promise<string>}
99+
*/
100+
async function downloadDir(): Promise<string> {
101+
return await promisified<string>({
102+
cmd: 'resolvePath',
103+
path: '.',
104+
directory: BaseDirectory.Download
105+
})
106+
}
107+
108+
/**
109+
* @name executableDir
110+
* @description Returns the path to the user's executable directory.
111+
* @return {Promise<string>}
112+
*/
113+
async function executableDir(): Promise<string> {
114+
return await promisified<string>({
115+
cmd: 'resolvePath',
116+
path: '.',
117+
directory: BaseDirectory.Executable
118+
})
119+
}
120+
121+
/**
122+
* @name fontDir
123+
* @description Returns the path to the user's font directory.
124+
* @return {Promise<string>}
125+
*/
126+
async function fontDir(): Promise<string> {
127+
return await promisified<string>({
128+
cmd: 'resolvePath',
129+
path: '.',
130+
directory: BaseDirectory.Font
131+
})
132+
}
133+
134+
/**
135+
* @name homeDir
136+
* @description Returns the path to the user's home directory.
137+
* @return {Promise<string>}
138+
*/
139+
async function homeDir(): Promise<string> {
140+
return await promisified<string>({
141+
cmd: 'resolvePath',
142+
path: '.',
143+
directory: BaseDirectory.Home
144+
})
145+
}
146+
147+
/**
148+
* @name localDataDir
149+
* @description Returns the path to the user's local data directory.
150+
* @return {Promise<string>}
151+
*/
152+
async function localDataDir(): Promise<string> {
153+
return await promisified<string>({
154+
cmd: 'resolvePath',
155+
path: '.',
156+
directory: BaseDirectory.LocalData
157+
})
158+
}
159+
160+
/**
161+
* @name pictureDir
162+
* @description Returns the path to the user's picture directory.
163+
* @return {Promise<string>}
164+
*/
165+
async function pictureDir(): Promise<string> {
166+
return await promisified<string>({
167+
cmd: 'resolvePath',
168+
path: '.',
169+
directory: BaseDirectory.Picture
170+
})
171+
}
172+
173+
/**
174+
* @name publicDir
175+
* @description Returns the path to the user's public directory.
176+
* @return {Promise<string>}
177+
*/
178+
async function publicDir(): Promise<string> {
179+
return await promisified<string>({
180+
cmd: 'resolvePath',
181+
path: '.',
182+
directory: BaseDirectory.Public
183+
})
184+
}
185+
186+
/**
187+
* @name resourceDir
188+
* @description Returns the path to the user's resource directory.
189+
* @return {Promise<string>}
190+
*/
191+
async function resourceDir(): Promise<string> {
192+
return await promisified<string>({
193+
cmd: 'resolvePath',
194+
path: '.',
195+
directory: BaseDirectory.Resource
196+
})
197+
}
198+
199+
/**
200+
* @name runtimeDir
201+
* @descriptionReturns Returns the path to the user's runtime directory.
202+
* @return {Promise<string>}
203+
*/
204+
async function runtimeDir(): Promise<string> {
205+
return await promisified<string>({
206+
cmd: 'resolvePath',
207+
path: '.',
208+
directory: BaseDirectory.Runtime
209+
})
210+
}
211+
212+
/**
213+
* @name templateDir
214+
* @descriptionReturns Returns the path to the user's template directory.
215+
* @return {Promise<string>}
216+
*/
217+
async function templateDir(): Promise<string> {
218+
return await promisified<string>({
219+
cmd: 'resolvePath',
220+
path: '.',
221+
directory: BaseDirectory.Template
222+
})
223+
}
224+
225+
/**
226+
* @name videoDir
227+
* @descriptionReturns Returns the path to the user's video dir.
228+
* @return {Promise<string>}
229+
*/
230+
async function videoDir(): Promise<string> {
231+
return await promisified<string>({
232+
cmd: 'resolvePath',
233+
path: '.',
234+
directory: BaseDirectory.Video
235+
})
236+
}
237+
238+
/**
239+
* @name resolvePath
240+
* @descriptionReturns Resolves the path with the optional base directory.
241+
* @return {Promise<string>}
242+
*/
243+
async function resolvePath(
244+
path: string,
245+
directory: BaseDirectory
246+
): Promise<string> {
247+
return await promisified<string>({
248+
cmd: 'resolvePath',
249+
path,
250+
directory
251+
})
252+
}
253+
254+
export {
255+
appDir,
256+
audioDir,
257+
cacheDir,
258+
configDir,
259+
dataDir,
260+
desktopDir,
261+
documentDir,
262+
downloadDir,
263+
executableDir,
264+
fontDir,
265+
homeDir,
266+
localDataDir,
267+
pictureDir,
268+
publicDir,
269+
resourceDir,
270+
runtimeDir,
271+
templateDir,
272+
videoDir,
273+
resolvePath
274+
}

cli/tauri.js/rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default [
1111
{
1212
input: {
1313
fs: './api-src/fs.ts',
14+
path: './api-src/path.ts',
1415
dialog: './api-src/dialog.ts',
1516
event: './api-src/event.ts',
1617
http: './api-src/http.ts',

tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ create-dir = [ ]
6363
remove-dir = [ ]
6464
remove-file = [ ]
6565
rename-file = [ ]
66+
path-api = [ ]
6667
set-title = [ ]
6768
execute = [ ]
6869
open = [ ]

tauri/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ fn shared() {
9494
setup_env_aliases();
9595
}
9696

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

119+
// js path api
120+
path_api: { any(all_api, feature = "path-api") },
121+
118122
// window
119123
set_title: { any(all_api, feature = "set-title") },
120124
open: { any(all_api, feature = "open") },

tauri/src/endpoints.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod cmd;
22
#[allow(unused_imports)]
33
mod file_system;
4+
mod path;
45
mod salt;
56

67
#[cfg(assets)]
@@ -138,6 +139,17 @@ pub(crate) fn handle(webview: &mut Webview<'_>, arg: &str) -> crate::Result<()>
138139
#[cfg(not(rename_file))]
139140
allowlist_error(webview, error, "renameFile");
140141
}
142+
ResolvePath {
143+
path,
144+
directory,
145+
callback,
146+
error,
147+
} => {
148+
#[cfg(path_api)]
149+
path::resolve_path(webview, path, directory, callback, error);
150+
#[cfg(not(path_api))]
151+
allowlist_error(webview, error, "pathApi");
152+
}
141153
SetTitle { title } => {
142154
#[cfg(set_title)]
143155
webview.set_title(&title);

tauri/src/endpoints/cmd.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ pub enum Cmd {
139139
callback: String,
140140
error: String,
141141
},
142+
/// The resolve path API
143+
ResolvePath {
144+
path: String,
145+
directory: Option<BaseDirectory>,
146+
callback: String,
147+
error: String,
148+
},
142149
/// The set webview title API.
143150
SetTitle {
144151
title: String,

tauri/src/endpoints/path.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![cfg(path_api)]
2+
use tauri_api::path;
3+
use tauri_api::path::BaseDirectory;
4+
use webview_official::Webview;
5+
6+
pub fn resolve_path(
7+
webview: &mut Webview<'_>,
8+
path: String,
9+
directory: Option<BaseDirectory>,
10+
callback: String,
11+
error: String,
12+
) {
13+
crate::execute_promise(
14+
webview,
15+
move || path::resolve_path(path, directory),
16+
callback,
17+
error,
18+
)
19+
}

0 commit comments

Comments
 (0)