2014-09-08 13:23:47 +00:00
|
|
|
fs = require "fs"
|
|
|
|
Path = require "path"
|
2017-01-20 12:03:02 +00:00
|
|
|
pug = require "pug"
|
2015-10-08 17:17:53 +00:00
|
|
|
async = require "async"
|
2014-09-08 13:23:47 +00:00
|
|
|
|
|
|
|
MODULE_BASE_PATH = Path.resolve(__dirname + "/../../../modules")
|
|
|
|
|
|
|
|
module.exports = Modules =
|
2014-09-08 14:40:46 +00:00
|
|
|
modules: []
|
|
|
|
loadModules: () ->
|
|
|
|
for moduleName in fs.readdirSync(MODULE_BASE_PATH)
|
|
|
|
if fs.existsSync(Path.join(MODULE_BASE_PATH, moduleName, "index.js"))
|
|
|
|
loadedModule = require(Path.join(MODULE_BASE_PATH, moduleName, "index"))
|
|
|
|
loadedModule.name = moduleName
|
|
|
|
@modules.push loadedModule
|
2015-10-08 17:17:53 +00:00
|
|
|
Modules.attachHooks()
|
2014-09-08 14:40:46 +00:00
|
|
|
|
2017-07-05 13:41:14 +00:00
|
|
|
applyRouter: (webRouter, privateApiRouter, publicApiRouter) ->
|
2014-09-08 14:40:46 +00:00
|
|
|
for module in @modules
|
2017-07-05 13:41:14 +00:00
|
|
|
module.router?.apply?(webRouter, privateApiRouter, publicApiRouter)
|
2016-11-11 13:48:29 +00:00
|
|
|
|
2017-07-05 13:41:14 +00:00
|
|
|
applyNonCsrfRouter: (webRouter, privateApiRouter, publicApiRouter) ->
|
2016-11-11 13:48:29 +00:00
|
|
|
for module in @modules
|
2017-07-05 13:41:14 +00:00
|
|
|
module.nonCsrfRouter?.apply(webRouter, privateApiRouter, publicApiRouter)
|
|
|
|
module.router?.applyNonCsrfRouter?(webRouter, privateApiRouter, publicApiRouter)
|
2018-06-26 09:15:45 +00:00
|
|
|
|
2014-09-08 14:40:46 +00:00
|
|
|
viewIncludes: {}
|
|
|
|
loadViewIncludes: (app) ->
|
|
|
|
@viewIncludes = {}
|
|
|
|
for module in @modules
|
|
|
|
for view, partial of module.viewIncludes or {}
|
|
|
|
@viewIncludes[view] ||= []
|
2018-05-23 21:02:02 +00:00
|
|
|
filePath = Path.join(MODULE_BASE_PATH, module.name, "app/views", partial + ".pug")
|
|
|
|
@viewIncludes[view].push pug.compileFile(filePath, doctype: "html")
|
2018-06-26 09:15:45 +00:00
|
|
|
|
2014-09-08 14:40:46 +00:00
|
|
|
moduleIncludes: (view, locals) ->
|
2016-08-23 14:31:09 +00:00
|
|
|
compiledPartials = Modules.viewIncludes[view] or []
|
2014-09-08 14:40:46 +00:00
|
|
|
html = ""
|
2016-08-23 14:31:09 +00:00
|
|
|
for compiledPartial in compiledPartials
|
|
|
|
d = new Date()
|
|
|
|
html += compiledPartial(locals)
|
2014-09-08 14:40:46 +00:00
|
|
|
return html
|
2015-02-05 18:20:25 +00:00
|
|
|
|
|
|
|
moduleIncludesAvailable: (view) ->
|
|
|
|
return (Modules.viewIncludes[view] or []).length > 0
|
2018-06-26 09:15:45 +00:00
|
|
|
|
2018-04-11 11:36:47 +00:00
|
|
|
moduleAssetFiles: (pathPrefix) ->
|
|
|
|
assetFiles = []
|
|
|
|
for module in @modules
|
|
|
|
for assetFile in module.assetFiles or []
|
|
|
|
assetFiles.push "#{pathPrefix}#{assetFile}"
|
|
|
|
return assetFiles
|
|
|
|
|
2018-06-26 09:15:45 +00:00
|
|
|
linkedFileAgentsIncludes: () ->
|
|
|
|
agents = {}
|
|
|
|
for module in @modules
|
|
|
|
for name, agentFunction of module.linkedFileAgents
|
|
|
|
agents[name] = agentFunction()
|
|
|
|
return agents
|
|
|
|
|
2015-10-08 17:17:53 +00:00
|
|
|
attachHooks: () ->
|
|
|
|
for module in @modules
|
|
|
|
if module.hooks?
|
|
|
|
for hook, method of module.hooks
|
|
|
|
Modules.hooks.attach hook, method
|
2018-06-26 09:15:45 +00:00
|
|
|
|
2015-10-08 17:17:53 +00:00
|
|
|
hooks:
|
|
|
|
_hooks: {}
|
|
|
|
attach: (name, method) ->
|
|
|
|
@_hooks[name] ?= []
|
|
|
|
@_hooks[name].push method
|
2018-06-26 09:15:45 +00:00
|
|
|
|
2015-10-08 17:17:53 +00:00
|
|
|
fire: (name, args..., callback) ->
|
|
|
|
methods = @_hooks[name] or []
|
|
|
|
call_methods = methods.map (method) ->
|
|
|
|
return (cb) -> method(args..., cb)
|
|
|
|
async.series call_methods, (error, results) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
return callback null, results
|
2018-06-26 09:15:45 +00:00
|
|
|
|
2016-11-11 13:48:29 +00:00
|
|
|
Modules.loadModules()
|