add-component
Version:
Create React Component CLI
228 lines (170 loc) • 4.26 kB
Markdown
Generate the component boilerplate, CSS, and a shallow render test with one line.
Run
```npm install -g add-component```
```
$ add-component ${name}
$ add-component ${name} -c
$ add-component ${name} -c -f
$ add-component ${name} --redux
```
```sh
add-component example -c
```
Generates `example` folder with the following:
`index.js`
```js
import Example from './example.js'
export default Example
```
`style.css`
```css
.container {}
```
`example.js`
```js
import React, { PureComponent } from 'react'
import style from './style.css'
class Example extends PureComponent {
render () {
return (
<div className={style.container}>test</div>
)
}
}
export default Example
```
`example.test.js`
```js
import React from 'react'
import { shallow } from 'enzyme'
import Example from './example.js'
it('renders without props', () => {
shallow(<Example />)
})
```
```sh
add-component count --redux
```
Generates `count` folder with the following:
`actions.js`
```js
import t from './actionTypes.js'
export function increment () {
return {
type: t.INCREMENT
}
}
```
`actionTypes.js`
```js
export default {
INCREMENT: 'INCREMENT'
}
```
`reducer.js`
```js
import t from './actionTypes'
const defaultState = {
count: 0,
}
const score = (state = defaultState, action) => {
switch (action.type) {
case t.INCREMENT:
return {
...state,
count: state.count + 1
}
default:
return state
}
}
export default users
```
```
$ add-react-component -d src Example
$ add-react-component -c styled-components Example
$ add-react-component --no-index Example
```
You can define all the options in configuration file. Also, with configuration, you can redefine technology
generators, technology templates and filenames. Look into `config.js` to find out what cat be setted.
If you store your configuration file by `.add-component/config.js` path, you do not need any additional parameter. Just
Run the command as usual.
If you want your configuration file to have another name or be in another folder, tell the command where it is:
```
$ add-react-component --config configs/addcomponent-config.js Example
$ cat .add-component/config.js
const path = require('path')
module.exports = {
techsToGen: [
'styled-components',
'react',
'storybook',
],
techs: {
'react': {
fileName: 'index.js'
},
'storybook': {
template: path.resolve(__dirname, './storybook-template.js')
}
},
directory: './src',
}
```
In `techsToGen`, you can define the list of technologies to generate. This list will overwrite the default list, but if
you include `*`, the default technologies will preserve.<br/>
Note, that for custom technologies you will also need its configuration in `techs` field.
To re-define the list of technolofies:
```
module.exports = {
techsToGen: [
'styled-components',
'react'
]
}
```
To save default list of technologies and add more:
```
module.exports = {
techsToGen: [
'*',
'styled-components'
]
}
```
You can define a directory for your components. By default it is the root of your project.
```
module.exports = {
directory: './src'
}
```
You can choose not to have a directory for every component but put the files for all the components into the same source
folder. In this case, also configure the naming schemas for all the technologies so that all the files for different
components contain the component name and do not overwrite each other.
```
module.exports = {
componentDirectory: false
}
```
MIT © [Jack Hanford](http://jackhanford.com)