fs.ts 5.4 KB

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