react-gen-component v1.0.1
react-gen-component
A customizable cli tool that generates/scaffolds react components from templates.
npm i -g react-gen-componentTable of Contents
Usage
gen-component MyComponent
# Or shorthand
gen MyComponentCreates a new component:
š myComponent
š myComponent.jsx # implementation
š index.js # to export componentsmyComponent.jsx
// External imports
import React from "react";
// Component imports
// Project imports
const MyComponent = (props) => {
return <></>;
};
MyComponent.defaultProps = {};
export default MyComponent;index.js
import MyComponent from "./myComponent";
export * from "./myComponent";
export default MyComponent;Typescript
To generate typescript files instead, use the --typescript or --ts flag:
gen MyComponent --tsCreates a new typescript component:
š myComponent
š myComponent.tsx # implementation
š index.ts # to export components and types
š types.ts # for types and interfacesmyComponent.tsx
// External imports
import React from "react";
// Component imports
import { MyComponentProps } from "./types";
// Project imports
const MyComponent = (props: MyComponentProps) => {
return <></>;
};
MyComponent.defaultProps = {};
export default MyComponent;index.ts
import MyComponent from "./myComponent";
export * from "./myComponent";
export * from "./types";
export default MyComponent;types.ts
export interface MyComponentProps {}Custom Templates
There are a few templates available by default. You can also create your own templates.
- Create a custom template folder and name it anything you want. All your templates will be a subdirectory of this folder.
š templates- Create a folder with the name of the template. If the name is the same as one of the included templates, your custom template will be giver higher precedence and will be used whenever you use that name.
š templates
š myTemplate- Add a file called
component.jsx.jsorcomponent.jsx.ts
š templates
š myTemplate
š component.jsx.jsAnatomy of template file names: [a].[b].[c]
[a]: The name of the file. Occurrences of the word component will be replaced with the component name.
[b]: The extension of the generated file file. If the --typescript flag has been passed, any occurrences of js will be replaced with ts (eg. jsx to tsx).
[c]: The extension of the template file. Can be .ts or .js.
- Add any additional file(s) you need. These files can be in typescript too.
š templates
š myTemplate
š component.jsx.js
š component.stories.jsx.js
š component.test.jsx.js
š types.js.js
š index.js.js- In each file, you need to
export defaulta function that takes the following parameters:
componentName : stringThe name of the component you are generating.fileName : stringThe name of the component fileisTypescript : booleanHas the typescript flag been passed?
The function should return a string or null.
Example:
// component.jsx.js
export default (name, fileName, isTypescript) => `
const ${name} = (props${isTypescript ? `: ${name}Props` : ""}) => {
return (<></>);
};
export default ${name};
`;If you want to exclude a file conditionally, you can return null:
// types.js.js
export default (name, fileName, isTypescript) =>
isTypescript ? `export interface ${name}Props {}` : null;- Use your custom template using the
template-dirand--templateoptions.
gen MyComponent --td templates --t myTemplateResults in:
š myComponent
š myComponent.jsx
š myComponent.stories.jsx
š myComponent.test.jsx
š index.jsOr if the --typescript flag is passed:
gen MyComponent --td templates --t myTemplate --typescriptResults in:
š myComponent
š myComponent.tsx
š myComponent.stories.tsx
š myComponent.test.tsx
š types.ts
š index.tsTo avoid having to pass the template directory every time, you can use a config file.
Config File
You can create a gen.config.json file to store your config. The script will search for the nearest config file and use that.
{
"directory": "./src/components",
"template-dir": "./src/templates",
"typescript": true,
"case": "kebab"
// ... Other parameters if needed
}Options
typescript
--tsor--typescript: Generate typescript files.gen MyComponent --ts
directory
-dor--diror--directory: Specify components directory. Default: is.(directory where the script is run).Example:
gen MyComponent --dir componentsGenerates:
š components š myComponent š myComponent.jsx # implementation š index.js # to export components
case
-cor--case: Specify file name case.camel(camelCase)defaultkebab(kebab-case)pascal(PascalCase)
Example:
gen MyComponent --case kebabGenerates:
š my-component š my-component.jsx # implementation š index.js # to export components
comp-case
--ccor--comp-case: Specify the component file name case. If not specified, it is the same as--case.camel(camelCase)defaultkebab(kebab-case)pascal(PascalCase)
Example:
gen MyComponent --case kebab --comp-case pascalGenerates:
š my-component š MyComponent.jsx # implementation š index.js # to export components
template
-tor--template: Specify the template.functionaldefaultclass
Example:
gen MyComponent -t class
template-dir
--td or --template-dir:
Specify a custom template directory. You can then use the template option to specify a custom template. For more info see how to make custom templates.
Example:
š customTemplates
š classComp
š component.js # implementation
š index.js # to export components
š types.js # for types and interfaces
š animations.js # for animations
š tests
š components
...gen MyComponent --td customTemplates -t classCompGenerates:
š myComponent
š myComponent.jsx # implementation
š index.js # to export components
š types.js # for types and interfaces
š animations.js # for animationshelp
-h or --help: Show help.
gen --help3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago