fs.d.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. * @since 1.0.0
  3. */
  4. export declare enum BaseDirectory {
  5. Audio = 1,
  6. Cache = 2,
  7. Config = 3,
  8. Data = 4,
  9. LocalData = 5,
  10. Desktop = 6,
  11. Document = 7,
  12. Download = 8,
  13. Executable = 9,
  14. Font = 10,
  15. Home = 11,
  16. Picture = 12,
  17. Public = 13,
  18. Runtime = 14,
  19. Template = 15,
  20. Video = 16,
  21. Resource = 17,
  22. App = 18,
  23. Log = 19,
  24. Temp = 20,
  25. AppConfig = 21,
  26. AppData = 22,
  27. AppLocalData = 23,
  28. AppCache = 24,
  29. AppLog = 25
  30. }
  31. /**
  32. * @since 1.0.0
  33. */
  34. interface FsOptions {
  35. dir?: BaseDirectory;
  36. /**
  37. * Whether the content should overwrite the content of the file or append to it.
  38. *
  39. * @since 1.5.0
  40. */
  41. append?: boolean;
  42. }
  43. /**
  44. * @since 1.0.0
  45. */
  46. interface FsDirOptions {
  47. dir?: BaseDirectory;
  48. recursive?: boolean;
  49. }
  50. /**
  51. * Options object used to write a UTF-8 string to a file.
  52. *
  53. * @since 1.0.0
  54. */
  55. interface FsTextFileOption {
  56. /** Path to the file to write. */
  57. path: string;
  58. /** The UTF-8 string to write to the file. */
  59. contents: string;
  60. }
  61. type BinaryFileContents = Iterable<number> | ArrayLike<number> | ArrayBuffer;
  62. /**
  63. * Options object used to write a binary data to a file.
  64. *
  65. * @since 1.0.0
  66. */
  67. interface FsBinaryFileOption {
  68. /** Path to the file to write. */
  69. path: string;
  70. /** The byte array contents. */
  71. contents: BinaryFileContents;
  72. }
  73. /**
  74. * @since 1.0.0
  75. */
  76. interface FileEntry {
  77. path: string;
  78. /**
  79. * Name of the directory/file
  80. * can be null if the path terminates with `..`
  81. */
  82. name?: string;
  83. /** Children of this entry if it's a directory; null otherwise */
  84. children?: FileEntry[];
  85. }
  86. /**
  87. * Reads a file as an UTF-8 encoded string.
  88. * @example
  89. * ```typescript
  90. * import { readTextFile, BaseDirectory } from '@tauri-apps/api/fs';
  91. * // Read the text file in the `$APPCONFIG/app.conf` path
  92. * const contents = await readTextFile('app.conf', { dir: BaseDirectory.AppConfig });
  93. * ```
  94. *
  95. * @since 1.0.0
  96. */
  97. declare function readTextFile(filePath: string, options?: FsOptions): Promise<string>;
  98. /**
  99. * Reads a file as byte array.
  100. * @example
  101. * ```typescript
  102. * import { readBinaryFile, BaseDirectory } from '@tauri-apps/api/fs';
  103. * // Read the image file in the `$RESOURCEDIR/avatar.png` path
  104. * const contents = await readBinaryFile('avatar.png', { dir: BaseDirectory.Resource });
  105. * ```
  106. *
  107. * @since 1.0.0
  108. */
  109. declare function readBinaryFile(filePath: string, options?: FsOptions): Promise<Uint8Array>;
  110. /**
  111. * Writes a UTF-8 text file.
  112. * @example
  113. * ```typescript
  114. * import { writeTextFile, BaseDirectory } from '@tauri-apps/api/fs';
  115. * // Write a text file to the `$APPCONFIG/app.conf` path
  116. * await writeTextFile('app.conf', 'file contents', { dir: BaseDirectory.AppConfig });
  117. * ```
  118. *
  119. * @since 1.0.0
  120. */
  121. declare function writeTextFile(path: string, contents: string, options?: FsOptions): Promise<void>;
  122. /**
  123. * Writes a UTF-8 text file.
  124. * @example
  125. * ```typescript
  126. * import { writeTextFile, BaseDirectory } from '@tauri-apps/api/fs';
  127. * // Write a text file to the `$APPCONFIG/app.conf` path
  128. * await writeTextFile({ path: 'app.conf', contents: 'file contents' }, { dir: BaseDirectory.AppConfig });
  129. * ```
  130. * @returns A promise indicating the success or failure of the operation.
  131. *
  132. * @since 1.0.0
  133. */
  134. declare function writeTextFile(file: FsTextFileOption, options?: FsOptions): Promise<void>;
  135. /**
  136. * Writes a byte array content to a file.
  137. * @example
  138. * ```typescript
  139. * import { writeBinaryFile, BaseDirectory } from '@tauri-apps/api/fs';
  140. * // Write a binary file to the `$APPDATA/avatar.png` path
  141. * await writeBinaryFile('avatar.png', new Uint8Array([]), { dir: BaseDirectory.AppData });
  142. * ```
  143. *
  144. * @param options Configuration object.
  145. * @returns A promise indicating the success or failure of the operation.
  146. *
  147. * @since 1.0.0
  148. */
  149. declare function writeBinaryFile(path: string, contents: BinaryFileContents, options?: FsOptions): Promise<void>;
  150. /**
  151. * Writes a byte array content to a file.
  152. * @example
  153. * ```typescript
  154. * import { writeBinaryFile, BaseDirectory } from '@tauri-apps/api/fs';
  155. * // Write a binary file to the `$APPDATA/avatar.png` path
  156. * await writeBinaryFile({ path: 'avatar.png', contents: new Uint8Array([]) }, { dir: BaseDirectory.AppData });
  157. * ```
  158. *
  159. * @param file The object containing the file path and contents.
  160. * @param options Configuration object.
  161. * @returns A promise indicating the success or failure of the operation.
  162. *
  163. * @since 1.0.0
  164. */
  165. declare function writeBinaryFile(file: FsBinaryFileOption, options?: FsOptions): Promise<void>;
  166. /**
  167. * List directory files.
  168. * @example
  169. * ```typescript
  170. * import { readDir, BaseDirectory } from '@tauri-apps/api/fs';
  171. * // Reads the `$APPDATA/users` directory recursively
  172. * const entries = await readDir('users', { dir: BaseDirectory.AppData, recursive: true });
  173. *
  174. * function processEntries(entries) {
  175. * for (const entry of entries) {
  176. * console.log(`Entry: ${entry.path}`);
  177. * if (entry.children) {
  178. * processEntries(entry.children)
  179. * }
  180. * }
  181. * }
  182. * ```
  183. *
  184. * @since 1.0.0
  185. */
  186. declare function readDir(dir: string, options?: FsDirOptions): Promise<FileEntry[]>;
  187. /**
  188. * Creates a directory.
  189. * If one of the path's parent components doesn't exist
  190. * and the `recursive` option isn't set to true, the promise will be rejected.
  191. * @example
  192. * ```typescript
  193. * import { createDir, BaseDirectory } from '@tauri-apps/api/fs';
  194. * // Create the `$APPDATA/users` directory
  195. * await createDir('users', { dir: BaseDirectory.AppData, recursive: true });
  196. * ```
  197. *
  198. * @returns A promise indicating the success or failure of the operation.
  199. *
  200. * @since 1.0.0
  201. */
  202. declare function createDir(dir: string, options?: FsDirOptions): Promise<void>;
  203. /**
  204. * Removes a directory.
  205. * If the directory is not empty and the `recursive` option isn't set to true, the promise will be rejected.
  206. * @example
  207. * ```typescript
  208. * import { removeDir, BaseDirectory } from '@tauri-apps/api/fs';
  209. * // Remove the directory `$APPDATA/users`
  210. * await removeDir('users', { dir: BaseDirectory.AppData });
  211. * ```
  212. *
  213. * @returns A promise indicating the success or failure of the operation.
  214. *
  215. * @since 1.0.0
  216. */
  217. declare function removeDir(dir: string, options?: FsDirOptions): Promise<void>;
  218. /**
  219. * Copies a file to a destination.
  220. * @example
  221. * ```typescript
  222. * import { copyFile, BaseDirectory } from '@tauri-apps/api/fs';
  223. * // Copy the `$APPCONFIG/app.conf` file to `$APPCONFIG/app.conf.bk`
  224. * await copyFile('app.conf', 'app.conf.bk', { dir: BaseDirectory.AppConfig });
  225. * ```
  226. *
  227. * @returns A promise indicating the success or failure of the operation.
  228. *
  229. * @since 1.0.0
  230. */
  231. declare function copyFile(source: string, destination: string, options?: FsOptions): Promise<void>;
  232. /**
  233. * Removes a file.
  234. * @example
  235. * ```typescript
  236. * import { removeFile, BaseDirectory } from '@tauri-apps/api/fs';
  237. * // Remove the `$APPConfig/app.conf` file
  238. * await removeFile('app.conf', { dir: BaseDirectory.AppConfig });
  239. * ```
  240. *
  241. * @returns A promise indicating the success or failure of the operation.
  242. *
  243. * @since 1.0.0
  244. */
  245. declare function removeFile(file: string, options?: FsOptions): Promise<void>;
  246. /**
  247. * Renames a file.
  248. * @example
  249. * ```typescript
  250. * import { renameFile, BaseDirectory } from '@tauri-apps/api/fs';
  251. * // Rename the `$APPDATA/avatar.png` file
  252. * await renameFile('avatar.png', 'deleted.png', { dir: BaseDirectory.AppData });
  253. * ```
  254. *
  255. * @returns A promise indicating the success or failure of the operation.
  256. *
  257. * @since 1.0.0
  258. */
  259. declare function renameFile(oldPath: string, newPath: string, options?: FsOptions): Promise<void>;
  260. /**
  261. * Check if a path exists.
  262. * @example
  263. * ```typescript
  264. * import { exists, BaseDirectory } from '@tauri-apps/api/fs';
  265. * // Check if the `$APPDATA/avatar.png` file exists
  266. * await exists('avatar.png', { dir: BaseDirectory.AppData });
  267. * ```
  268. *
  269. * @since 1.1.0
  270. */
  271. declare function exists(path: string, options?: FsOptions): Promise<boolean>;
  272. export type { FsOptions, FsDirOptions, FsTextFileOption, BinaryFileContents, FsBinaryFileOption, FileEntry };
  273. export { BaseDirectory as Dir, readTextFile, readBinaryFile, writeTextFile, writeTextFile as writeFile, writeBinaryFile, readDir, createDir, removeDir, copyFile, removeFile, renameFile, exists };