createAttributes.cjs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 cfg = require('./../src/config.json');
  8. const TYPE_IDENTIFY_OPEN = 'tbody_open';
  9. const TYPE_IDENTIFY_CLOSE = 'tbody_close';
  10. const TR_TYPE_IDENTIFY_OPEN = 'tr_open';
  11. const TR_TYPE_IDENTIFY_CLOSE = 'tr_close';
  12. const argv = process.argv[2];
  13. let packages = [];
  14. const kebabCase = (str) => {
  15. str = str.replace(str.charAt(0), str.charAt(0).toLocaleLowerCase());
  16. return str.replace(/([a-z])([A-Z])/g, (_, p1, p2) => p1 + '-' + p2.toLowerCase());
  17. };
  18. const getCompName = (name) => {
  19. if(!packages.length) {
  20. cfg.nav.forEach((item, index) => {
  21. packages = packages.concat(item.packages);
  22. });
  23. }
  24. const packageName = packages.find((item) => item.name.toLowerCase() === name.toLowerCase());
  25. return packageName.name
  26. }
  27. const getSubSources = (sources) => {
  28. let sourcesMap = [];
  29. const startIndex = sources.findIndex((source) => source.type === TYPE_IDENTIFY_OPEN);
  30. const endIndex = sources.findIndex((source) => source.type === TYPE_IDENTIFY_CLOSE);
  31. sources = sources.slice(startIndex, endIndex + 1);
  32. while (sources.filter((source) => source.type === TR_TYPE_IDENTIFY_OPEN).length) {
  33. let trStartIndex = sources.findIndex((source) => source.type === TR_TYPE_IDENTIFY_OPEN);
  34. let trEndIndex = sources.findIndex((source) => source.type === TR_TYPE_IDENTIFY_CLOSE);
  35. sourcesMap.push(sources.slice(trStartIndex, trEndIndex + 1));
  36. sources.splice(trStartIndex, trEndIndex - trStartIndex + 1);
  37. }
  38. return sourcesMap;
  39. };
  40. const genaratorWebTypes = () => {
  41. let typesData = {
  42. $schema: 'https://raw.githubusercontent.com/JetBrains/web-types/master/schema/web-types.json',
  43. framework: 'vue',
  44. name: 'NutUI',
  45. version: config.version,
  46. contributions: {
  47. html: {
  48. tags: [],
  49. attributes: [],
  50. 'types-syntax': 'typescript'
  51. }
  52. }
  53. };
  54. if (!componentDirs.length) return;
  55. for (let componentDir of componentDirs) {
  56. let stat = fs.lstatSync(`${basePath}/${componentDir}`);
  57. if (stat.isDirectory()) {
  58. let absolutePath = path.join(`${basePath}/${componentDir}`, `doc.md`);
  59. if(argv === 'taro') {
  60. absolutePath = path.join(`${basePath}/${componentDir}`, `doc.taro.md`);
  61. }
  62. let attributes = [];
  63. if (!fs.existsSync(absolutePath)) continue;
  64. const data = fs.readFileSync(absolutePath, 'utf8');
  65. let sources = MarkdownIt.parse(data, {});
  66. let sourcesMap = getSubSources(sources);
  67. for (let sourceMap of sourcesMap) {
  68. const inlineItem = sourceMap.filter((source) => source.type === 'inline').length
  69. ? sourceMap.filter((source) => source.type === 'inline')
  70. : [];
  71. const propItem = inlineItem.length ? `${inlineItem[0].content.replace(/`.*?`/g, '')}` : '';
  72. const infoItem = inlineItem.length ? `${inlineItem[1].content}` : '';
  73. const typeItem = inlineItem.length ? `${inlineItem[2].content.toLowerCase()}` : '';
  74. const defaultItem = inlineItem.length ? `${inlineItem[3].content}` : '';
  75. attributes.push({
  76. name: propItem,
  77. default: defaultItem,
  78. description: infoItem,
  79. value: {
  80. type: typeItem,
  81. kind: 'expression'
  82. }
  83. });
  84. }
  85. let compoName = kebabCase(getCompName(componentDir));
  86. typesData.contributions.html.tags.push({
  87. name: `nut-${compoName}`,
  88. slots: [],
  89. events: [],
  90. attributes: attributes.slice()
  91. });
  92. }
  93. }
  94. return typesData;
  95. };
  96. const writeWebTypes = () => {
  97. const typesData = genaratorWebTypes();
  98. let innerText = `${JSON.stringify(typesData, null, 2)}`;
  99. const distPath = path.resolve(__dirname, './../dist');
  100. const componentWebTypespPath = path.resolve(__dirname, './../dist/smartips/web-types.json');
  101. if (!fs.existsSync(path.join(distPath + '/smartips'))) {
  102. fs.mkdirSync(path.join(distPath + '/smartips'));
  103. }
  104. fs.writeFileSync(componentWebTypespPath, innerText);
  105. };
  106. writeWebTypes();