Modules.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 MODULE_BASE_PATH = Path.resolve(__dirname + '/../../../modules')
  26. module.exports = Modules = {
  27. modules: [],
  28. loadModules() {
  29. for (let moduleName of Array.from(fs.readdirSync(MODULE_BASE_PATH))) {
  30. if (fs.existsSync(Path.join(MODULE_BASE_PATH, moduleName, 'index.js'))) {
  31. const loadedModule = require(Path.join(
  32. MODULE_BASE_PATH,
  33. moduleName,
  34. 'index'
  35. ))
  36. loadedModule.name = moduleName
  37. this.modules.push(loadedModule)
  38. }
  39. }
  40. return Modules.attachHooks()
  41. },
  42. applyRouter(webRouter, privateApiRouter, publicApiRouter) {
  43. return Array.from(this.modules).map(module =>
  44. __guardMethod__(module.router, 'apply', o =>
  45. o.apply(webRouter, privateApiRouter, publicApiRouter)
  46. )
  47. )
  48. },
  49. applyNonCsrfRouter(webRouter, privateApiRouter, publicApiRouter) {
  50. return (() => {
  51. const result = []
  52. for (let module of Array.from(this.modules)) {
  53. if (module.nonCsrfRouter != null) {
  54. module.nonCsrfRouter.apply(
  55. webRouter,
  56. privateApiRouter,
  57. publicApiRouter
  58. )
  59. }
  60. result.push(
  61. __guardMethod__(module.router, 'applyNonCsrfRouter', o =>
  62. o.applyNonCsrfRouter(webRouter, privateApiRouter, publicApiRouter)
  63. )
  64. )
  65. }
  66. return result
  67. })()
  68. },
  69. viewIncludes: {},
  70. loadViewIncludes(app) {
  71. this.viewIncludes = {}
  72. return Array.from(this.modules).map(module =>
  73. (() => {
  74. const result = []
  75. const object = module.viewIncludes || {}
  76. for (let view in object) {
  77. const partial = object[view]
  78. if (!this.viewIncludes[view]) {
  79. this.viewIncludes[view] = []
  80. }
  81. const filePath = Path.join(
  82. MODULE_BASE_PATH,
  83. module.name,
  84. 'app/views',
  85. partial + '.pug'
  86. )
  87. result.push(
  88. this.viewIncludes[view].push(
  89. pug.compileFile(filePath, { doctype: 'html' })
  90. )
  91. )
  92. }
  93. return result
  94. })()
  95. )
  96. },
  97. moduleIncludes(view, locals) {
  98. const compiledPartials = Modules.viewIncludes[view] || []
  99. let html = ''
  100. for (let compiledPartial of Array.from(compiledPartials)) {
  101. const d = new Date()
  102. html += compiledPartial(locals)
  103. }
  104. return html
  105. },
  106. moduleIncludesAvailable(view) {
  107. return (Modules.viewIncludes[view] || []).length > 0
  108. },
  109. linkedFileAgentsIncludes() {
  110. const agents = {}
  111. for (let module of Array.from(this.modules)) {
  112. for (let name in module.linkedFileAgents) {
  113. const agentFunction = module.linkedFileAgents[name]
  114. agents[name] = agentFunction()
  115. }
  116. }
  117. return agents
  118. },
  119. attachHooks() {
  120. return (() => {
  121. const result = []
  122. for (var module of Array.from(this.modules)) {
  123. if (module.hooks != null) {
  124. result.push(
  125. (() => {
  126. const result1 = []
  127. for (let hook in module.hooks) {
  128. const method = module.hooks[hook]
  129. result1.push(Modules.hooks.attach(hook, method))
  130. }
  131. return result1
  132. })()
  133. )
  134. } else {
  135. result.push(undefined)
  136. }
  137. }
  138. return result
  139. })()
  140. },
  141. hooks: {
  142. _hooks: {},
  143. attach(name, method) {
  144. if (this._hooks[name] == null) {
  145. this._hooks[name] = []
  146. }
  147. return this._hooks[name].push(method)
  148. },
  149. fire(name, ...rest) {
  150. const adjustedLength = Math.max(rest.length, 1),
  151. args = rest.slice(0, adjustedLength - 1),
  152. callback = rest[adjustedLength - 1]
  153. const methods = this._hooks[name] || []
  154. const call_methods = methods.map(method => cb =>
  155. method(...Array.from(args), cb)
  156. )
  157. return async.series(call_methods, function(error, results) {
  158. if (error != null) {
  159. return callback(error)
  160. }
  161. return callback(null, results)
  162. })
  163. }
  164. }
  165. }
  166. Modules.loadModules()
  167. function __guardMethod__(obj, methodName, transform) {
  168. if (
  169. typeof obj !== 'undefined' &&
  170. obj !== null &&
  171. typeof obj[methodName] === 'function'
  172. ) {
  173. return transform(obj, methodName)
  174. } else {
  175. return undefined
  176. }
  177. }