You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
621 lines
17 KiB
621 lines
17 KiB
// Copyright 2019-2022 Tauri Programme within The Commons Conservancy
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import { invoke } from '@tauri-apps/api/tauri'
|
|
|
|
function isWindows(): boolean {
|
|
return navigator.appVersion.includes('Win')
|
|
}
|
|
|
|
export enum BaseDirectory {
|
|
Audio = 1,
|
|
Cache,
|
|
Config,
|
|
Data,
|
|
LocalData,
|
|
Document,
|
|
Download,
|
|
Picture,
|
|
Public,
|
|
Video,
|
|
Resource,
|
|
App,
|
|
Log,
|
|
Temp,
|
|
AppConfig,
|
|
AppData,
|
|
AppLocalData,
|
|
AppCache,
|
|
AppLog,
|
|
|
|
Desktop,
|
|
Executable,
|
|
Font,
|
|
Home,
|
|
Runtime,
|
|
Template,
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the suggested directory for your app's config files.
|
|
* 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`.
|
|
* @example
|
|
* ```typescript
|
|
* import { appConfigDir } from '@tauri-apps/api/path';
|
|
* const appConfigDirPath = await appConfigDir();
|
|
* ```
|
|
*/
|
|
async function appConfigDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.AppConfig
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the suggested directory for your app's data files.
|
|
* 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`.
|
|
* @example
|
|
* ```typescript
|
|
* import { appDataDir } from '@tauri-apps/api/path';
|
|
* const appDataDirPath = await appDataDir();
|
|
* ```
|
|
*/
|
|
async function appDataDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.AppData
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the suggested directory for your app's local data files.
|
|
* 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`.
|
|
* @example
|
|
* ```typescript
|
|
* import { appLocalDataDir } from '@tauri-apps/api/path';
|
|
* const appLocalDataDirPath = await appLocalDataDir();
|
|
* ```
|
|
*/
|
|
async function appLocalDataDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.AppLocalData
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the suggested directory for your app's cache files.
|
|
* 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`.
|
|
* @example
|
|
* ```typescript
|
|
* import { appCacheDir } from '@tauri-apps/api/path';
|
|
* const appCacheDirPath = await appCacheDir();
|
|
* ```
|
|
*/
|
|
async function appCacheDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.AppCache
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the user's audio directory.
|
|
*
|
|
* #### Platform-specific
|
|
*
|
|
* - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_MUSIC_DIR`.
|
|
* - **macOS:** Resolves to `$HOME/Music`.
|
|
* - **Windows:** Resolves to `{FOLDERID_Music}`.
|
|
* @example
|
|
* ```typescript
|
|
* import { audioDir } from '@tauri-apps/api/path';
|
|
* const audioDirPath = await audioDir();
|
|
* ```
|
|
*/
|
|
async function audioDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.Audio
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the user's cache directory.
|
|
*
|
|
* #### Platform-specific
|
|
*
|
|
* - **Linux:** Resolves to `$XDG_CACHE_HOME` or `$HOME/.cache`.
|
|
* - **macOS:** Resolves to `$HOME/Library/Caches`.
|
|
* - **Windows:** Resolves to `{FOLDERID_LocalAppData}`.
|
|
* @example
|
|
* ```typescript
|
|
* import { cacheDir } from '@tauri-apps/api/path';
|
|
* const cacheDirPath = await cacheDir();
|
|
* ```
|
|
*/
|
|
async function cacheDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.Cache
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the user's config directory.
|
|
*
|
|
* #### Platform-specific
|
|
*
|
|
* - **Linux:** Resolves to `$XDG_CONFIG_HOME` or `$HOME/.config`.
|
|
* - **macOS:** Resolves to `$HOME/Library/Application Support`.
|
|
* - **Windows:** Resolves to `{FOLDERID_RoamingAppData}`.
|
|
* @example
|
|
* ```typescript
|
|
* import { configDir } from '@tauri-apps/api/path';
|
|
* const configDirPath = await configDir();
|
|
* ```
|
|
*/
|
|
async function configDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.Config
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the user's data directory.
|
|
*
|
|
* #### Platform-specific
|
|
*
|
|
* - **Linux:** Resolves to `$XDG_DATA_HOME` or `$HOME/.local/share`.
|
|
* - **macOS:** Resolves to `$HOME/Library/Application Support`.
|
|
* - **Windows:** Resolves to `{FOLDERID_RoamingAppData}`.
|
|
* @example
|
|
* ```typescript
|
|
* import { dataDir } from '@tauri-apps/api/path';
|
|
* const dataDirPath = await dataDir();
|
|
* ```
|
|
*/
|
|
async function dataDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.Data
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the user's desktop directory.
|
|
*
|
|
* #### Platform-specific
|
|
*
|
|
* - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_DESKTOP_DIR`.
|
|
* - **macOS:** Resolves to `$HOME/Desktop`.
|
|
* - **Windows:** Resolves to `{FOLDERID_Desktop}`.
|
|
* @example
|
|
* ```typescript
|
|
* import { desktopDir } from '@tauri-apps/api/path';
|
|
* const desktopPath = await desktopDir();
|
|
* ```
|
|
*/
|
|
async function desktopDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.Desktop
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the user's document directory.
|
|
* @example
|
|
* ```typescript
|
|
* import { documentDir } from '@tauri-apps/api/path';
|
|
* const documentDirPath = await documentDir();
|
|
* ```
|
|
*
|
|
* #### Platform-specific
|
|
*
|
|
* - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_DOCUMENTS_DIR`.
|
|
* - **macOS:** Resolves to `$HOME/Documents`.
|
|
* - **Windows:** Resolves to `{FOLDERID_Documents}`.
|
|
*/
|
|
async function documentDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.Document
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the user's download directory.
|
|
*
|
|
* #### Platform-specific
|
|
*
|
|
* - **Linux**: Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_DOWNLOAD_DIR`.
|
|
* - **macOS**: Resolves to `$HOME/Downloads`.
|
|
* - **Windows**: Resolves to `{FOLDERID_Downloads}`.
|
|
* @example
|
|
* ```typescript
|
|
* import { downloadDir } from '@tauri-apps/api/path';
|
|
* const downloadDirPath = await downloadDir();
|
|
* ```
|
|
*/
|
|
async function downloadDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.Download
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the user's executable directory.
|
|
*
|
|
* #### Platform-specific
|
|
*
|
|
* - **Linux:** Resolves to `$XDG_BIN_HOME/../bin` or `$XDG_DATA_HOME/../bin` or `$HOME/.local/bin`.
|
|
* - **macOS:** Not supported.
|
|
* - **Windows:** Not supported.
|
|
* @example
|
|
* ```typescript
|
|
* import { executableDir } from '@tauri-apps/api/path';
|
|
* const executableDirPath = await executableDir();
|
|
* ```
|
|
*/
|
|
async function executableDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.Executable
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the user's font directory.
|
|
*
|
|
* #### Platform-specific
|
|
*
|
|
* - **Linux:** Resolves to `$XDG_DATA_HOME/fonts` or `$HOME/.local/share/fonts`.
|
|
* - **macOS:** Resolves to `$HOME/Library/Fonts`.
|
|
* - **Windows:** Not supported.
|
|
* @example
|
|
* ```typescript
|
|
* import { fontDir } from '@tauri-apps/api/path';
|
|
* const fontDirPath = await fontDir();
|
|
* ```
|
|
*/
|
|
async function fontDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.Font
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the user's home directory.
|
|
*
|
|
* #### Platform-specific
|
|
*
|
|
* - **Linux:** Resolves to `$HOME`.
|
|
* - **macOS:** Resolves to `$HOME`.
|
|
* - **Windows:** Resolves to `{FOLDERID_Profile}`.
|
|
* @example
|
|
* ```typescript
|
|
* import { homeDir } from '@tauri-apps/api/path';
|
|
* const homeDirPath = await homeDir();
|
|
* ```
|
|
*/
|
|
async function homeDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.Home
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the user's local data directory.
|
|
*
|
|
* #### Platform-specific
|
|
*
|
|
* - **Linux:** Resolves to `$XDG_DATA_HOME` or `$HOME/.local/share`.
|
|
* - **macOS:** Resolves to `$HOME/Library/Application Support`.
|
|
* - **Windows:** Resolves to `{FOLDERID_LocalAppData}`.
|
|
* @example
|
|
* ```typescript
|
|
* import { localDataDir } from '@tauri-apps/api/path';
|
|
* const localDataDirPath = await localDataDir();
|
|
* ```
|
|
*/
|
|
async function localDataDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.LocalData
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the user's picture directory.
|
|
*
|
|
* #### Platform-specific
|
|
*
|
|
* - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_PICTURES_DIR`.
|
|
* - **macOS:** Resolves to `$HOME/Pictures`.
|
|
* - **Windows:** Resolves to `{FOLDERID_Pictures}`.
|
|
* @example
|
|
* ```typescript
|
|
* import { pictureDir } from '@tauri-apps/api/path';
|
|
* const pictureDirPath = await pictureDir();
|
|
* ```
|
|
*/
|
|
async function pictureDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.Picture
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the user's public directory.
|
|
*
|
|
* #### Platform-specific
|
|
*
|
|
* - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_PUBLICSHARE_DIR`.
|
|
* - **macOS:** Resolves to `$HOME/Public`.
|
|
* - **Windows:** Resolves to `{FOLDERID_Public}`.
|
|
* @example
|
|
* ```typescript
|
|
* import { publicDir } from '@tauri-apps/api/path';
|
|
* const publicDirPath = await publicDir();
|
|
* ```
|
|
*/
|
|
async function publicDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.Public
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the application's resource directory.
|
|
* To resolve a resource path, see the [[resolveResource | `resolveResource API`]].
|
|
* @example
|
|
* ```typescript
|
|
* import { resourceDir } from '@tauri-apps/api/path';
|
|
* const resourceDirPath = await resourceDir();
|
|
* ```
|
|
*/
|
|
async function resourceDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.Resource
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Resolve the path to a resource file.
|
|
* @example
|
|
* ```typescript
|
|
* import { resolveResource } from '@tauri-apps/api/path';
|
|
* const resourcePath = await resolveResource('script.sh');
|
|
* ```
|
|
*
|
|
* @param resourcePath The path to the resource.
|
|
* Must follow the same syntax as defined in `tauri.conf.json > tauri > bundle > resources`, i.e. keeping subfolders and parent dir components (`../`).
|
|
* @returns The full path to the resource.
|
|
*/
|
|
async function resolveResource(resourcePath: string): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.Resource,
|
|
path: resourcePath
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the user's runtime directory.
|
|
*
|
|
* #### Platform-specific
|
|
*
|
|
* - **Linux:** Resolves to `$XDG_RUNTIME_DIR`.
|
|
* - **macOS:** Not supported.
|
|
* - **Windows:** Not supported.
|
|
* @example
|
|
* ```typescript
|
|
* import { runtimeDir } from '@tauri-apps/api/path';
|
|
* const runtimeDirPath = await runtimeDir();
|
|
* ```
|
|
*/
|
|
async function runtimeDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.Runtime
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the user's template directory.
|
|
*
|
|
* #### Platform-specific
|
|
*
|
|
* - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_TEMPLATES_DIR`.
|
|
* - **macOS:** Not supported.
|
|
* - **Windows:** Resolves to `{FOLDERID_Templates}`.
|
|
* @example
|
|
* ```typescript
|
|
* import { templateDir } from '@tauri-apps/api/path';
|
|
* const templateDirPath = await templateDir();
|
|
* ```
|
|
*/
|
|
async function templateDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.Template
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the user's video directory.
|
|
*
|
|
* #### Platform-specific
|
|
*
|
|
* - **Linux:** Resolves to [`xdg-user-dirs`](https://www.freedesktop.org/wiki/Software/xdg-user-dirs/)' `XDG_VIDEOS_DIR`.
|
|
* - **macOS:** Resolves to `$HOME/Movies`.
|
|
* - **Windows:** Resolves to `{FOLDERID_Videos}`.
|
|
* @example
|
|
* ```typescript
|
|
* import { videoDir } from '@tauri-apps/api/path';
|
|
* const videoDirPath = await videoDir();
|
|
* ```
|
|
*/
|
|
async function videoDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.Video
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Returns the path to the suggested directory for your app's log files.
|
|
*
|
|
* #### Platform-specific
|
|
*
|
|
* - **Linux:** Resolves to `${configDir}/${bundleIdentifier}/logs`.
|
|
* - **macOS:** Resolves to `${homeDir}/Library/Logs/{bundleIdentifier}`
|
|
* - **Windows:** Resolves to `${configDir}/${bundleIdentifier}/logs`.
|
|
* @example
|
|
* ```typescript
|
|
* import { appLogDir } from '@tauri-apps/api/path';
|
|
* const appLogDirPath = await appLogDir();
|
|
* ```
|
|
*/
|
|
async function appLogDir(): Promise<string> {
|
|
return invoke('plugin:path|resolve_directory', {
|
|
directory: BaseDirectory.AppLog
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Provides the platform-specific path segment separator:
|
|
* - `\` on Windows
|
|
* - `/` on POSIX
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
const sep = isWindows() ? '\\' : '/'
|
|
|
|
/**
|
|
* Provides the platform-specific path segment delimiter:
|
|
* - `;` on Windows
|
|
* - `:` on POSIX
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
const delimiter = isWindows() ? ';' : ':'
|
|
|
|
/**
|
|
* Resolves a sequence of `paths` or `path` segments into an absolute path.
|
|
* @example
|
|
* ```typescript
|
|
* import { resolve, appDataDir } from '@tauri-apps/api/path';
|
|
* const appDataDirPath = await appDataDir();
|
|
* const path = await resolve(appDataDirPath, '..', 'users', 'tauri', 'avatar.png');
|
|
* ```
|
|
*/
|
|
async function resolve(...paths: string[]): Promise<string> {
|
|
return invoke('plugin:path|resolve', { paths })
|
|
}
|
|
|
|
/**
|
|
* Normalizes the given `path`, resolving `'..'` and `'.'` segments and resolve symbolic links.
|
|
* @example
|
|
* ```typescript
|
|
* import { normalize, appDataDir } from '@tauri-apps/api/path';
|
|
* const appDataDirPath = await appDataDir();
|
|
* const path = await normalize(appDataDirPath, '..', 'users', 'tauri', 'avatar.png');
|
|
* ```
|
|
*/
|
|
async function normalize(path: string): Promise<string> {
|
|
return invoke('plugin:path|normalize', { path })
|
|
}
|
|
|
|
/**
|
|
* Joins all given `path` segments together using the platform-specific separator as a delimiter, then normalizes the resulting path.
|
|
* @example
|
|
* ```typescript
|
|
* import { join, appDataDir } from '@tauri-apps/api/path';
|
|
* const appDataDirPath = await appDataDir();
|
|
* const path = await join(appDataDirPath, 'users', 'tauri', 'avatar.png');
|
|
* ```
|
|
*/
|
|
async function join(...paths: string[]): Promise<string> {
|
|
return invoke('plugin:path|join', { paths })
|
|
}
|
|
|
|
/**
|
|
* Returns the directory name of a `path`. Trailing directory separators are ignored.
|
|
* @example
|
|
* ```typescript
|
|
* import { dirname, appDataDir } from '@tauri-apps/api/path';
|
|
* const appDataDirPath = await appDataDir();
|
|
* const dir = await dirname(appDataDirPath);
|
|
* ```
|
|
*/
|
|
async function dirname(path: string): Promise<string> {
|
|
return invoke('plugin:path|dirname', { path })
|
|
}
|
|
|
|
/**
|
|
* Returns the extension of the `path`.
|
|
* @example
|
|
* ```typescript
|
|
* import { extname, resolveResource } from '@tauri-apps/api/path';
|
|
* const resourcePath = await resolveResource('app.conf');
|
|
* const ext = await extname(resourcePath);
|
|
* assert(ext === 'conf');
|
|
* ```
|
|
*/
|
|
async function extname(path: string): Promise<string> {
|
|
return invoke('plugin:path|extname', { path })
|
|
}
|
|
|
|
/**
|
|
* Returns the last portion of a `path`. Trailing directory separators are ignored.
|
|
* @example
|
|
* ```typescript
|
|
* import { basename, resolveResource } from '@tauri-apps/api/path';
|
|
* const resourcePath = await resolveResource('app.conf');
|
|
* const base = await basename(resourcePath);
|
|
* assert(base === 'app');
|
|
* ```
|
|
*
|
|
* @param ext An optional file extension to be removed from the returned path.
|
|
*/
|
|
async function basename(path: string, ext?: string): Promise<string> {
|
|
return invoke('plugin:path|basename', { path, ext })
|
|
}
|
|
|
|
/**
|
|
* Returns whether the path is absolute or not.
|
|
* @example
|
|
* ```typescript
|
|
* import { isAbsolute } from '@tauri-apps/api/path';
|
|
* assert(await isAbsolute('/home/tauri'));
|
|
* ```
|
|
*/
|
|
async function isAbsolute(path: string): Promise<boolean> {
|
|
return invoke('plugin:path|isAbsolute', { path })
|
|
}
|
|
|
|
export {
|
|
appDir,
|
|
appConfigDir,
|
|
appDataDir,
|
|
appLocalDataDir,
|
|
appCacheDir,
|
|
appLogDir,
|
|
audioDir,
|
|
cacheDir,
|
|
configDir,
|
|
dataDir,
|
|
desktopDir,
|
|
documentDir,
|
|
downloadDir,
|
|
executableDir,
|
|
fontDir,
|
|
homeDir,
|
|
localDataDir,
|
|
pictureDir,
|
|
publicDir,
|
|
resourceDir,
|
|
resolveResource,
|
|
runtimeDir,
|
|
templateDir,
|
|
videoDir,
|
|
sep,
|
|
delimiter,
|
|
resolve,
|
|
normalize,
|
|
join,
|
|
dirname,
|
|
extname,
|
|
basename,
|
|
isAbsolute
|
|
}
|