3
0

indexize.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. let fs = require('fs');
  2. let path = require('path');
  3. var reactDocs = require('react-docgen');
  4. // let TEXT = fs.readFileSync('../../node_modules/native-base/src/basic/Button.js');
  5. // var componentInfo = reactDocs.parse(TEXT);
  6. // console.log("NativeBaseCI", componentInfo);
  7. //joining path of directory
  8. const directoryPath = "../../node_modules/native-base/src/basic/";
  9. //passsing directoryPath and callback function
  10. fs.readdir(directoryPath, function (err, files) {
  11. //handling error
  12. if (err) {
  13. return console.log('Unable to scan directory: ' + err);
  14. }
  15. let Infos = [];
  16. //listing all files using forEach
  17. files.forEach(function (file, index) {
  18. // Do whatever you want to do with the file
  19. if(!file.endsWith('.js') || file.includes('.android') || file.includes('.ios')) return;
  20. console.log("No. " + index +" : ", file);
  21. let content = fs.readFileSync(directoryPath + file);
  22. try{
  23. let info = reactDocs.parse(content);
  24. info.componentName = file.replace('.js', '');
  25. Infos.push(info);
  26. } catch(e) {
  27. console.log("Error");
  28. console.log(e);
  29. }
  30. });
  31. let fileText = generateFile(Infos);
  32. fs.writeFileSync('./index.js', fileText);
  33. });
  34. function generateFile(Infos) {
  35. let headers = `
  36. import React from 'react';
  37. import Types from '../../lib/Types';
  38. import Module from '../../lib/Module';\n`;
  39. let IM = [],
  40. EX = [],
  41. PROPS = [],
  42. Names = [];
  43. Infos.forEach((info) => {
  44. let { im, ex, props} = createClass(info);
  45. Names.push(info.componentName);
  46. IM.push(im);
  47. EX.push(ex);
  48. PROPS.push(props);
  49. })
  50. return headers + "\n\n" +
  51. IM.join('\n') + "\n\n" +
  52. EX.join('\n') + "\n\n" +
  53. PROPS.join('\n')+"\n\n" +
  54. `export default { \n\t${Names.join(',\n\t')} \n}`;
  55. }
  56. function createClass(item) {
  57. let name = item.componentName;
  58. let props = generatePropsText(item.props);
  59. let s = `export class ${name} extends Module {
  60. constructor(props) {
  61. super(props);
  62. }
  63. display() {
  64. return <${name+'Ctor'} {...this.props} />
  65. }
  66. }`;
  67. let propsText = `${name}.Inputs = ${props}`;
  68. return {
  69. im: `import { ${name} as ${name+'Ctor'} } from 'native-base';`,
  70. ex: s,
  71. props: propsText
  72. }
  73. }
  74. function generatePropsText(props) {
  75. let Props = [];
  76. for(var name in props) {
  77. let p = props[name];
  78. let validator;
  79. switch(p.type && p.type.name) {
  80. case 'number':
  81. validator = "new Types.Real()"
  82. break;
  83. case 'string':
  84. validator = "new Types.Text()"
  85. break;
  86. case 'bool':
  87. validator = "new Types.Bool()"
  88. break;
  89. default:
  90. console.log("Style type for ", name , p.type);
  91. continue;
  92. break;
  93. }
  94. if(p.required) validator += ".require()"
  95. if(p.defaultValue) validator += `.default(${p.defaultValue.value})`
  96. let s = `${name}: ${validator}`;
  97. Props.push(s);
  98. }
  99. return `{
  100. ${Props.join(',\n\t')}
  101. }`;
  102. }