path.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. import { invokeTauriCommand } from './helpers/tauri.js';
  2. import { BaseDirectory } from './fs.js';
  3. import { isWindows } from './helpers/os-check.js';
  4. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  5. // SPDX-License-Identifier: Apache-2.0
  6. // SPDX-License-Identifier: MIT
  7. /**
  8. * The path module provides utilities for working with file and directory paths.
  9. *
  10. * This package is also accessible with `window.__TAURI__.path` when [`build.withGlobalTauri`](https://tauri.app/v1/api/config/#buildconfig.withglobaltauri) in `tauri.conf.json` is set to `true`.
  11. *
  12. * The APIs must be added to [`tauri.allowlist.path`](https://tauri.app/v1/api/config/#allowlistconfig.path) in `tauri.conf.json`:
  13. * ```json
  14. * {
  15. * "tauri": {
  16. * "allowlist": {
  17. * "path": {
  18. * "all": true, // enable all Path APIs
  19. * }
  20. * }
  21. * }
  22. * }
  23. * ```
  24. * It is recommended to allowlist only the APIs you use for optimal bundle size and security.
  25. * @module
  26. */
  27. /**
  28. * Returns the path to the suggested directory for your app config files.
  29. *
  30. * @deprecated since 1.2.0: Will be removed in 2.0.0. Use {@link appConfigDir} or {@link appDataDir} instead.
  31. * @since 1.0.0
  32. */
  33. async function appDir() {
  34. return appConfigDir();
  35. }
  36. /**
  37. * Returns the path to the suggested directory for your app's config files.
  38. * Resolves to `${configDir}/${bundleIdentifier}`, where `bundleIdentifier` is the value [`tauri.bundle.identifier`](https://tauri.app/v1/api/config/#bundleconfig.identifier) is configured in `tauri.conf.json`.
  39. * @example
  40. * ```typescript
  41. * import { appConfigDir } from '@tauri-apps/api/path';
  42. * const appConfigDirPath = await appConfigDir();
  43. * ```
  44. *
  45. * @since 1.2.0
  46. */
  47. async function appConfigDir() {
  48. return invokeTauriCommand({
  49. __tauriModule: 'Path',
  50. message: {
  51. cmd: 'resolvePath',
  52. path: '',
  53. directory: BaseDirectory.AppConfig
  54. }
  55. });
  56. }
  57. /**
  58. * Returns the path to the suggested directory for your app's data files.
  59. * Resolves to `${dataDir}/${bundleIdentifier}`, where `bundleIdentifier` is the value [`tauri.bundle.identifier`](https://tauri.app/v1/api/config/#bundleconfig.identifier) is configured in `tauri.conf.json`.
  60. * @example
  61. * ```typescript
  62. * import { appDataDir } from '@tauri-apps/api/path';
  63. * const appDataDirPath = await appDataDir();
  64. * ```
  65. *
  66. * @since 1.2.0
  67. */
  68. async function appDataDir() {
  69. return invokeTauriCommand({
  70. __tauriModule: 'Path',
  71. message: {
  72. cmd: 'resolvePath',
  73. path: '',
  74. directory: BaseDirectory.AppData
  75. }
  76. });
  77. }
  78. /**
  79. * Returns the path to the suggested directory for your app's local data files.
  80. * Resolves to `${localDataDir}/${bundleIdentifier}`, where `bundleIdentifier` is the value [`tauri.bundle.identifier`](https://tauri.app/v1/api/config/#bundleconfig.identifier) is configured in `tauri.conf.json`.
  81. * @example
  82. * ```typescript
  83. * import { appLocalDataDir } from '@tauri-apps/api/path';
  84. * const appLocalDataDirPath = await appLocalDataDir();
  85. * ```
  86. *
  87. * @since 1.2.0
  88. */
  89. async function appLocalDataDir() {
  90. return invokeTauriCommand({
  91. __tauriModule: 'Path',
  92. message: {
  93. cmd: 'resolvePath',
  94. path: '',
  95. directory: BaseDirectory.AppLocalData
  96. }
  97. });
  98. }
  99. /**
  100. * Returns the path to the suggested directory for your app's cache files.
  101. * Resolves to `${cacheDir}/${bundleIdentifier}`, where `bundleIdentifier` is the value [`tauri.bundle.identifier`](https://tauri.app/v1/api/config/#bundleconfig.identifier) is configured in `tauri.conf.json`.
  102. * @example
  103. * ```typescript
  104. * import { appCacheDir } from '@tauri-apps/api/path';
  105. * const appCacheDirPath = await appCacheDir();
  106. * ```
  107. *
  108. * @since 1.2.0
  109. */
  110. async function appCacheDir() {
  111. return invokeTauriCommand({
  112. __tauriModule: 'Path',
  113. message: {
  114. cmd: 'resolvePath',
  115. path: '',
  116. directory: BaseDirectory.AppCache
  117. }
  118. });
  119. }
  120. /**
  121. * Returns the path to the user's audio directory.
  122. *
  123. * #### Platform-specific
  124. *
  125. * - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_MUSIC_DIR`.
  126. * - **macOS:** Resolves to `$HOME/Music`.
  127. * - **Windows:** Resolves to `{FOLDERID_Music}`.
  128. * @example
  129. * ```typescript
  130. * import { audioDir } from '@tauri-apps/api/path';
  131. * const audioDirPath = await audioDir();
  132. * ```
  133. *
  134. * @since 1.0.0
  135. */
  136. async function audioDir() {
  137. return invokeTauriCommand({
  138. __tauriModule: 'Path',
  139. message: {
  140. cmd: 'resolvePath',
  141. path: '',
  142. directory: BaseDirectory.Audio
  143. }
  144. });
  145. }
  146. /**
  147. * Returns the path to the user's cache directory.
  148. *
  149. * #### Platform-specific
  150. *
  151. * - **Linux:** Resolves to `$XDG_CACHE_HOME` or `$HOME/.cache`.
  152. * - **macOS:** Resolves to `$HOME/Library/Caches`.
  153. * - **Windows:** Resolves to `{FOLDERID_LocalAppData}`.
  154. * @example
  155. * ```typescript
  156. * import { cacheDir } from '@tauri-apps/api/path';
  157. * const cacheDirPath = await cacheDir();
  158. * ```
  159. *
  160. * @since 1.0.0
  161. */
  162. async function cacheDir() {
  163. return invokeTauriCommand({
  164. __tauriModule: 'Path',
  165. message: {
  166. cmd: 'resolvePath',
  167. path: '',
  168. directory: BaseDirectory.Cache
  169. }
  170. });
  171. }
  172. /**
  173. * Returns the path to the user's config directory.
  174. *
  175. * #### Platform-specific
  176. *
  177. * - **Linux:** Resolves to `$XDG_CONFIG_HOME` or `$HOME/.config`.
  178. * - **macOS:** Resolves to `$HOME/Library/Application Support`.
  179. * - **Windows:** Resolves to `{FOLDERID_RoamingAppData}`.
  180. * @example
  181. * ```typescript
  182. * import { configDir } from '@tauri-apps/api/path';
  183. * const configDirPath = await configDir();
  184. * ```
  185. *
  186. * @since 1.0.0
  187. */
  188. async function configDir() {
  189. return invokeTauriCommand({
  190. __tauriModule: 'Path',
  191. message: {
  192. cmd: 'resolvePath',
  193. path: '',
  194. directory: BaseDirectory.Config
  195. }
  196. });
  197. }
  198. /**
  199. * Returns the path to the user's data directory.
  200. *
  201. * #### Platform-specific
  202. *
  203. * - **Linux:** Resolves to `$XDG_DATA_HOME` or `$HOME/.local/share`.
  204. * - **macOS:** Resolves to `$HOME/Library/Application Support`.
  205. * - **Windows:** Resolves to `{FOLDERID_RoamingAppData}`.
  206. * @example
  207. * ```typescript
  208. * import { dataDir } from '@tauri-apps/api/path';
  209. * const dataDirPath = await dataDir();
  210. * ```
  211. *
  212. * @since 1.0.0
  213. */
  214. async function dataDir() {
  215. return invokeTauriCommand({
  216. __tauriModule: 'Path',
  217. message: {
  218. cmd: 'resolvePath',
  219. path: '',
  220. directory: BaseDirectory.Data
  221. }
  222. });
  223. }
  224. /**
  225. * Returns the path to the user's desktop directory.
  226. *
  227. * #### Platform-specific
  228. *
  229. * - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_DESKTOP_DIR`.
  230. * - **macOS:** Resolves to `$HOME/Desktop`.
  231. * - **Windows:** Resolves to `{FOLDERID_Desktop}`.
  232. * @example
  233. * ```typescript
  234. * import { desktopDir } from '@tauri-apps/api/path';
  235. * const desktopPath = await desktopDir();
  236. * ```
  237. *
  238. * @since 1.0.0
  239. */
  240. async function desktopDir() {
  241. return invokeTauriCommand({
  242. __tauriModule: 'Path',
  243. message: {
  244. cmd: 'resolvePath',
  245. path: '',
  246. directory: BaseDirectory.Desktop
  247. }
  248. });
  249. }
  250. /**
  251. * Returns the path to the user's document directory.
  252. * @example
  253. * ```typescript
  254. * import { documentDir } from '@tauri-apps/api/path';
  255. * const documentDirPath = await documentDir();
  256. * ```
  257. *
  258. * #### Platform-specific
  259. *
  260. * - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_DOCUMENTS_DIR`.
  261. * - **macOS:** Resolves to `$HOME/Documents`.
  262. * - **Windows:** Resolves to `{FOLDERID_Documents}`.
  263. *
  264. * @since 1.0.0
  265. */
  266. async function documentDir() {
  267. return invokeTauriCommand({
  268. __tauriModule: 'Path',
  269. message: {
  270. cmd: 'resolvePath',
  271. path: '',
  272. directory: BaseDirectory.Document
  273. }
  274. });
  275. }
  276. /**
  277. * Returns the path to the user's download directory.
  278. *
  279. * #### Platform-specific
  280. *
  281. * - **Linux**: Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_DOWNLOAD_DIR`.
  282. * - **macOS**: Resolves to `$HOME/Downloads`.
  283. * - **Windows**: Resolves to `{FOLDERID_Downloads}`.
  284. * @example
  285. * ```typescript
  286. * import { downloadDir } from '@tauri-apps/api/path';
  287. * const downloadDirPath = await downloadDir();
  288. * ```
  289. *
  290. * @since 1.0.0
  291. */
  292. async function downloadDir() {
  293. return invokeTauriCommand({
  294. __tauriModule: 'Path',
  295. message: {
  296. cmd: 'resolvePath',
  297. path: '',
  298. directory: BaseDirectory.Download
  299. }
  300. });
  301. }
  302. /**
  303. * Returns the path to the user's executable directory.
  304. *
  305. * #### Platform-specific
  306. *
  307. * - **Linux:** Resolves to `$XDG_BIN_HOME/../bin` or `$XDG_DATA_HOME/../bin` or `$HOME/.local/bin`.
  308. * - **macOS:** Not supported.
  309. * - **Windows:** Not supported.
  310. * @example
  311. * ```typescript
  312. * import { executableDir } from '@tauri-apps/api/path';
  313. * const executableDirPath = await executableDir();
  314. * ```
  315. *
  316. * @since 1.0.0
  317. */
  318. async function executableDir() {
  319. return invokeTauriCommand({
  320. __tauriModule: 'Path',
  321. message: {
  322. cmd: 'resolvePath',
  323. path: '',
  324. directory: BaseDirectory.Executable
  325. }
  326. });
  327. }
  328. /**
  329. * Returns the path to the user's font directory.
  330. *
  331. * #### Platform-specific
  332. *
  333. * - **Linux:** Resolves to `$XDG_DATA_HOME/fonts` or `$HOME/.local/share/fonts`.
  334. * - **macOS:** Resolves to `$HOME/Library/Fonts`.
  335. * - **Windows:** Not supported.
  336. * @example
  337. * ```typescript
  338. * import { fontDir } from '@tauri-apps/api/path';
  339. * const fontDirPath = await fontDir();
  340. * ```
  341. *
  342. * @since 1.0.0
  343. */
  344. async function fontDir() {
  345. return invokeTauriCommand({
  346. __tauriModule: 'Path',
  347. message: {
  348. cmd: 'resolvePath',
  349. path: '',
  350. directory: BaseDirectory.Font
  351. }
  352. });
  353. }
  354. /**
  355. * Returns the path to the user's home directory.
  356. *
  357. * #### Platform-specific
  358. *
  359. * - **Linux:** Resolves to `$HOME`.
  360. * - **macOS:** Resolves to `$HOME`.
  361. * - **Windows:** Resolves to `{FOLDERID_Profile}`.
  362. * @example
  363. * ```typescript
  364. * import { homeDir } from '@tauri-apps/api/path';
  365. * const homeDirPath = await homeDir();
  366. * ```
  367. *
  368. * @since 1.0.0
  369. */
  370. async function homeDir() {
  371. return invokeTauriCommand({
  372. __tauriModule: 'Path',
  373. message: {
  374. cmd: 'resolvePath',
  375. path: '',
  376. directory: BaseDirectory.Home
  377. }
  378. });
  379. }
  380. /**
  381. * Returns the path to the user's local data directory.
  382. *
  383. * #### Platform-specific
  384. *
  385. * - **Linux:** Resolves to `$XDG_DATA_HOME` or `$HOME/.local/share`.
  386. * - **macOS:** Resolves to `$HOME/Library/Application Support`.
  387. * - **Windows:** Resolves to `{FOLDERID_LocalAppData}`.
  388. * @example
  389. * ```typescript
  390. * import { localDataDir } from '@tauri-apps/api/path';
  391. * const localDataDirPath = await localDataDir();
  392. * ```
  393. *
  394. * @since 1.0.0
  395. */
  396. async function localDataDir() {
  397. return invokeTauriCommand({
  398. __tauriModule: 'Path',
  399. message: {
  400. cmd: 'resolvePath',
  401. path: '',
  402. directory: BaseDirectory.LocalData
  403. }
  404. });
  405. }
  406. /**
  407. * Returns the path to the user's picture directory.
  408. *
  409. * #### Platform-specific
  410. *
  411. * - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_PICTURES_DIR`.
  412. * - **macOS:** Resolves to `$HOME/Pictures`.
  413. * - **Windows:** Resolves to `{FOLDERID_Pictures}`.
  414. * @example
  415. * ```typescript
  416. * import { pictureDir } from '@tauri-apps/api/path';
  417. * const pictureDirPath = await pictureDir();
  418. * ```
  419. *
  420. * @since 1.0.0
  421. */
  422. async function pictureDir() {
  423. return invokeTauriCommand({
  424. __tauriModule: 'Path',
  425. message: {
  426. cmd: 'resolvePath',
  427. path: '',
  428. directory: BaseDirectory.Picture
  429. }
  430. });
  431. }
  432. /**
  433. * Returns the path to the user's public directory.
  434. *
  435. * #### Platform-specific
  436. *
  437. * - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_PUBLICSHARE_DIR`.
  438. * - **macOS:** Resolves to `$HOME/Public`.
  439. * - **Windows:** Resolves to `{FOLDERID_Public}`.
  440. * @example
  441. * ```typescript
  442. * import { publicDir } from '@tauri-apps/api/path';
  443. * const publicDirPath = await publicDir();
  444. * ```
  445. *
  446. * @since 1.0.0
  447. */
  448. async function publicDir() {
  449. return invokeTauriCommand({
  450. __tauriModule: 'Path',
  451. message: {
  452. cmd: 'resolvePath',
  453. path: '',
  454. directory: BaseDirectory.Public
  455. }
  456. });
  457. }
  458. /**
  459. * Returns the path to the application's resource directory.
  460. * To resolve a resource path, see the [[resolveResource | `resolveResource API`]].
  461. * @example
  462. * ```typescript
  463. * import { resourceDir } from '@tauri-apps/api/path';
  464. * const resourceDirPath = await resourceDir();
  465. * ```
  466. *
  467. * @since 1.0.0
  468. */
  469. async function resourceDir() {
  470. return invokeTauriCommand({
  471. __tauriModule: 'Path',
  472. message: {
  473. cmd: 'resolvePath',
  474. path: '',
  475. directory: BaseDirectory.Resource
  476. }
  477. });
  478. }
  479. /**
  480. * Resolve the path to a resource file.
  481. * @example
  482. * ```typescript
  483. * import { resolveResource } from '@tauri-apps/api/path';
  484. * const resourcePath = await resolveResource('script.sh');
  485. * ```
  486. *
  487. * @param resourcePath The path to the resource.
  488. * Must follow the same syntax as defined in `tauri.conf.json > tauri > bundle > resources`, i.e. keeping subfolders and parent dir components (`../`).
  489. * @returns The full path to the resource.
  490. *
  491. * @since 1.0.0
  492. */
  493. async function resolveResource(resourcePath) {
  494. return invokeTauriCommand({
  495. __tauriModule: 'Path',
  496. message: {
  497. cmd: 'resolvePath',
  498. path: resourcePath,
  499. directory: BaseDirectory.Resource
  500. }
  501. });
  502. }
  503. /**
  504. * Returns the path to the user's runtime directory.
  505. *
  506. * #### Platform-specific
  507. *
  508. * - **Linux:** Resolves to `$XDG_RUNTIME_DIR`.
  509. * - **macOS:** Not supported.
  510. * - **Windows:** Not supported.
  511. * @example
  512. * ```typescript
  513. * import { runtimeDir } from '@tauri-apps/api/path';
  514. * const runtimeDirPath = await runtimeDir();
  515. * ```
  516. *
  517. * @since 1.0.0
  518. */
  519. async function runtimeDir() {
  520. return invokeTauriCommand({
  521. __tauriModule: 'Path',
  522. message: {
  523. cmd: 'resolvePath',
  524. path: '',
  525. directory: BaseDirectory.Runtime
  526. }
  527. });
  528. }
  529. /**
  530. * Returns the path to the user's template directory.
  531. *
  532. * #### Platform-specific
  533. *
  534. * - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_TEMPLATES_DIR`.
  535. * - **macOS:** Not supported.
  536. * - **Windows:** Resolves to `{FOLDERID_Templates}`.
  537. * @example
  538. * ```typescript
  539. * import { templateDir } from '@tauri-apps/api/path';
  540. * const templateDirPath = await templateDir();
  541. * ```
  542. *
  543. * @since 1.0.0
  544. */
  545. async function templateDir() {
  546. return invokeTauriCommand({
  547. __tauriModule: 'Path',
  548. message: {
  549. cmd: 'resolvePath',
  550. path: '',
  551. directory: BaseDirectory.Template
  552. }
  553. });
  554. }
  555. /**
  556. * Returns the path to the user's video directory.
  557. *
  558. * #### Platform-specific
  559. *
  560. * - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_VIDEOS_DIR`.
  561. * - **macOS:** Resolves to `$HOME/Movies`.
  562. * - **Windows:** Resolves to `{FOLDERID_Videos}`.
  563. * @example
  564. * ```typescript
  565. * import { videoDir } from '@tauri-apps/api/path';
  566. * const videoDirPath = await videoDir();
  567. * ```
  568. *
  569. * @since 1.0.0
  570. */
  571. async function videoDir() {
  572. return invokeTauriCommand({
  573. __tauriModule: 'Path',
  574. message: {
  575. cmd: 'resolvePath',
  576. path: '',
  577. directory: BaseDirectory.Video
  578. }
  579. });
  580. }
  581. /**
  582. * Returns the path to the suggested log directory.
  583. *
  584. * @deprecated since 1.2.0: Will be removed in 2.0.0. Use {@link appLogDir} instead.
  585. * @since 1.0.0
  586. */
  587. async function logDir() {
  588. return appLogDir();
  589. }
  590. /**
  591. * Returns the path to the suggested directory for your app's log files.
  592. *
  593. * #### Platform-specific
  594. *
  595. * - **Linux:** Resolves to `${configDir}/${bundleIdentifier}/logs`.
  596. * - **macOS:** Resolves to `${homeDir}/Library/Logs/{bundleIdentifier}`
  597. * - **Windows:** Resolves to `${configDir}/${bundleIdentifier}/logs`.
  598. * @example
  599. * ```typescript
  600. * import { appLogDir } from '@tauri-apps/api/path';
  601. * const appLogDirPath = await appLogDir();
  602. * ```
  603. *
  604. * @since 1.2.0
  605. */
  606. async function appLogDir() {
  607. return invokeTauriCommand({
  608. __tauriModule: 'Path',
  609. message: {
  610. cmd: 'resolvePath',
  611. path: '',
  612. directory: BaseDirectory.AppLog
  613. }
  614. });
  615. }
  616. /**
  617. * Provides the platform-specific path segment separator:
  618. * - `\` on Windows
  619. * - `/` on POSIX
  620. *
  621. * @since 1.0.0
  622. */
  623. const sep = isWindows() ? '\\' : '/';
  624. /**
  625. * Provides the platform-specific path segment delimiter:
  626. * - `;` on Windows
  627. * - `:` on POSIX
  628. *
  629. * @since 1.0.0
  630. */
  631. const delimiter = isWindows() ? ';' : ':';
  632. /**
  633. * Resolves a sequence of `paths` or `path` segments into an absolute path.
  634. * @example
  635. * ```typescript
  636. * import { resolve, appDataDir } from '@tauri-apps/api/path';
  637. * const appDataDirPath = await appDataDir();
  638. * const path = await resolve(appDataDirPath, '..', 'users', 'tauri', 'avatar.png');
  639. * ```
  640. *
  641. * @since 1.0.0
  642. */
  643. async function resolve(...paths) {
  644. return invokeTauriCommand({
  645. __tauriModule: 'Path',
  646. message: {
  647. cmd: 'resolve',
  648. paths
  649. }
  650. });
  651. }
  652. /**
  653. * Normalizes the given `path`, resolving `'..'` and `'.'` segments and resolve symbolic links.
  654. * @example
  655. * ```typescript
  656. * import { normalize, appDataDir } from '@tauri-apps/api/path';
  657. * const appDataDirPath = await appDataDir();
  658. * const path = await normalize(appDataDirPath, '..', 'users', 'tauri', 'avatar.png');
  659. * ```
  660. *
  661. * @since 1.0.0
  662. */
  663. async function normalize(path) {
  664. return invokeTauriCommand({
  665. __tauriModule: 'Path',
  666. message: {
  667. cmd: 'normalize',
  668. path
  669. }
  670. });
  671. }
  672. /**
  673. * Joins all given `path` segments together using the platform-specific separator as a delimiter, then normalizes the resulting path.
  674. * @example
  675. * ```typescript
  676. * import { join, appDataDir } from '@tauri-apps/api/path';
  677. * const appDataDirPath = await appDataDir();
  678. * const path = await join(appDataDirPath, 'users', 'tauri', 'avatar.png');
  679. * ```
  680. *
  681. * @since 1.0.0
  682. */
  683. async function join(...paths) {
  684. return invokeTauriCommand({
  685. __tauriModule: 'Path',
  686. message: {
  687. cmd: 'join',
  688. paths
  689. }
  690. });
  691. }
  692. /**
  693. * Returns the directory name of a `path`. Trailing directory separators are ignored.
  694. * @example
  695. * ```typescript
  696. * import { dirname } from '@tauri-apps/api/path';
  697. * const dir = await dirname('/path/to/somedir/');
  698. * assert(dir === 'somedir');
  699. * ```
  700. *
  701. * @since 1.0.0
  702. */
  703. async function dirname(path) {
  704. return invokeTauriCommand({
  705. __tauriModule: 'Path',
  706. message: {
  707. cmd: 'dirname',
  708. path
  709. }
  710. });
  711. }
  712. /**
  713. * Returns the extension of the `path`.
  714. * @example
  715. * ```typescript
  716. * import { extname } from '@tauri-apps/api/path';
  717. * const ext = await extname('/path/to/file.html');
  718. * assert(ext === 'html');
  719. * ```
  720. *
  721. * @since 1.0.0
  722. */
  723. async function extname(path) {
  724. return invokeTauriCommand({
  725. __tauriModule: 'Path',
  726. message: {
  727. cmd: 'extname',
  728. path
  729. }
  730. });
  731. }
  732. /**
  733. * Returns the last portion of a `path`. Trailing directory separators are ignored.
  734. * @example
  735. * ```typescript
  736. * import { basename } from '@tauri-apps/api/path';
  737. * const base = await basename('path/to/app.conf');
  738. * assert(base === 'app.conf');
  739. * ```
  740. *
  741. * @param ext An optional file extension to be removed from the returned path.
  742. *
  743. * @since 1.0.0
  744. */
  745. async function basename(path, ext) {
  746. return invokeTauriCommand({
  747. __tauriModule: 'Path',
  748. message: {
  749. cmd: 'basename',
  750. path,
  751. ext
  752. }
  753. });
  754. }
  755. /**
  756. * Returns whether the path is absolute or not.
  757. * @example
  758. * ```typescript
  759. * import { isAbsolute } from '@tauri-apps/api/path';
  760. * assert(await isAbsolute('/home/tauri'));
  761. * ```
  762. *
  763. * @since 1.0.0
  764. */
  765. async function isAbsolute(path) {
  766. return invokeTauriCommand({
  767. __tauriModule: 'Path',
  768. message: {
  769. cmd: 'isAbsolute',
  770. path
  771. }
  772. });
  773. }
  774. export { BaseDirectory, appCacheDir, appConfigDir, appDataDir, appDir, appLocalDataDir, appLogDir, audioDir, basename, cacheDir, configDir, dataDir, delimiter, desktopDir, dirname, documentDir, downloadDir, executableDir, extname, fontDir, homeDir, isAbsolute, join, localDataDir, logDir, normalize, pictureDir, publicDir, resolve, resolveResource, resourceDir, runtimeDir, sep, templateDir, videoDir };