Created
September 25, 2018 23:03
-
-
Save jpenney1/dee9dc27e65bf7bdef0d0553183e03ed to your computer and use it in GitHub Desktop.
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
var ARGS = process.argv.slice(2), | |
COMPONENT_NAME = ARGS[0], | |
COMPONENT_IS_SYSTEM = ARGS[1] == 'true', | |
JSX_SUUBPATH = (COMPONENT_IS_SYSTEM) ? 'systems' : 'components', | |
COMPONENT_DIR = `./${JSX_SUUBPATH}/${COMPONENT_NAME}`, | |
fs = require('fs') | |
if(!COMPONENT_NAME) { | |
return console.log(` | |
------------------------------------------------------------------------------ | |
| | |
| To generate a react component you must supply a component name: | |
| | |
| "node */generate-react-component.js SuperCoolInput" | |
| | |
| To generate a react view just append a second argument: | |
| | |
| "node */generate-react-component.js SuperCoolInput true" | |
| | |
------------------------------------------------------------------------------- | |
`) | |
} | |
if( COMPONENT_NAME[0] != COMPONENT_NAME[0].toUpperCase() ) return console.log('Must start with an upper case letter') | |
if( !COMPONENT_NAME[0].match(/[a-z]/i) ) return console.log('Must start with A-Z') | |
const write = (path, string) => new Promise((resolve, reject) => fs.writeFile(path, string, resolve)) | |
var jsx = ` | |
/** | |
* @class ${COMPONENT_NAME} | |
* @extends React.Component | |
* @description | |
*/ | |
const React = require('react') | |
const style = require('./style.js') | |
class ${COMPONENT_NAME} extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = {} | |
} | |
componentDidMount () { | |
} | |
render() { | |
return ( | |
<div className="${COMPONENT_NAME}" style={style}> | |
</div> | |
) | |
} | |
} | |
module.exports = ${COMPONENT_NAME} | |
` | |
var styleJson = ` | |
/** | |
* Styles for ${COMPONENT_NAME} | |
*/ | |
module.exports = { | |
// styles here | |
} | |
` | |
fs.readFile(COMPONENT_DIR, err => { | |
if(err) { | |
fs.mkdir(COMPONENT_DIR, err => { | |
fs.mkdir(`${COMPONENT_DIR}/style`, err => { | |
Promise | |
.all([ | |
write(`${COMPONENT_DIR}/${COMPONENT_NAME}.jsx`, jsx), | |
write(`${COMPONENT_DIR}/style.js`, styleJson) | |
]) | |
.then(() => console.log(`Success: ${COMPONENT_DIR}/${COMPONENT_NAME} initialized!`)) | |
}) | |
}) | |
} else { | |
console.log(`ERROR: Something went wrong while generating ${COMPONENT_NAME}. Ensure that the component does not exist and try again.`) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment