createAttributes.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const path = require('path');
  2. const fs = require('fs');
  3. const MarkdownIt = require('markdown-it')();
  4. const basePath = path.resolve(__dirname, './../src/packages/__VUE');
  5. const componentDirs = fs.readdirSync(basePath, 'utf8');
  6. const TYPE_IDENTIFY_OPEN = 'tbody_open';
  7. const TYPE_IDENTIFY_CLOSE = 'tbody_close';
  8. const TR_TYPE_IDENTIFY_OPEN = 'tr_open';
  9. const TR_TYPE_IDENTIFY_CLOSE = 'tr_close';
  10. const getSubSources = (sources) => {
  11. let sourcesMap = [];
  12. const startIndex = sources.findIndex((source) => source.type === TYPE_IDENTIFY_OPEN);
  13. const endIndex = sources.findIndex((source) => source.type === TYPE_IDENTIFY_CLOSE);
  14. sources = sources.slice(startIndex, endIndex + 1);
  15. while (sources.filter((source) => source.type === TR_TYPE_IDENTIFY_OPEN).length) {
  16. let trStartIndex = sources.findIndex((source) => source.type === TR_TYPE_IDENTIFY_OPEN);
  17. let trEndIndex = sources.findIndex((source) => source.type === TR_TYPE_IDENTIFY_CLOSE);
  18. sourcesMap.push(sources.slice(trStartIndex, trEndIndex + 1));
  19. sources.splice(trStartIndex, trEndIndex - trStartIndex + 1);
  20. }
  21. return sourcesMap;
  22. };
  23. const genaratorTags = () => {
  24. let componentTags = {};
  25. if (!componentDirs.length) return;
  26. for (let componentDir of componentDirs) {
  27. let stat = fs.lstatSync(`${basePath}/${componentDir}`);
  28. if (stat.isDirectory()) {
  29. const absolutePath = path.join(`${basePath}/${componentDir}`, 'doc.md');
  30. if (!fs.existsSync(absolutePath)) continue;
  31. const data = fs.readFileSync(absolutePath, 'utf8');
  32. let sources = MarkdownIt.parse(data, {});
  33. let sourcesMap = getSubSources(sources);
  34. componentTags[`nut-${componentDir}`] = { attributes: [] };
  35. for (let sourceMap of sourcesMap) {
  36. let propItem = sourceMap.filter((source) => source.type === 'inline').length
  37. ? `${sourceMap.filter((source) => source.type === 'inline')[0].content}`
  38. : '';
  39. componentTags[`nut-${componentDir}`]['attributes'].push(propItem);
  40. }
  41. }
  42. }
  43. return componentTags;
  44. };
  45. const genaratorAttributes = () => {
  46. let componentTags = {};
  47. if (!componentDirs.length) return;
  48. for (let componentDir of componentDirs) {
  49. let stat = fs.lstatSync(`${basePath}/${componentDir}`);
  50. if (stat.isDirectory()) {
  51. const absolutePath = path.join(`${basePath}/${componentDir}`, 'doc.md');
  52. if (!fs.existsSync(absolutePath)) continue;
  53. const data = fs.readFileSync(absolutePath, 'utf8');
  54. let sources = MarkdownIt.parse(data, {});
  55. let sourcesMap = getSubSources(sources);
  56. for (let sourceMap of sourcesMap) {
  57. const inlineItem = sourceMap.filter((source) => source.type === 'inline').length
  58. ? sourceMap.filter((source) => source.type === 'inline')
  59. : [];
  60. const propItem = inlineItem.length ? `${inlineItem[0].content}` : '';
  61. const infoItem = inlineItem.length ? `${inlineItem[1].content}` : '';
  62. const typeItem = inlineItem.length ? `${inlineItem[2].content.toLowerCase()}` : '';
  63. const defaultItem = inlineItem.length ? `${inlineItem[3].content}` : '';
  64. componentTags[`nut-${componentDir}/${propItem}`] = {
  65. type: `${typeItem}`,
  66. description: `属性说明:${infoItem},默认值:${defaultItem}`
  67. };
  68. }
  69. }
  70. }
  71. return componentTags;
  72. };
  73. const writeTags = () => {
  74. const componentTags = genaratorTags();
  75. let innerText = `${JSON.stringify(componentTags, null, 2)}`;
  76. const distPath = path.resolve(__dirname, './../dist');
  77. const componentTagsPath = path.resolve(__dirname, './../dist/smartips/tags.json');
  78. if (!fs.existsSync(path.join(distPath + '/smartips'))) {
  79. fs.mkdirSync(path.join(distPath + '/smartips'));
  80. }
  81. fs.writeFileSync(componentTagsPath, innerText);
  82. };
  83. const writeAttributes = () => {
  84. const componentAttributes = genaratorAttributes();
  85. let innerText = `${JSON.stringify(componentAttributes, null, 2)}`;
  86. const distPath = path.resolve(__dirname, './../dist');
  87. const componentAttributespPath = path.resolve(__dirname, './../dist/smartips/attributes.json');
  88. if (!fs.existsSync(path.join(distPath + '/smartips'))) {
  89. fs.mkdirSync(path.join(distPath + '/smartips'));
  90. }
  91. fs.writeFileSync(componentAttributespPath, innerText);
  92. };
  93. writeTags();
  94. writeAttributes();