123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- let fs = require('fs');
- let path = require('path');
- var reactDocs = require('react-docgen');
- // let TEXT = fs.readFileSync('../../node_modules/native-base/src/basic/Button.js');
- // var componentInfo = reactDocs.parse(TEXT);
- // console.log("NativeBaseCI", componentInfo);
- //joining path of directory
- const directoryPath = "../../node_modules/native-base/src/basic/";
- //passsing directoryPath and callback function
- fs.readdir(directoryPath, function (err, files) {
- //handling error
- if (err) {
- return console.log('Unable to scan directory: ' + err);
- }
-
- let Infos = [];
- //listing all files using forEach
- files.forEach(function (file, index) {
- // Do whatever you want to do with the file
- if(!file.endsWith('.js') || file.includes('.android') || file.includes('.ios')) return;
- console.log("No. " + index +" : ", file);
-
- let content = fs.readFileSync(directoryPath + file);
- try{
- let info = reactDocs.parse(content);
- info.componentName = file.replace('.js', '');
- Infos.push(info);
- } catch(e) {
- console.log("Error");
- console.log(e);
- }
- });
- let fileText = generateFile(Infos);
- fs.writeFileSync('./index.js', fileText);
- });
- function generateFile(Infos) {
- let headers = `
- import React from 'react';
- import Types from '../../lib/Types';
- import Module from '../../lib/Module';\n`;
- let IM = [],
- EX = [],
- PROPS = [],
- Names = [];
- Infos.forEach((info) => {
- let { im, ex, props} = createClass(info);
- Names.push(info.componentName);
- IM.push(im);
- EX.push(ex);
- PROPS.push(props);
- })
- return headers + "\n\n" +
- IM.join('\n') + "\n\n" +
- EX.join('\n') + "\n\n" +
- PROPS.join('\n')+"\n\n" +
- `export default { \n\t${Names.join(',\n\t')} \n}`;
- }
- function createClass(item) {
- let name = item.componentName;
- let props = generatePropsText(item.props);
- let s = `export class ${name} extends Module {
- constructor(props) {
- super(props);
- }
- display() {
- return <${name+'Ctor'} {...this.props} />
- }
- }`;
- let propsText = `${name}.Inputs = ${props}`;
- return {
- im: `import { ${name} as ${name+'Ctor'} } from 'native-base';`,
- ex: s,
- props: propsText
- }
- }
- function generatePropsText(props) {
- let Props = [];
- for(var name in props) {
- let p = props[name];
- let validator;
- switch(p.type && p.type.name) {
- case 'number':
- validator = "new Types.Real()"
- break;
- case 'string':
- validator = "new Types.Text()"
- break;
- case 'bool':
- validator = "new Types.Bool()"
- break;
- default:
-
- console.log("Style type for ", name , p.type);
- continue;
- break;
- }
- if(p.required) validator += ".require()"
- if(p.defaultValue) validator += `.default(${p.defaultValue.value})`
- let s = `${name}: ${validator}`;
- Props.push(s);
- }
- return `{
- ${Props.join(',\n\t')}
- }`;
- }
|