createAttributes.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 config = require('./../package.json');
  7. const TYPE_IDENTIFY_OPEN = 'tbody_open';
  8. const TYPE_IDENTIFY_CLOSE = 'tbody_close';
  9. const TR_TYPE_IDENTIFY_OPEN = 'tr_open';
  10. const TR_TYPE_IDENTIFY_CLOSE = 'tr_close';
  11. const getSubSources = (sources) => {
  12. let sourcesMap = [];
  13. const startIndex = sources.findIndex((source) => source.type === TYPE_IDENTIFY_OPEN);
  14. const endIndex = sources.findIndex((source) => source.type === TYPE_IDENTIFY_CLOSE);
  15. sources = sources.slice(startIndex, endIndex + 1);
  16. while (sources.filter((source) => source.type === TR_TYPE_IDENTIFY_OPEN).length) {
  17. let trStartIndex = sources.findIndex((source) => source.type === TR_TYPE_IDENTIFY_OPEN);
  18. let trEndIndex = sources.findIndex((source) => source.type === TR_TYPE_IDENTIFY_CLOSE);
  19. sourcesMap.push(sources.slice(trStartIndex, trEndIndex + 1));
  20. sources.splice(trStartIndex, trEndIndex - trStartIndex + 1);
  21. }
  22. return sourcesMap;
  23. };
  24. const genaratorTags = () => {
  25. let componentTags = {};
  26. if (!componentDirs.length) return;
  27. for (let componentDir of componentDirs) {
  28. let stat = fs.lstatSync(`${basePath}/${componentDir}`);
  29. if (stat.isDirectory()) {
  30. const absolutePath = path.join(`${basePath}/${componentDir}`, 'doc.md');
  31. if (!fs.existsSync(absolutePath)) continue;
  32. const data = fs.readFileSync(absolutePath, 'utf8');
  33. let sources = MarkdownIt.parse(data, {});
  34. let sourcesMap = getSubSources(sources);
  35. componentTags[`nut-${componentDir}`] = { attributes: [] };
  36. for (let sourceMap of sourcesMap) {
  37. let propItem = sourceMap.filter((source) => source.type === 'inline').length
  38. ? `${sourceMap.filter((source) => source.type === 'inline')[0].content.replace(/`.*?`/g, '')}`
  39. : '';
  40. componentTags[`nut-${componentDir}`]['attributes'].push(propItem);
  41. }
  42. }
  43. }
  44. return componentTags;
  45. };
  46. const genaratorAttributes = () => {
  47. let componentTags = {};
  48. if (!componentDirs.length) return;
  49. for (let componentDir of componentDirs) {
  50. let stat = fs.lstatSync(`${basePath}/${componentDir}`);
  51. if (stat.isDirectory()) {
  52. const absolutePath = path.join(`${basePath}/${componentDir}`, 'doc.md');
  53. if (!fs.existsSync(absolutePath)) continue;
  54. const data = fs.readFileSync(absolutePath, 'utf8');
  55. let sources = MarkdownIt.parse(data, {});
  56. let sourcesMap = getSubSources(sources);
  57. for (let sourceMap of sourcesMap) {
  58. const inlineItem = sourceMap.filter((source) => source.type === 'inline').length
  59. ? sourceMap.filter((source) => source.type === 'inline')
  60. : [];
  61. const propItem = inlineItem.length ? `${inlineItem[0].content.replace(/`.*?`/g, '')}` : '';
  62. const infoItem = inlineItem.length ? `${inlineItem[1].content}` : '';
  63. const typeItem = inlineItem.length ? `${inlineItem[2].content.toLowerCase()}` : '';
  64. const defaultItem = inlineItem.length ? `${inlineItem[3].content}` : '';
  65. componentTags[`nut-${componentDir}/${propItem}`] = {
  66. type: `${typeItem}`,
  67. description: `属性说明:${infoItem},默认值:${defaultItem}`
  68. };
  69. }
  70. }
  71. }
  72. return componentTags;
  73. };
  74. const genaratorWebTypes = () => {
  75. let typesData = {
  76. $schema: 'https://raw.githubusercontent.com/JetBrains/web-types/master/schema/web-types.json',
  77. framework: 'vue',
  78. name: 'NutUI',
  79. version: config.version,
  80. contributions: {
  81. html: {
  82. tags: [],
  83. attributes: [],
  84. 'types-syntax': 'typescript'
  85. }
  86. }
  87. };
  88. if (!componentDirs.length) return;
  89. for (let componentDir of componentDirs) {
  90. let stat = fs.lstatSync(`${basePath}/${componentDir}`);
  91. if (stat.isDirectory()) {
  92. const absolutePath = path.join(`${basePath}/${componentDir}`, 'doc.md');
  93. let attributes = [];
  94. if (!fs.existsSync(absolutePath)) continue;
  95. const data = fs.readFileSync(absolutePath, 'utf8');
  96. let sources = MarkdownIt.parse(data, {});
  97. let sourcesMap = getSubSources(sources);
  98. for (let sourceMap of sourcesMap) {
  99. const inlineItem = sourceMap.filter((source) => source.type === 'inline').length
  100. ? sourceMap.filter((source) => source.type === 'inline')
  101. : [];
  102. const propItem = inlineItem.length ? `${inlineItem[0].content.replace(/`.*?`/g, '')}` : '';
  103. const infoItem = inlineItem.length ? `${inlineItem[1].content}` : '';
  104. const typeItem = inlineItem.length ? `${inlineItem[2].content.toLowerCase()}` : '';
  105. const defaultItem = inlineItem.length ? `${inlineItem[3].content}` : '';
  106. attributes.push({
  107. name: propItem,
  108. default: defaultItem,
  109. description: infoItem,
  110. value: {
  111. type: typeItem,
  112. kind: 'expression'
  113. }
  114. });
  115. }
  116. typesData.contributions.html.tags.push({
  117. name: `nut-${componentDir}`,
  118. slots: [],
  119. events: [],
  120. attributes: attributes.slice()
  121. });
  122. }
  123. }
  124. return typesData;
  125. };
  126. const writeTags = () => {
  127. const componentTags = genaratorTags();
  128. let innerText = `${JSON.stringify(componentTags, null, 2)}`;
  129. const distPath = path.resolve(__dirname, './../dist');
  130. const componentTagsPath = path.resolve(__dirname, './../dist/smartips/tags.json');
  131. if (!fs.existsSync(path.join(distPath + '/smartips'))) {
  132. fs.mkdirSync(path.join(distPath + '/smartips'));
  133. }
  134. fs.writeFileSync(componentTagsPath, innerText);
  135. };
  136. const writeAttributes = () => {
  137. const componentAttributes = genaratorAttributes();
  138. let innerText = `${JSON.stringify(componentAttributes, null, 2)}`;
  139. const distPath = path.resolve(__dirname, './../dist');
  140. const componentAttributespPath = path.resolve(__dirname, './../dist/smartips/attributes.json');
  141. if (!fs.existsSync(path.join(distPath + '/smartips'))) {
  142. fs.mkdirSync(path.join(distPath + '/smartips'));
  143. }
  144. fs.writeFileSync(componentAttributespPath, innerText);
  145. };
  146. const writeWebTypes = () => {
  147. const typesData = genaratorWebTypes();
  148. let innerText = `${JSON.stringify(typesData, null, 2)}`;
  149. const distPath = path.resolve(__dirname, './../dist');
  150. const componentWebTypespPath = path.resolve(__dirname, './../dist/smartips/web-types.json');
  151. if (!fs.existsSync(path.join(distPath + '/smartips'))) {
  152. fs.mkdirSync(path.join(distPath + '/smartips'));
  153. }
  154. fs.writeFileSync(componentWebTypespPath, innerText);
  155. };
  156. writeTags();
  157. writeAttributes();
  158. writeWebTypes();