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

rc-codegen

v1.0.1

Published

React code generation helpers

Downloads

3

Readme

rc-codegen

React code generation for your projects. This package contains opinionated generators. For a sole developer this might not be useful however for teams this approach might be useful to keep a consistent coding style among developers (and react projects)

This tool uses http://www.hygen.io/ in the background but exposes only a curated set of generators for the consumers of this package. What does this actually mean?

If you read the hygen documentation it tells you that you should

  1. init hygen in your project
  2. then commit the _templates to the repository

This works well for one project, but what if your workplace/team runs multiple react projects in parallell however there is a need for a consistent code style.

If you like the generated react code what this lib provides then use it, otherwise you can incorporate the idea behing this and create your own shared react code generator.

Motivation

I noticed in my team that the developers after setting up some ground rules with regards to coding conventions doesn't care how we write something until we are consistent. For example, in a react Provider we make the provider context available to the rest of the codebase via a hook. It doesn't matter for the developers if we export such hook from the Provider.tsx file or we create a separate file just for the hook until we all do thee same.

Usually, these kind of conventions are covered by a documentation or coding guidelines. This package takes this to the next level by providing code generation tools where the generated code follows those guidelines.

Example

In a react project

npm i rc-codegen

This exposes a rcgen binary. To use it either run npm exec rcgen

~/Development/repos/rc-codegen-test (main ✗) npm exec rcgen
✔ What do you want to generate? · provider
✔ What kind of provider do you want to generate? · with-context
✔ What's your provider's name? · ThemeProvider
✔ What javascript flavor do you want use? · .tsx
✔ What kind of state container? · useReducer
✔ Outfile: src/providers/ThemeProvider.tsx (y/N) · true

Loaded templates: node_modules/rc-codegen/_templates
✔      exists: src/providers/ThemeProvider.tsx. Overwrite? (y/N):  (y/N) · true
       added: src/providers/ThemeProvider.tsx

Or call from node_modules as node_modules/.bin/rcgen

~/Development/repos/rc-codegen-test (main ✗) node_modules/.bin/rcgen 
? What do you want to generate? … 
hook
provider

Or create an npm script for it in package.json.

To add a new generator

You should read http://www.hygen.io/ first to have a good idea about generating code with hygen

npx hygen generator new <GENERATOR_NAME>

Example:

npx hygen generator new provider

this will generate

_templates
└── provider
    └── new
        └── hello.ejs.t

Here, provider is thee command and new is the subcommand. Let's rename new to with-context. Next, we also want to provide a nice prompt, so add a new file index.js next to hello.ejs.t. We should also rename hello.ejs.t to provider.ejs.t. After all this, we end up with

_templates
└── provider
    └── with-context
        ├── index.js
        └── provider.ejs.t

The index.js file covers the prompt and .ejs.t file is the template

Writing EJS templates

The playground helps tremendously https://ionicabizau.github.io/ejs-playground/