Last active
January 12, 2019 16:20
-
-
Save saitbnzl/8fb5528e49935840ca7bffe5044424de to your computer and use it in GitHub Desktop.
Script to create react native component template
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 mkdirp = require('mkdirp'); | |
var path = require('path'); | |
const fs = require('fs'); | |
const args = process.argv.slice(2); | |
name = args[0]; | |
const mainIndexContent = `\r\nexport {default as ${name}} from './${name}'`; | |
const indexContent = `import ${name} from './${name}'\r\nexport default ${name}`; | |
const componentTemplate = `import React, { Component } from 'react'; | |
import { View, Text } from 'react-native'; | |
export default class ${name} extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
}; | |
} | |
render() { | |
return ( | |
<View> | |
<Text> ${name} </Text> | |
</View> | |
); | |
} | |
} | |
`; | |
process.chdir('src/components'); | |
fs.appendFileSync('./index.js', mainIndexContent); | |
mkdirp(path.join(process.cwd(), name), function (err) { | |
if (err) { | |
return console.error(err); | |
} | |
console.log("Directory created at ", path.join(process.cwd(), name)); | |
process.chdir(name); | |
fs.writeFile(path.join(process.cwd(), 'index.js'), indexContent, function(err) { | |
if(err) { | |
return console.log(err); | |
} | |
console.log("index.js saved"); | |
fs.writeFile(path.join(process.cwd(), name+'.js'), componentTemplate, function(err) { | |
if(err) { | |
return console.log(err); | |
} | |
console.log(`${name}.js saved`); | |
}) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"component":"node ./create_component.js"
Add this to scripts in package.json and run it with:
npm run component --name Test