Client.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. let Client
  16. const request = require('request')
  17. const fs = require('fs')
  18. const Settings = require('settings-sharelatex')
  19. const host = 'localhost'
  20. module.exports = Client = {
  21. host: Settings.apis.clsi.url,
  22. randomId() {
  23. return Math.random()
  24. .toString(16)
  25. .slice(2)
  26. },
  27. compile(project_id, data, callback) {
  28. if (callback == null) {
  29. callback = function(error, res, body) {}
  30. }
  31. return request.post(
  32. {
  33. url: `${this.host}/project/${project_id}/compile`,
  34. json: {
  35. compile: data
  36. }
  37. },
  38. callback
  39. )
  40. },
  41. clearCache(project_id, callback) {
  42. if (callback == null) {
  43. callback = function(error, res, body) {}
  44. }
  45. return request.del(`${this.host}/project/${project_id}`, callback)
  46. },
  47. getOutputFile(response, type) {
  48. for (const file of Array.from(response.compile.outputFiles)) {
  49. if (file.type === type && file.url.match(`output.${type}`)) {
  50. return file
  51. }
  52. }
  53. return null
  54. },
  55. runServer(port, directory) {
  56. const express = require('express')
  57. const app = express()
  58. app.use(express.static(directory))
  59. console.log('starting test server on', port, host)
  60. return app.listen(port, host).on('error', error => {
  61. console.error('error starting server:', error.message)
  62. return process.exit(1)
  63. })
  64. },
  65. syncFromCode(project_id, file, line, column, callback) {
  66. if (callback == null) {
  67. callback = function(error, pdfPositions) {}
  68. }
  69. return request.get(
  70. {
  71. url: `${this.host}/project/${project_id}/sync/code`,
  72. qs: {
  73. file,
  74. line,
  75. column
  76. },
  77. json: true
  78. },
  79. (error, response, body) => {
  80. if (error != null) {
  81. return callback(error)
  82. }
  83. return callback(null, body)
  84. }
  85. )
  86. },
  87. syncFromPdf(project_id, page, h, v, callback) {
  88. if (callback == null) {
  89. callback = function(error, pdfPositions) {}
  90. }
  91. return request.get(
  92. {
  93. url: `${this.host}/project/${project_id}/sync/pdf`,
  94. qs: {
  95. page,
  96. h,
  97. v
  98. },
  99. json: true
  100. },
  101. (error, response, body) => {
  102. if (error != null) {
  103. return callback(error)
  104. }
  105. return callback(null, body)
  106. }
  107. )
  108. },
  109. compileDirectory(project_id, baseDirectory, directory, serverPort, callback) {
  110. if (callback == null) {
  111. callback = function(error, res, body) {}
  112. }
  113. const resources = []
  114. let entities = fs.readdirSync(`${baseDirectory}/${directory}`)
  115. let rootResourcePath = 'main.tex'
  116. while (entities.length > 0) {
  117. var entity = entities.pop()
  118. const stat = fs.statSync(`${baseDirectory}/${directory}/${entity}`)
  119. if (stat.isDirectory()) {
  120. entities = entities.concat(
  121. fs
  122. .readdirSync(`${baseDirectory}/${directory}/${entity}`)
  123. .map(subEntity => {
  124. if (subEntity === 'main.tex') {
  125. rootResourcePath = `${entity}/${subEntity}`
  126. }
  127. return `${entity}/${subEntity}`
  128. })
  129. )
  130. } else if (stat.isFile() && entity !== 'output.pdf') {
  131. const extension = entity.split('.').pop()
  132. if (
  133. [
  134. 'tex',
  135. 'bib',
  136. 'cls',
  137. 'sty',
  138. 'pdf_tex',
  139. 'Rtex',
  140. 'ist',
  141. 'md',
  142. 'Rmd'
  143. ].indexOf(extension) > -1
  144. ) {
  145. resources.push({
  146. path: entity,
  147. content: fs
  148. .readFileSync(`${baseDirectory}/${directory}/${entity}`)
  149. .toString()
  150. })
  151. } else if (
  152. ['eps', 'ttf', 'png', 'jpg', 'pdf', 'jpeg'].indexOf(extension) > -1
  153. ) {
  154. resources.push({
  155. path: entity,
  156. url: `http://${host}:${serverPort}/${directory}/${entity}`,
  157. modified: stat.mtime
  158. })
  159. }
  160. }
  161. }
  162. return fs.readFile(
  163. `${baseDirectory}/${directory}/options.json`,
  164. (error, body) => {
  165. const req = {
  166. resources,
  167. rootResourcePath
  168. }
  169. if (error == null) {
  170. body = JSON.parse(body)
  171. req.options = body
  172. }
  173. return this.compile(project_id, req, callback)
  174. }
  175. )
  176. },
  177. wordcount(project_id, file, callback) {
  178. const image = undefined
  179. Client.wordcountWithImage(project_id, file, image, callback)
  180. },
  181. wordcountWithImage(project_id, file, image, callback) {
  182. if (callback == null) {
  183. callback = function(error, pdfPositions) {}
  184. }
  185. return request.get(
  186. {
  187. url: `${this.host}/project/${project_id}/wordcount`,
  188. qs: {
  189. image,
  190. file
  191. }
  192. },
  193. (error, response, body) => {
  194. if (error != null) {
  195. return callback(error)
  196. }
  197. if (response.statusCode !== 200) {
  198. return callback(new Error(`statusCode=${response.statusCode}`))
  199. }
  200. return callback(null, JSON.parse(body))
  201. }
  202. )
  203. }
  204. }