index.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <script>
  5. function callBackEnd(route, args) {
  6. console.log(route)
  7. console.log(args)
  8. window.__TAURI__.http
  9. .fetch('http://localhost:7000/' + route, {
  10. method: 'POST',
  11. body: window.__TAURI__.http.Body.json(args),
  12. responseType: window.__TAURI__.http.ResponseType.Binary
  13. })
  14. .catch(console.error)
  15. }
  16. function reply(args) {
  17. return callBackEnd('reply', args)
  18. }
  19. function sendError(error) {
  20. return callBackEnd('error', error)
  21. }
  22. function testFs(dir) {
  23. var contents = 'TAURI E2E TEST FILE'
  24. var commandSuffix = dir ? 'WithDir' : ''
  25. var options = {
  26. dir: dir || null
  27. }
  28. return window.__TAURI__.fs
  29. .writeFile(
  30. {
  31. path: 'tauri-test.txt',
  32. contents: contents
  33. },
  34. options
  35. )
  36. .then(function (res) {
  37. reply({
  38. cmd: 'writeFile' + commandSuffix
  39. })
  40. return window.__TAURI__.fs
  41. .readTextFile('tauri-test.txt', options)
  42. .then(function (res) {
  43. if (res === contents) {
  44. reply({
  45. cmd: 'readFile' + commandSuffix
  46. })
  47. return window.__TAURI__.fs
  48. .readDir('.', options)
  49. .then((res) => {
  50. reply({
  51. cmd: 'readDir' + commandSuffix
  52. })
  53. return window.__TAURI__.fs
  54. .copyFile(
  55. 'tauri-test.txt',
  56. 'tauri-test-copy.txt',
  57. options
  58. )
  59. .then(function (res) {
  60. reply({
  61. cmd: 'copyFile' + commandSuffix
  62. })
  63. return window.__TAURI__.fs
  64. .createDir('tauri-test-dir', options)
  65. .then(function (res) {
  66. reply({
  67. cmd: 'createDir' + commandSuffix
  68. })
  69. return window.__TAURI__.fs
  70. .removeDir('tauri-test-dir', options)
  71. .then(function (res) {
  72. reply({
  73. cmd: 'removeDir' + commandSuffix
  74. })
  75. })
  76. })
  77. .then(function (res) {
  78. return window.__TAURI__.fs
  79. .renameFile(
  80. 'tauri-test.txt',
  81. 'tauri.testt.txt',
  82. options
  83. )
  84. .then(function (res) {
  85. reply({
  86. cmd: 'renameFile' + commandSuffix
  87. })
  88. return Promise.all([
  89. window.__TAURI__.fs.removeFile(
  90. 'tauri.testt.txt',
  91. options
  92. ),
  93. window.__TAURI__.fs.removeFile(
  94. 'tauri-test-copy.txt',
  95. options
  96. )
  97. ]).then(function (res) {
  98. reply({
  99. cmd: 'removeFile' + commandSuffix
  100. })
  101. })
  102. })
  103. })
  104. })
  105. })
  106. } else {
  107. sendError('expected "' + contents + '" found "' + res + '"')
  108. }
  109. })
  110. })
  111. .catch(sendError)
  112. }
  113. window.__TAURI__.event.listen('reply', function (res) {
  114. reply({
  115. cmd: 'listen'
  116. })
  117. })
  118. window.onTauriInit = function () {
  119. window.__TAURI__.event.emit('hello')
  120. }
  121. testFs(null).then(function () {
  122. testFs(window.__TAURI__.fs.Dir.Config)
  123. })
  124. setTimeout(function () {
  125. window.__TAURI__.invoke('exit')
  126. }, 15000)
  127. </script>
  128. </body>
  129. </html>