Skip to content

Instantly share code, notes, and snippets.

@praveenperera
Forked from rusty-key/parser.js
Created September 11, 2018 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save praveenperera/5fbe923cf05483c83c625e66f21fbb4d to your computer and use it in GitHub Desktop.
Save praveenperera/5fbe923cf05483c83c625e66f21fbb4d to your computer and use it in GitHub Desktop.
PropTypes → BS
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