GithubHelp home page GithubHelp logo

ahsansarwar45 / react-gen-component Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 838 KB

A customizable cli tool that generates/scaffolds react components from templates

Home Page: https://www.npmjs.com/package/react-gen-component

License: MIT License

TypeScript 98.94% JavaScript 1.06%
react component generate scaffold script template typescript cli tool

react-gen-component's Introduction

react-gen-component

A customizable cli tool that generates/scaffolds react components from templates.

npm i -g react-gen-component

Table of Contents

  1. Usage
  2. Typescript
  3. Custom Templates
  4. Config File
  5. Options

Usage

gen-component MyComponent
# Or shorthand
gen MyComponent

Creates a new component:

๐Ÿ“ myComponent
    ๐Ÿ“„ myComponent.jsx # implementation
    ๐Ÿ“„ index.js # to export components

myComponent.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 --ts

Creates a new typescript component:

๐Ÿ“ myComponent
    ๐Ÿ“„ myComponent.tsx # implementation
    ๐Ÿ“„ index.ts # to export components and types
    ๐Ÿ“„ types.ts # for types and interfaces

myComponent.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.

  1. Create a custom template folder and name it anything you want. All your templates will be a subdirectory of this folder.
๐Ÿ“ templates
  1. 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
  1. Add a file called component.jsx.js or component.jsx.ts
๐Ÿ“ templates
    ๐Ÿ“ myTemplate
        ๐Ÿ“„ component.jsx.js

Anatomy 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.

  1. 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
  1. In each file, you need to export default a function that takes the following parameters:
  • componentName : string The name of the component you are generating.
  • fileName : string The name of the component file
  • isTypescript : boolean Has 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;
  1. Use your custom template using the template-dir and --template options.
gen MyComponent --td templates --t myTemplate

Results in:

๐Ÿ“ myComponent
    ๐Ÿ“„ myComponent.jsx
    ๐Ÿ“„ myComponent.stories.jsx
    ๐Ÿ“„ myComponent.test.jsx
    ๐Ÿ“„ index.js

Or if the --typescript flag is passed:

gen MyComponent --td templates --t myTemplate --typescript

Results in:

๐Ÿ“ myComponent
    ๐Ÿ“„ myComponent.tsx
    ๐Ÿ“„ myComponent.stories.tsx
    ๐Ÿ“„ myComponent.test.tsx
    ๐Ÿ“„ types.ts
    ๐Ÿ“„ index.ts

To 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

  • --ts or --typescript: Generate typescript files.

    gen MyComponent --ts

directory

  • -d or --dir or --directory: Specify components directory. Default: is . (directory where the script is run).

    Example:

    gen MyComponent --dir components

    Generates:

    ๐Ÿ“ components
        ๐Ÿ“ myComponent
            ๐Ÿ“„ myComponent.jsx # implementation
            ๐Ÿ“„ index.js # to export components

case

  • -c or --case: Specify file name case.

    • camel (camelCase) default
    • kebab (kebab-case)
    • pascal (PascalCase)

    Example:

    gen MyComponent --case kebab

    Generates:

    ๐Ÿ“ my-component
        ๐Ÿ“„ my-component.jsx # implementation
        ๐Ÿ“„ index.js # to export components

comp-case

  • --cc or --comp-case: Specify the component file name case. If not specified, it is the same as --case.

    • camel (camelCase) default
    • kebab (kebab-case)
    • pascal (PascalCase)

    Example:

    gen MyComponent --case kebab --comp-case pascal

    Generates:

    ๐Ÿ“ my-component
        ๐Ÿ“„ MyComponent.jsx # implementation
        ๐Ÿ“„ index.js # to export components

template

  • -t or --template: Specify the template.

    • functional default
    • class

    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 classComp

Generates:

๐Ÿ“ myComponent
    ๐Ÿ“„ myComponent.jsx # implementation
    ๐Ÿ“„ index.js # to export components
    ๐Ÿ“„ types.js # for types and interfaces
    ๐Ÿ“„ animations.js # for animations

help

-h or --help: Show help.

gen --help

react-gen-component's People

Contributors

ahsansarwar45 avatar azeem-io avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.