Skip to content

Instantly share code, notes, and snippets.

@saitbnzl
Last active January 12, 2019 16:20
Show Gist options
  • Save saitbnzl/8fb5528e49935840ca7bffe5044424de to your computer and use it in GitHub Desktop.
Save saitbnzl/8fb5528e49935840ca7bffe5044424de to your computer and use it in GitHub Desktop.
Script to create react native component template
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`);
})
});
});
@saitbnzl
Copy link
Author

saitbnzl commented Jan 12, 2019

"component":"node ./create_component.js"

Add this to scripts in package.json and run it with:

npm run component --name Test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment