renderers.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const Model = require('objection').Model
  2. const path = require('path')
  3. const fs = require('fs-extra')
  4. const _ = require('lodash')
  5. const yaml = require('js-yaml')
  6. const DepGraph = require('dependency-graph').DepGraph
  7. const commonHelper = require('../helpers/common')
  8. /* global WIKI */
  9. /**
  10. * Renderer model
  11. */
  12. module.exports = class Renderer extends Model {
  13. static get tableName() { return 'renderers' }
  14. static get jsonSchema () {
  15. return {
  16. type: 'object',
  17. required: ['module', 'isEnabled'],
  18. properties: {
  19. id: {type: 'string'},
  20. module: {type: 'string'},
  21. isEnabled: {type: 'boolean'}
  22. }
  23. }
  24. }
  25. static get jsonAttributes() {
  26. return ['config']
  27. }
  28. static async getRenderers() {
  29. return WIKI.models.renderers.query()
  30. }
  31. static async fetchDefinitions() {
  32. try {
  33. // -> Fetch definitions from disk
  34. const renderersDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/rendering'))
  35. WIKI.data.renderers = []
  36. for (const dir of renderersDirs) {
  37. const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/rendering', dir, 'definition.yml'), 'utf8')
  38. const defParsed = yaml.load(def)
  39. defParsed.key = dir
  40. defParsed.props = commonHelper.parseModuleProps(defParsed.props)
  41. WIKI.data.renderers.push(defParsed)
  42. WIKI.logger.debug(`Loaded renderers module definition ${dir}: [ OK ]`)
  43. }
  44. WIKI.logger.info(`Loaded ${WIKI.data.renderers.length} renderers module definitions: [ OK ]`)
  45. } catch (err) {
  46. WIKI.logger.error(`Failed to scan or load renderers providers: [ FAILED ]`)
  47. WIKI.logger.error(err)
  48. }
  49. }
  50. static async refreshRenderersFromDisk() {
  51. // const dbRenderers = await WIKI.models.renderers.query()
  52. // -> Fetch definitions from disk
  53. await WIKI.models.renderers.fetchDefinitions()
  54. // TODO: Merge existing configs with updated modules
  55. }
  56. static async getRenderingPipeline(contentType) {
  57. const renderersDb = await WIKI.models.renderers.query().where('isEnabled', true)
  58. if (renderersDb && renderersDb.length > 0) {
  59. const renderers = renderersDb.map(rdr => {
  60. const renderer = _.find(WIKI.data.renderers, ['key', rdr.key])
  61. return {
  62. ...renderer,
  63. config: rdr.config
  64. }
  65. })
  66. // Build tree
  67. const rawCores = _.filter(renderers, renderer => !_.has(renderer, 'dependsOn')).map(core => {
  68. core.children = _.filter(renderers, ['dependsOn', core.key])
  69. return core
  70. })
  71. // Build dependency graph
  72. const graph = new DepGraph({ circular: true })
  73. rawCores.map(core => { graph.addNode(core.key) })
  74. rawCores.map(core => {
  75. rawCores.map(coreTarget => {
  76. if (core.key !== coreTarget.key) {
  77. if (core.output === coreTarget.input) {
  78. graph.addDependency(core.key, coreTarget.key)
  79. }
  80. }
  81. })
  82. })
  83. // Filter unused cores
  84. let activeCoreKeys = _.filter(rawCores, ['input', contentType]).map(core => core.key)
  85. _.clone(activeCoreKeys).map(coreKey => {
  86. activeCoreKeys = _.union(activeCoreKeys, graph.dependenciesOf(coreKey))
  87. })
  88. const activeCores = _.filter(rawCores, core => _.includes(activeCoreKeys, core.key))
  89. // Rebuild dependency graph with active cores
  90. const graphActive = new DepGraph({ circular: true })
  91. activeCores.map(core => { graphActive.addNode(core.key) })
  92. activeCores.map(core => {
  93. activeCores.map(coreTarget => {
  94. if (core.key !== coreTarget.key) {
  95. if (core.output === coreTarget.input) {
  96. graphActive.addDependency(core.key, coreTarget.key)
  97. }
  98. }
  99. })
  100. })
  101. // Reorder cores in reverse dependency order
  102. let orderedCores = []
  103. _.reverse(graphActive.overallOrder()).map(coreKey => {
  104. orderedCores.push(_.find(rawCores, ['key', coreKey]))
  105. })
  106. return orderedCores
  107. } else {
  108. WIKI.logger.error(`Rendering pipeline is empty!`)
  109. return false
  110. }
  111. }
  112. }