renderers.js 4.0 KB

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