Modules.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-path-concat,
  5. no-unused-vars,
  6. one-var,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS101: Remove unnecessary use of Array.from
  13. * DS102: Remove unnecessary code created because of implicit returns
  14. * DS103: Rewrite code to no longer use __guard__
  15. * DS201: Simplify complex destructure assignments
  16. * DS205: Consider reworking code to avoid use of IIFEs
  17. * DS207: Consider shorter variations of null checks
  18. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  19. */
  20. let Modules
  21. const fs = require('fs')
  22. const Path = require('path')
  23. const pug = require('pug')
  24. const async = require('async')
  25. const Settings = require('settings-sharelatex')
  26. const MODULE_BASE_PATH = Path.resolve(__dirname + '/../../../modules')
  27. module.exports = Modules = {
  28. modules: [],
  29. loadModules() {
  30. for (let moduleName of Array.from(fs.readdirSync(MODULE_BASE_PATH))) {
  31. if (fs.existsSync(Path.join(MODULE_BASE_PATH, moduleName, 'index.js'))) {
  32. const loadedModule = require(Path.join(
  33. MODULE_BASE_PATH,
  34. moduleName,
  35. 'index'
  36. ))
  37. loadedModule.name = moduleName
  38. this.modules.push(loadedModule)
  39. }
  40. }
  41. return Modules.attachHooks()
  42. },
  43. applyRouter(webRouter, privateApiRouter, publicApiRouter) {
  44. return Array.from(this.modules).map(module =>
  45. __guardMethod__(module.router, 'apply', o =>
  46. o.apply(webRouter, privateApiRouter, publicApiRouter)
  47. )
  48. )
  49. },
  50. applyNonCsrfRouter(webRouter, privateApiRouter, publicApiRouter) {
  51. return (() => {
  52. const result = []
  53. for (let module of Array.from(this.modules)) {
  54. if (module.nonCsrfRouter != null) {
  55. module.nonCsrfRouter.apply(
  56. webRouter,
  57. privateApiRouter,
  58. publicApiRouter
  59. )
  60. }
  61. result.push(
  62. __guardMethod__(module.router, 'applyNonCsrfRouter', o =>
  63. o.applyNonCsrfRouter(webRouter, privateApiRouter, publicApiRouter)
  64. )
  65. )
  66. }
  67. return result
  68. })()
  69. },
  70. viewIncludes: {},
  71. loadViewIncludes(app) {
  72. this.viewIncludes = {}
  73. return Array.from(this.modules).map(module =>
  74. (() => {
  75. const result = []
  76. const object = module.viewIncludes || {}
  77. for (let view in object) {
  78. const partial = object[view]
  79. if (!this.viewIncludes[view]) {
  80. this.viewIncludes[view] = []
  81. }
  82. const filePath = Path.join(
  83. MODULE_BASE_PATH,
  84. module.name,
  85. 'app/views',
  86. partial + '.pug'
  87. )
  88. result.push(
  89. this.viewIncludes[view].push(
  90. pug.compileFile(filePath, {
  91. doctype: 'html',
  92. compileDebug: Settings.debugPugTemplates
  93. })
  94. )
  95. )
  96. }
  97. return result
  98. })()
  99. )
  100. },
  101. moduleIncludes(view, locals) {
  102. const compiledPartials = Modules.viewIncludes[view] || []
  103. let html = ''
  104. for (let compiledPartial of Array.from(compiledPartials)) {
  105. const d = new Date()
  106. html += compiledPartial(locals)
  107. }
  108. return html
  109. },
  110. moduleIncludesAvailable(view) {
  111. return (Modules.viewIncludes[view] || []).length > 0
  112. },
  113. linkedFileAgentsIncludes() {
  114. const agents = {}
  115. for (let module of Array.from(this.modules)) {
  116. for (let name in module.linkedFileAgents) {
  117. const agentFunction = module.linkedFileAgents[name]
  118. agents[name] = agentFunction()
  119. }
  120. }
  121. return agents
  122. },
  123. attachHooks() {
  124. return (() => {
  125. const result = []
  126. for (var module of Array.from(this.modules)) {
  127. if (module.hooks != null) {
  128. result.push(
  129. (() => {
  130. const result1 = []
  131. for (let hook in module.hooks) {
  132. const method = module.hooks[hook]
  133. result1.push(Modules.hooks.attach(hook, method))
  134. }
  135. return result1
  136. })()
  137. )
  138. } else {
  139. result.push(undefined)
  140. }
  141. }
  142. return result
  143. })()
  144. },
  145. hooks: {
  146. _hooks: {},
  147. attach(name, method) {
  148. if (this._hooks[name] == null) {
  149. this._hooks[name] = []
  150. }
  151. return this._hooks[name].push(method)
  152. },
  153. fire(name, ...rest) {
  154. const adjustedLength = Math.max(rest.length, 1),
  155. args = rest.slice(0, adjustedLength - 1),
  156. callback = rest[adjustedLength - 1]
  157. const methods = this._hooks[name] || []
  158. const call_methods = methods.map(method => cb =>
  159. method(...Array.from(args), cb)
  160. )
  161. return async.series(call_methods, function(error, results) {
  162. if (error != null) {
  163. return callback(error)
  164. }
  165. return callback(null, results)
  166. })
  167. }
  168. }
  169. }
  170. Modules.loadModules()
  171. function __guardMethod__(obj, methodName, transform) {
  172. if (
  173. typeof obj !== 'undefined' &&
  174. obj !== null &&
  175. typeof obj[methodName] === 'function'
  176. ) {
  177. return transform(obj, methodName)
  178. } else {
  179. return undefined
  180. }
  181. }