Last active
May 29, 2016 17:43
-
-
Save tkbremnes/f2e59bdf60a5ccceccc33bff980924c3 to your computer and use it in GitHub Desktop.
Personal React createComponent script, cuts down on the boilerplate code I need to write.
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
"use strict"; | |
const fs = require('fs'); | |
if (!process.argv[2]) { | |
// TODO: make it possible to create a name here | |
throw new Error("No component name"); | |
} | |
const componentName = process.argv[2]; | |
const template = [ | |
`import React from 'react';`, | |
``, | |
`class ${ componentName } extends React.Component {`, | |
` render() {`, | |
` return (`, | |
` `, | |
` )`, | |
` }`, | |
`}`, | |
``, | |
`export default ${ componentName };` | |
]; | |
const templateString = (function templateToString(arrayOfStrings) { | |
let res = ''; | |
arrayOfStrings.forEach((string) => { | |
res += `${ string }\n`; | |
}); | |
return res; | |
})(template); | |
const path = `./scripts/Components/${ componentName }`; | |
fs.exists(path, (componentExists) => { | |
if(componentExists) { | |
throw new Error("Component already exists"); | |
} | |
fs.mkdir(path, () => { | |
fs.writeFile(`${ path }/index.jsx`, templateString, (err) => { | |
if (err) { | |
throw new Error('Could not write file'); | |
} | |
console.log(`${ componentName } created`); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment