Skip to content

Instantly share code, notes, and snippets.

@matheuscorreia
Last active July 17, 2017 21:45
Show Gist options
  • Save matheuscorreia/b9f1447e6d1f04a503f91d23db3e9dd2 to your computer and use it in GitHub Desktop.
Save matheuscorreia/b9f1447e6d1f04a503f91d23db3e9dd2 to your computer and use it in GitHub Desktop.
A npm script I've made for myself, to automate the component creation process in React Native.
const fs = require('fs');
const args = JSON.parse(process.env.npm_config_argv).remain
const [componentGroupName, mainComponentName] = args
if(!(mainComponentName && mainComponentName)){
throw new Error('please provide the component folder name, and the main component name')
}
// |------------------------------------------------------|
// | Setting up templates |
// |------------------------------------------------------|
const indexTemplate = `import ${mainComponentName} from './${mainComponentName}'
import styles from './styles'
export { ${mainComponentName}, styles }
`
const stylesTemplate = `import EStyleSheet from 'react-native-extended-stylesheet'
export default EStyleSheet.create({})
`
const componentTemplate = `import React from 'react'
import { TouchableOpacity, View, Text, Image } from 'react-native'
import styles from './styles'
const ${mainComponentName} = (props) => ()
export default ${mainComponentName}
`
// |------------------------------------------------------|
// |******************************************************|
// |------------------------------------------------------|
fs.mkdirSync(`./app/components/${componentGroupName}`)
// writing index.js
fs.writeFileSync(`./app/components/${componentGroupName}/index.js`, indexTemplate)
// writing styles.js
fs.writeFileSync(`./app/components/${componentGroupName}/styles.js`, stylesTemplate)
// writing <mainComponentName>.js
fs.writeFileSync(`./app/components/${componentGroupName}/${mainComponentName}.js`, componentTemplate)
console.log('Created everything sucessfully.\nCode away!!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment