index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const fs = require('fs'),
  2. path = require('path')
  3. const { dirTree } = require('./getHierarchy')
  4. const setJson = (obj) => {
  5. // eslint-disable-next-line no-prototype-builtins
  6. if (obj.hasOwnProperty('children')) {
  7. if (obj.children.find((x) => x.name === obj.name && x.file_type === 'vue')) {
  8. const srcPath = path.resolve('src/') + '/'
  9. obj.path = obj.path.replace(srcPath, '')
  10. obj.type = 'widget'
  11. obj.children.forEach((element) => {
  12. if (element.self_type === 'file' && element.file_type === 'json' && obj.name === element.name) {
  13. const rawData = fs.readFileSync(element.path)
  14. const jsonData = JSON.parse(rawData)
  15. obj.info = jsonData
  16. }
  17. if (element.self_type === 'file' && element.file_type === 'vue' && element.name === 'OptionPanel') {
  18. const optionPath = path.resolve('src/') + '/'
  19. obj.optionPanel = element.path.replace(optionPath, '')
  20. obj.optionPanelName = obj.name + 'OptionPanel'
  21. }
  22. })
  23. delete obj.children
  24. return
  25. } else {
  26. obj.type = 'folder'
  27. }
  28. obj.children.forEach((element) => {
  29. if (element.self_type === 'folder') {
  30. setJson(element)
  31. } else {
  32. if (element.file_type === 'json' && obj.name === element.name) {
  33. const rawData = fs.readFileSync(element.path)
  34. const jsonData = JSON.parse(rawData)
  35. obj.info = jsonData
  36. }
  37. if (element.file_type === 'vue' && element.name === 'OptionPanel') {
  38. obj.optionPanel = element.path
  39. }
  40. }
  41. })
  42. }
  43. }
  44. const generateWidgetList = (_dirname) => {
  45. const absolutePath = path.resolve(_dirname)
  46. const dirHierarchy = dirTree(absolutePath)
  47. setJson(dirHierarchy)
  48. const data = JSON.stringify(dirHierarchy, null, 2)
  49. fs.writeFile(`${absolutePath}/widget.json`, data, (err) => {
  50. if (err) throw err
  51. console.info('Data written to file')
  52. generateWidgetListJs(dirHierarchy, absolutePath)
  53. })
  54. return data
  55. }
  56. const generateWidgetListJs = (jsonData, absolutePath) => {
  57. const widgets = []
  58. function extractWidgets (json) {
  59. json.children.forEach(element => {
  60. if (element.type === 'widget') {
  61. widgets.push(element)
  62. } else {
  63. // eslint-disable-next-line no-prototype-builtins
  64. if (element.hasOwnProperty('children')) {
  65. extractWidgets(element)
  66. }
  67. }
  68. })
  69. }
  70. extractWidgets(jsonData)
  71. let widgetsComponentString = ''
  72. widgets.forEach((widget, index) => {
  73. const widgetName = widget.name
  74. const widgetPath = widget.path
  75. // console.log('index', index)
  76. // console.log('widgets.length', widgets.length)
  77. const lastComponent = ((widgets.length - 1) === index)
  78. // console.log('lastComponent', lastComponent)
  79. widgetsComponentString += ' ' + widgetName + ': defineAsyncComponent(() => import(\'src/' + widgetPath + '/' + widgetName + '.vue\'))'
  80. if (!lastComponent) {
  81. widgetsComponentString += ',\n'
  82. } else {
  83. widgetsComponentString += '\n'
  84. }
  85. // console.log('widgetsComponentString', widgetsComponentString)
  86. })
  87. let widgetsOptionPanelString = ''
  88. const widgetsOptionPanel = widgets.filter(widget => widget.optionPanel)
  89. widgetsOptionPanel.forEach((widget, index) => {
  90. const widgetName = widget.name
  91. const widgetPath = widget.path
  92. const hasOptionPanel = !!widget.optionPanel
  93. if (hasOptionPanel) {
  94. const widgetOptionPanelName = widgetName + 'OptionPanel'
  95. const widgetOptionPanelPath = widgetPath + '/' + 'OptionPanel'
  96. const lastComponent = (widgetsOptionPanel.length - 1 === index)
  97. widgetsOptionPanelString += ' ' + widgetOptionPanelName + ': defineAsyncComponent(() => import(\'src/' + widgetOptionPanelPath + '.vue\'))'
  98. if (!lastComponent) {
  99. widgetsOptionPanelString += ',\n'
  100. } else {
  101. widgetsOptionPanelString += '\n'
  102. }
  103. }
  104. })
  105. createComponentJsFile(widgetsComponentString, absolutePath, 'PageBuilderComponents')
  106. createComponentJsFile(widgetsOptionPanelString, absolutePath, 'PageBuilderOptionPanels')
  107. }
  108. function createComponentJsFile (componentsString, absolutePath, fileName) {
  109. let jsString = '' +
  110. 'import { defineAsyncComponent } from \'vue\'\n' +
  111. '\n' +
  112. 'export default {\n'
  113. jsString += componentsString
  114. jsString += '}\n'
  115. fs.writeFile(`${absolutePath}/${fileName}.js`, jsString, (err) => {
  116. if (err) throw err
  117. console.info('Data written to file (' + fileName + ')')
  118. })
  119. }
  120. module.exports.generateWidgetList = generateWidgetList