rc-gen v1.0.1
react-components-generator
TL;DR
$ npm i -g rc-gen
# or Yarn master race!
$ yarn global add rc-gen
# And... Enjoy saved time!
$ rc-gen -p ./components -n MyComponentTable of Content
Introduction
Are you tired of manually creating file structure for each react component module? And are you using React, styled-components, storybook and love unit testing with Enzyme? With react-components-generator you can make it with single CLI command! Feel exited? Read How to use react-components-generator
react-components-generator is best for those who use following dev-stack:
Prerequisites
- Node 8+
- npm
Install
npm
$ npm install --global rc-genyarn
$ yarn global add rc-genUsage
Command example
$ rc-gen -p ./components -n MyComponent -t flow -SUB --verboseThis will generate following file structure according to defined templates:
MyComponent/
__tests__/
index.unit.jsx
stories/
index.stories.js
index.jsx
styledComponents.jsOptions
| short | long | arguments | description |
|---|---|---|---|
| -n | --component-name | Component Name or identifier | |
| -p | --path | Path where you want generate a component (e.g. /src/component) | |
| -e | --file-extension | Component file extension, allowed: jsx js ts, default: jsx | |
| -t | --type-checker | Static Type Checker, allowed: flow ts none, default: none | |
| -f | --functional | Generate functional stateless component | |
| -S | --with-styled | Generate styled-components file | |
| -U | --with-test | Generate a unit test template with simple test | |
| -B | --with-stories | Generate a simple story for Storybook Tool | |
| -O | --allow-overwrites | USE AT YOUR OWN RISK! This will overwrite your files if file path matches | |
| --verbose | Prints detailed progress info | ||
| -v | --version | Version info | |
| -h | --help | Prints this manual |
Generated file templates
Following templates are made especially for Flow. Templates for
TypeScript or default ES6+ are very similar. You can check them all in lib/templates/.
React Component template
// @flow
import React, { Component } from 'react';
import * as Styled from './styledComponents';
type Props = {};
type State = {};
class MyComponent extends Component<Props, State> {
static defaultProps = {};
state = {};
render() {
return <Styled.Container>Hello world!</Styled.Container>;
}
}
export default MyComponent;React Stateless Component template
// @flow
import React from 'react';
import * as Styled from './styledComponents';
type Props = {};
const MyComponent = (props: Props) => <Styled.Container>Hello world!</Styled.Container>;
MyComponent.defaultProps = {};
export default MyComponent;Styled Components template
// @flow
import styled from 'styled-components';
export const Container = styled.div``;Unit Test template
import React from 'react';
import { shallow } from 'enzyme';
import MyComponent from '../index';
const setup = propsOverrides => {
const props = {
// TODO: props setup
...propsOverrides,
};
const wrapper = shallow(<MyComponent {...props} />);
return {
wrapper,
};
};
describe('<MyComponent />', () => {
test('Is defined', () => {
expect(MyComponent).toBeDefined();
});
test('Renders without crash', () => {
const { wrapper } = setup();
expect(wrapper.exists()).toBe(true);
});
// TODO: more tests
});Storybook template
import React from 'react';
import { storiesOf } from '@storybook/react';
import { action } from '@storybook/addon-actions';
import { ThemeProvider } from 'styled-components';
import MyComponent from '../index';
// NOTE: use your path to theme or setup Alias in Webpack
import theme from '@project/theme';
storiesOf('MyComponent', module).add('default', () => (
<ThemeProvider theme={theme}>
<MyComponent />
</ThemeProvider>
));Todo List
- Flow Support
- Partial TypeScript Support (Feedback welcome)
- Customizable file extensions (js | jsx | ts)
- More options for style engines (css, inline-styles, ...)
- Remove
shelljsdependency and rewrite it with Nodefs - Remove imports of
styledComponents.jswhen--with-styledis not set - Integrate Flow into project
- Refactoring, refactoring, refactoring...
Notes
ā Used file structure and code style is set by my own preferences and based on my personal experience and is used on project I am working on. Inspired by Airbnb's JavaScript Code Style
š This is my first released open-source project! Yaaay!
āļø Feedback is welcome!