fs.ts 6.2 KB

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