Created
January 3, 2019 14:48
-
-
Save iamchristough/990da1f078ae3dd00897b9db9d3a6a2b to your computer and use it in GitHub Desktop.
Creates React components
This file contains 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'); | |
/** | |
* Arguments | |
*/ | |
const name = process.argv[2]; | |
const location = process.argv[3]; | |
const needRuntime = process.argv[4] === 'runtime'; | |
/** | |
* Directory Path | |
*/ | |
const dirPath = `src/${location}/${name}`; | |
/** | |
* Creates index.js file | |
*/ | |
const jsPath = path.join(dirPath, 'index.js'); | |
const jsContent = `import ${name} from './${name}'; | |
export default ${name}; | |
`; | |
/** | |
* Creates SCSS file | |
*/ | |
const scssPath = path.join(dirPath, `${name}.scss`); | |
const scssContent = `.${name} { | |
} | |
`; | |
/** | |
* Creates runtime.js file | |
*/ | |
const runtimePath = path.join(dirPath, '_runtime.js'); | |
const runtimeContent = `export default { | |
// doSth() { | |
// console.log('aaaaa'); | |
// }, | |
}; | |
`; | |
/** | |
* Creates JSX file | |
*/ | |
const jsxPath = path.join(dirPath, `${name}.jsx`); | |
const jsxContent = `import React, { Component } from 'react'; | |
import classnames from 'classnames'; | |
import PropTypes from 'prop-types'; | |
import scss from './${name}.scss'; | |
${generateRuntimeDeclare(needRuntime)} | |
class ${name} extends Component { | |
componentDidMount() { | |
${generateRuntimeImport(needRuntime)} | |
} | |
render() { | |
return ( | |
<div | |
className={classnames( | |
scss.${name}, | |
this.props.className, | |
)} | |
> | |
{this.props.children} | |
</div> | |
); | |
} | |
} | |
${name}.propTypes = { | |
children: PropTypes.node.isRequired, | |
className: PropTypes.string, | |
}; | |
${name}.defaultProps = { | |
className: undefined, | |
}; | |
export default ${name}; | |
`; | |
/** | |
* Creates JSX file | |
*/ | |
if (fs.existsSync(dirPath)) { | |
console.log('========== folder exist =========='); | |
} else { | |
fs.mkdirSync(dirPath); | |
fs.writeFileSync(jsPath, jsContent); | |
fs.writeFileSync(scssPath, scssContent); | |
fs.writeFileSync(jsxPath, jsxContent); | |
if (needRuntime) { | |
fs.writeFileSync(runtimePath, runtimeContent); | |
} | |
} | |
/** | |
* Generates runtime object | |
*/ | |
function generateRuntimeDeclare(ifNeed) { | |
if (ifNeed) { | |
return 'let runtimeObj;'; | |
} | |
return ''; | |
} | |
/** | |
* Creates runtime import | |
*/ | |
function generateRuntimeImport(ifNeed) { | |
if (ifNeed) { | |
return ` System | |
.import('./_runtime.js') | |
.then((runtime) => { runtimeObj = runtime; });`; | |
} | |
return ''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment