fs.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import { promisified } from './tauri'
  2. export enum BaseDirectory {
  3. Audio = 1,
  4. Cache,
  5. Config,
  6. Data,
  7. LocalData,
  8. Desktop,
  9. Document,
  10. Download,
  11. Executable,
  12. Font,
  13. Home,
  14. Picture,
  15. Public,
  16. Runtime,
  17. Template,
  18. Video,
  19. Resource,
  20. App,
  21. }
  22. export interface FsOptions {
  23. dir?: BaseDirectory
  24. }
  25. export interface FsTextFileOption {
  26. path: string
  27. contents: string
  28. }
  29. export interface FsBinaryFileOption {
  30. path: string
  31. contents: ArrayBuffer
  32. }
  33. export interface FileEntry {
  34. path: string
  35. // name of the directory/file
  36. // can be null if the path terminates with `..`
  37. name?: string
  38. // children of this entry if it's a directory; null otherwise
  39. children?: FileEntry[]
  40. }
  41. /**
  42. * @name readTextFile
  43. * @description Reads a file as text
  44. * @param {string} filePath path to the file
  45. * @param {FsOptions} [options] configuration object
  46. * @param {BaseDirectory} [options.dir] base directory
  47. * @return {Promise<string>}
  48. */
  49. async function readTextFile(
  50. filePath: string,
  51. options: FsOptions = {}
  52. ): Promise<string> {
  53. return await promisified<string>({
  54. cmd: 'readTextFile',
  55. path: filePath,
  56. options
  57. })
  58. }
  59. /**
  60. * @name readBinaryFile
  61. * @description Reads a file as binary
  62. * @param {string} filePath path to the file
  63. * @param {FsOptions} [options] configuration object
  64. * @param {BaseDirectory} [options.dir] base directory
  65. * @return {Promise<number[]>}
  66. */
  67. async function readBinaryFile(
  68. filePath: string,
  69. options: FsOptions = {}
  70. ): Promise<number[]> {
  71. return await promisified<number[]>({
  72. cmd: 'readBinaryFile',
  73. path: filePath,
  74. options
  75. })
  76. }
  77. /**
  78. * writes a text file
  79. *
  80. * @param file
  81. * @param file.path path of the file
  82. * @param file.contents contents of the file
  83. * @param [options] configuration object
  84. * @param [options.dir] base directory
  85. * @return
  86. */
  87. async function writeFile(file: FsTextFileOption, options: FsOptions = {}): Promise<void> {
  88. if (typeof options === 'object') {
  89. Object.freeze(options)
  90. }
  91. if (typeof file === 'object') {
  92. Object.freeze(file)
  93. }
  94. return await promisified({
  95. cmd: 'writeFile',
  96. path: file.path,
  97. contents: file.contents,
  98. options
  99. })
  100. }
  101. const CHUNK_SIZE = 65536
  102. /**
  103. * convert an Uint8Array to ascii string
  104. *
  105. * @param arr
  106. * @return ASCII string
  107. */
  108. function uint8ArrayToString(arr: Uint8Array): string {
  109. if (arr.length < CHUNK_SIZE) {
  110. return String.fromCharCode.apply(null, Array.from(arr))
  111. }
  112. let result = ''
  113. const arrLen = arr.length
  114. for (let i = 0; i < arrLen; i++) {
  115. const chunk = arr.subarray(i * CHUNK_SIZE, (i + 1) * CHUNK_SIZE)
  116. result += String.fromCharCode.apply(null, Array.from(chunk))
  117. }
  118. return result
  119. }
  120. /**
  121. * convert an ArrayBuffer to base64 encoded string
  122. *
  123. * @param buffer
  124. * @return base64 encoded string
  125. */
  126. function arrayBufferToBase64(buffer: ArrayBuffer): string {
  127. const str = uint8ArrayToString(new Uint8Array(buffer))
  128. return btoa(str)
  129. }
  130. /**
  131. * writes a binary file
  132. *
  133. * @param file
  134. * @param file.path path of the file
  135. * @param file.contents contents of the file
  136. * @param [options] configuration object
  137. * @param [options.dir] base directory
  138. * @return
  139. */
  140. async function writeBinaryFile(file: FsBinaryFileOption, options: FsOptions = {}): Promise<void> {
  141. if (typeof options === 'object') {
  142. Object.freeze(options)
  143. }
  144. if (typeof file === 'object') {
  145. Object.freeze(file)
  146. }
  147. return await promisified({
  148. cmd: 'writeFile',
  149. path: file.path,
  150. contents: arrayBufferToBase64(file.contents),
  151. options
  152. })
  153. }
  154. /**
  155. * list directory files
  156. *
  157. * @param dir path to the directory to read
  158. * @param [options] configuration object
  159. * @param [options.recursive] whether to list dirs recursively or not
  160. * @param [options.dir] base directory
  161. * @return
  162. */
  163. async function readDir(dir: string, options: FsOptions = {}): Promise<FileEntry[]> {
  164. return await promisified({
  165. cmd: 'readDir',
  166. path: dir,
  167. options
  168. })
  169. }
  170. /**
  171. * Creates a directory
  172. * If one of the path's parent components doesn't exist
  173. * and the `recursive` option isn't set to true, it will be rejected
  174. *
  175. * @param dir path to the directory to create
  176. * @param [options] configuration object
  177. * @param [options.recursive] whether to create the directory's parent components or not
  178. * @param [options.dir] base directory
  179. * @return
  180. */
  181. async function createDir(dir: string, options: FsOptions = {}): Promise<void> {
  182. return await promisified({
  183. cmd: 'createDir',
  184. path: dir,
  185. options
  186. })
  187. }
  188. /**
  189. * Removes a directory
  190. * If the directory is not empty and the `recursive` option isn't set to true, it will be rejected
  191. *
  192. * @param dir path to the directory to remove
  193. * @param [options] configuration object
  194. * @param [options.recursive] whether to remove all of the directory's content or not
  195. * @param [options.dir] base directory
  196. * @return
  197. */
  198. async function removeDir(dir: string, options: FsOptions = {}): Promise<void> {
  199. return await promisified({
  200. cmd: 'removeDir',
  201. path: dir,
  202. options
  203. })
  204. }
  205. /**
  206. * Copy file
  207. *
  208. * @param source
  209. * @param destination
  210. * @param [options] configuration object
  211. * @param [options.dir] base directory
  212. * @return
  213. */
  214. async function copyFile(source: string, destination: string, options: FsOptions = {}): Promise<void> {
  215. return await promisified({
  216. cmd: 'copyFile',
  217. source,
  218. destination,
  219. options
  220. })
  221. }
  222. /**
  223. * Removes a file
  224. *
  225. * @param file path to the file to remove
  226. * @param [options] configuration object
  227. * @param [options.dir] base directory
  228. * @return
  229. */
  230. async function removeFile(file: string, options: FsOptions = {}): Promise<void> {
  231. return await promisified({
  232. cmd: 'removeFile',
  233. path: file,
  234. options: options
  235. })
  236. }
  237. /**
  238. * Renames a file
  239. *
  240. * @param oldPath
  241. * @param newPath
  242. * @param [options] configuration object
  243. * @param [options.dir] base directory
  244. * @return
  245. */
  246. async function renameFile(oldPath: string, newPath: string, options: FsOptions = {}): Promise<void> {
  247. return await promisified({
  248. cmd: 'renameFile',
  249. oldPath,
  250. newPath,
  251. options
  252. })
  253. }
  254. export {
  255. BaseDirectory as Dir,
  256. readTextFile,
  257. readBinaryFile,
  258. writeFile,
  259. writeBinaryFile,
  260. readDir,
  261. createDir,
  262. removeDir,
  263. copyFile,
  264. removeFile,
  265. renameFile
  266. }