npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rcg-cli

v0.0.3

Published

A CLI tool to quickly generate React components

Downloads

1

Readme

An Opinionated React Component Generator

Helping to enforce convention within a react project

In an effort to help remove a lot of boilerplate/duplication, this CLI allows you to generate components from the command line. It takes care of placing components in the correct directory, adding routes to the appropriate directory and updates our router.

To get started.

npm install -g rcg-cli

To add a component, simply use the generate command:

rcg generate component my-component

This will add a new component to the /components directory named my-component.js.

import React from 'react';

export default function myComponent({}) {
  return (
    <div>
    </div>
  );
}

This works with nested directories. You can also use aliased commands such gen, new or g for the generate command.

rcg g component navigation/sidebar/menu-items

reacte-component-generator will create the appropriate directories if they exist.

Routes

Routes follow a similar pattern but will be put into a routes directory. This is a somewhat opinionated approach, but the idea is that components used to serve routes should be separate than traditional components.

rcg g route profile

This will add a new component to the /routes directory named profile.js; This will also import and add the route to your App.js file. Note: this makes the assumption you are using react-router.

import React, { Component } from 'react';
import Profile from './routes/profile';
import './App.css';
import { BrowserRouter as Router, Route } from "react-router-dom";

class App extends Component {
  render() {
    return (
      <Router>
        <div className="App">
          <Route path="/profile" exact component={Profile} />
        </div>
      </Router>
    );
  }
}

export default App;

Specifying class components

By default, rcg-cli adds function components, as that seems to be the recommended pattern going forward. However, it can generate class components by using the --class flag.

rcg g component my-component --class

Generates:

import React, { Component } from 'react';

class MyComponent extends Component {
  render() {
    return (
      <div>
      </div>
    );
  }
}

This works for both routes and components.

Deleting components

Similar to generating components, they can quickly be removed with the delete command.

rcg destroy component my-component

This will remove any javascript file by that name in the components directory. This command can be aliased to del, d, or destroy.

But I Want Different Defaults

In your project, create a .react-component-generator file. This is a text file that follows a JSON format and is read at runtime to find project-specific defaults.

Currently the settings you can specify:

componentType - this can be class or function

  • class will generate class-based components by default (without requiring the --class flag)
  • function is the default and does not need to be specified

componentsDirectory - indicates the directory components will be created routesDirectory - indicates the directory routes will be created

{
  "componentType": "class",
  "componentsDirectory": "partials",
  "routesDirectory": "pages"
}

Coming soon:

Specify where the Router exists -- the assumption now is that it lives in src/App.js but that may not be the case for ever project. Specify the wrapping element that holds the routes -- the assumption is that it is the default div with a class of App that is added by create-react-app.