Client.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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().toString(16).slice(2)
  24. },
  25. compile(project_id, data, callback) {
  26. if (callback == null) {
  27. callback = function (error, res, body) {}
  28. }
  29. return request.post(
  30. {
  31. url: `${this.host}/project/${project_id}/compile`,
  32. json: {
  33. compile: data
  34. }
  35. },
  36. callback
  37. )
  38. },
  39. clearCache(project_id, callback) {
  40. if (callback == null) {
  41. callback = function (error, res, body) {}
  42. }
  43. return request.del(`${this.host}/project/${project_id}`, callback)
  44. },
  45. getOutputFile(response, type) {
  46. for (const file of Array.from(response.compile.outputFiles)) {
  47. if (file.type === type && file.url.match(`output.${type}`)) {
  48. return file
  49. }
  50. }
  51. return null
  52. },
  53. runServer(port, directory) {
  54. const express = require('express')
  55. const app = express()
  56. app.use(express.static(directory))
  57. console.log('starting test server on', port, host)
  58. return app.listen(port, host).on('error', (error) => {
  59. console.error('error starting server:', error.message)
  60. return process.exit(1)
  61. })
  62. },
  63. syncFromCode(project_id, file, line, column, callback) {
  64. if (callback == null) {
  65. callback = function (error, pdfPositions) {}
  66. }
  67. return request.get(
  68. {
  69. url: `${this.host}/project/${project_id}/sync/code`,
  70. qs: {
  71. file,
  72. line,
  73. column
  74. },
  75. json: true
  76. },
  77. (error, response, body) => {
  78. if (error != null) {
  79. return callback(error)
  80. }
  81. return callback(null, body)
  82. }
  83. )
  84. },
  85. syncFromPdf(project_id, page, h, v, callback) {
  86. if (callback == null) {
  87. callback = function (error, pdfPositions) {}
  88. }
  89. return request.get(
  90. {
  91. url: `${this.host}/project/${project_id}/sync/pdf`,
  92. qs: {
  93. page,
  94. h,
  95. v
  96. },
  97. json: true
  98. },
  99. (error, response, body) => {
  100. if (error != null) {
  101. return callback(error)
  102. }
  103. return callback(null, body)
  104. }
  105. )
  106. },
  107. compileDirectory(project_id, baseDirectory, directory, serverPort, callback) {
  108. if (callback == null) {
  109. callback = function (error, res, body) {}
  110. }
  111. const resources = []
  112. let entities = fs.readdirSync(`${baseDirectory}/${directory}`)
  113. let rootResourcePath = 'main.tex'
  114. while (entities.length > 0) {
  115. var entity = entities.pop()
  116. const stat = fs.statSync(`${baseDirectory}/${directory}/${entity}`)
  117. if (stat.isDirectory()) {
  118. entities = entities.concat(
  119. fs
  120. .readdirSync(`${baseDirectory}/${directory}/${entity}`)
  121. .map((subEntity) => {
  122. if (subEntity === 'main.tex') {
  123. rootResourcePath = `${entity}/${subEntity}`
  124. }
  125. return `${entity}/${subEntity}`
  126. })
  127. )
  128. } else if (stat.isFile() && entity !== 'output.pdf') {
  129. const extension = entity.split('.').pop()
  130. if (
  131. [
  132. 'tex',
  133. 'bib',
  134. 'cls',
  135. 'sty',
  136. 'pdf_tex',
  137. 'Rtex',
  138. 'ist',
  139. 'md',
  140. 'Rmd'
  141. ].indexOf(extension) > -1
  142. ) {
  143. resources.push({
  144. path: entity,
  145. content: fs
  146. .readFileSync(`${baseDirectory}/${directory}/${entity}`)
  147. .toString()
  148. })
  149. } else if (
  150. ['eps', 'ttf', 'png', 'jpg', 'pdf', 'jpeg'].indexOf(extension) > -1
  151. ) {
  152. resources.push({
  153. path: entity,
  154. url: `http://${host}:${serverPort}/${directory}/${entity}`,
  155. modified: stat.mtime
  156. })
  157. }
  158. }
  159. }
  160. return fs.readFile(
  161. `${baseDirectory}/${directory}/options.json`,
  162. (error, body) => {
  163. const req = {
  164. resources,
  165. rootResourcePath
  166. }
  167. if (error == null) {
  168. body = JSON.parse(body)
  169. req.options = body
  170. }
  171. return this.compile(project_id, req, callback)
  172. }
  173. )
  174. },
  175. wordcount(project_id, file, callback) {
  176. const image = undefined
  177. Client.wordcountWithImage(project_id, file, image, callback)
  178. },
  179. wordcountWithImage(project_id, file, image, callback) {
  180. if (callback == null) {
  181. callback = function (error, pdfPositions) {}
  182. }
  183. return request.get(
  184. {
  185. url: `${this.host}/project/${project_id}/wordcount`,
  186. qs: {
  187. image,
  188. file
  189. }
  190. },
  191. (error, response, body) => {
  192. if (error != null) {
  193. return callback(error)
  194. }
  195. if (response.statusCode !== 200) {
  196. return callback(new Error(`statusCode=${response.statusCode}`))
  197. }
  198. return callback(null, JSON.parse(body))
  199. }
  200. )
  201. }
  202. }