Created
September 10, 2018 13:59
-
-
Save rusty-key/f129f9405790f0c69656530a16bd5f09 to your computer and use it in GitHub Desktop.
PropTypes → BS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const path = require('path') | |
const definedTypes = { | |
'PropTypes.string': 'string', | |
'PropTypes.bool': 'bool', | |
} | |
const files = fs.readdirSync('src') | |
.filter(file => file[0] === file[0].toUpperCase() && file[0] !== '_') | |
files.forEach(fileName => { | |
const file = fs.readFileSync(path.join('src', fileName), 'utf-8') | |
const componentName = fileName.replace('.js', '') | |
const lowerCasedComponentName = componentName.charAt(0).toLowerCase() + componentName.slice(1) | |
const match = file.match(/const propTypes = \{(.*\n)*?\}/) | |
if (match) { | |
const pt = match[0].replace(/(.+)\:( *)(.+)(,*)/g, '$1: `$3`,') | |
eval(pt.replace('const', 'var')) | |
let makePropsString = '' | |
let argumentsString = '' | |
let propsString = '' | |
let letter = 'a' | |
for (let key in propTypes) { | |
if (key === 'children') continue | |
const isRequired = propTypes[key].indexOf('isRequired') > -1 | |
const value = propTypes[key].replace('.isRequired', '').replace(',', '') | |
let type = definedTypes[value] | |
if (!type) { | |
type = `'${letter}` | |
letter = String.fromCharCode(letter.charCodeAt(0) + 1) | |
} | |
makePropsString += ` ~${key}: ${type}${isRequired ? '' : '=?'},\n` | |
argumentsString += ` ~${key}${isRequired ? '' : '=?'},\n` | |
propsString += ` ~${key}${isRequired ? '' : '?'},\n` | |
} | |
makePropsString = makePropsString.slice(0, -1) | |
argumentsString = argumentsString.slice(0, -1) | |
propsString = propsString.slice(0, -1) | |
const binding = `[@bs.module "reactstrap"] external ${lowerCasedComponentName} : ReasonReact.reactClass = "${componentName}"; | |
[@bs.obj] | |
external makeProps : ( | |
${makePropsString} | |
unit | |
) => _ = ""; | |
let make = ( | |
${argumentsString} | |
children | |
) => | |
ReasonReact.wrapJsForReason( | |
~reactClass=${lowerCasedComponentName}, | |
~props=makeProps( | |
${propsString} | |
() | |
), | |
children | |
); | |
` | |
fs.writeFileSync(path.join('re', `${componentName}.re`), binding, 'utf-8') | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment