index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import tauri from '../tauri'
  2. import { Dir } from './dir'
  3. /**
  4. * reads a file as text
  5. *
  6. * @param {String} filePath path to the file
  7. * @param {Object} [options] configuration object
  8. * @param {BaseDirectory} [options.dir] base directory
  9. * @return {Promise<string>}
  10. */
  11. function readTextFile (filePath, options = {}) {
  12. return tauri.readTextFile(filePath, options)
  13. }
  14. /**
  15. * reads a file as binary
  16. *
  17. * @param {String} filePath path to the file
  18. * @param {Object} [options] configuration object
  19. * @param {BaseDirectory} [options.dir] base directory
  20. * @return {Promise<int[]>}
  21. */
  22. function readBinaryFile (filePath, options = {}) {
  23. return tauri.readBinaryFile(filePath, options)
  24. }
  25. /**
  26. * writes a text file
  27. *
  28. * @param {Object} file
  29. * @param {String} file.path path of the file
  30. * @param {String} file.contents contents of the file
  31. * @param {Object} [options] configuration object
  32. * @param {BaseDirectory} [options.dir] base directory
  33. * @return {Promise<void>}
  34. */
  35. function writeFile (file, options = {}) {
  36. return tauri.writeFile(file, options)
  37. }
  38. /**
  39. * @typedef {Object} FileEntry
  40. * @property {String} path
  41. * @property {Boolean} is_dir
  42. * @property {String} name
  43. */
  44. /**
  45. * list directory files
  46. *
  47. * @param {String} dir path to the directory to read
  48. * @param {Object} [options] configuration object
  49. * @param {Boolean} [options.recursive] whether to list dirs recursively or not
  50. * @param {BaseDirectory} [options.dir] base directory
  51. * @return {Promise<FileEntry[]>}
  52. */
  53. function readDir (dir, options = {}) {
  54. return tauri.readDir(dir, options)
  55. }
  56. /**
  57. * Creates a directory
  58. * If one of the path's parent components doesn't exist
  59. * and the `recursive` option isn't set to true, it will be rejected
  60. *
  61. * @param {String} dir path to the directory to create
  62. * @param {Object} [options] configuration object
  63. * @param {Boolean} [options.recursive] whether to create the directory's parent components or not
  64. * @param {BaseDirectory} [options.dir] base directory
  65. * @return {Promise<void>}
  66. */
  67. function createDir (dir, options = {}) {
  68. return tauri.createDir(dir, options)
  69. }
  70. /**
  71. * Removes a directory
  72. * If the directory is not empty and the `recursive` option isn't set to true, it will be rejected
  73. *
  74. * @param {String} dir path to the directory to remove
  75. * @param {Object} [options] configuration object
  76. * @param {Boolean} [options.recursive] whether to remove all of the directory's content or not
  77. * @param {BaseDirectory} [options.dir] base directory
  78. * @return {Promise<void>}
  79. */
  80. function removeDir (dir, options = {}) {
  81. return tauri.removeDir(dir, options)
  82. }
  83. /**
  84. * Copy file
  85. *
  86. * @param {string} source
  87. * @param {string} destination
  88. * @param {object} [options] configuration object
  89. * @param {BaseDirectory} [options.dir] base directory
  90. * @return {Promise<void>}
  91. */
  92. function copyFile (source, destination, options = {}) {
  93. return tauri.copyFile(source, destination, options)
  94. }
  95. /**
  96. * Removes a file
  97. *
  98. * @param {String} file path to the file to remove
  99. * @param {Object} [options] configuration object
  100. * @param {BaseDirectory} [options.dir] base directory
  101. * @return {Promise<void>}
  102. */
  103. function removeFile (file, options = {}) {
  104. return tauri.removeFile(file, options)
  105. }
  106. /**
  107. * Renames a file
  108. *
  109. * @param {String} oldPath
  110. * @param {String} newPath
  111. * @param {Object} [options] configuration object
  112. * @param {BaseDirectory} [options.dir] base directory
  113. * @return {Promise<void>}
  114. */
  115. function renameFile (oldPath, newPath, options = {}) {
  116. return tauri.renameFile(oldPath, newPath, options)
  117. }
  118. export {
  119. Dir,
  120. readTextFile,
  121. readBinaryFile,
  122. writeFile,
  123. readDir,
  124. createDir,
  125. removeDir,
  126. copyFile,
  127. removeFile,
  128. renameFile
  129. }