Client.js 6.2 KB

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