fs.js 2.6 KB

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