Skip to content

catstackdev/react-plop-template

Repository files navigation

πŸ“ Plop React Generator

A modular Plop.js code generator for creating React components, hooks, contexts, Redux slices, and more with TypeScript support.

πŸš€ Installation & Usage

Method 1: Global Link (Recommended)

Step 1: Link this package globally

# In this directory (/Users/cybercat/work/react/plop-react-generator)
pnpm link --global

Step 2: Link in your React project

# In your React project (e.g., react-test)
cd /Users/cybercat/work/react/react-test
pnpm link --global @cybercat/plop-generators

Step 3: Use the generator

# Method A: Using cybercat-plop binary
cybercat-plop component MyButton

# Method B: Using npx plop (if plop is installed in your project)
npx plop component MyButton

# Method C: Add to package.json scripts
{
  "scripts": {
    "generate": "cybercat-plop",
    "g": "cybercat-plop"
  }
}
# Then: pnpm g component MyButton

Method 2: Direct Install (Alternative)

# In your React project
cd /Users/cybercat/work/react/react-test
pnpm add -D file:../plop-react-generator

# Use it
npx plop component MyButton

Method 3: Run Directly (No Install)

# From your React project
npx plop --plopfile ../plop-react-generator/plopfile.cjs component MyButton

# Or add to package.json:
{
  "scripts": {
    "plop": "plop --plopfile ../plop-react-generator/plopfile.cjs",
    "g": "plop --plopfile ../plop-react-generator/plopfile.cjs"
  }
}

🎯 Available Generators

Core React

  • component - React component with TypeScript, CSS modules, tests
  • layout - Layout component
  • page / page2 - Page components
  • context - Context (Basic or Advanced mode)
  • hook - Custom React hook

State Management

  • redux-basic - Basic Redux slice
  • redux-async - Async Redux slice with thunks
  • redux-entity - Redux entity slice
  • redux-api - Redux API slice
  • reducer-basic - Basic reducer
  • lazy-redux-* - Lazy-loaded Redux variants

React Query

  • react-query-basic - Basic query hook
  • react-query-mutation - Mutation hook
  • react-query-infinite - Infinite query hook
  • react-query-service - API service with hooks

Utilities

  • service - API service
  • model - Data model
  • constants - Constants file
  • util - Utility functions
  • error - Error classes
  • story - Storybook story

πŸ’‘ Usage Examples

Interactive Mode

cybercat-plop
# or
npx plop

Create Component

cybercat-plop component Button

Prompts:

  • Component name? Button
  • Base HTML element? button
  • Make component flexible (allow 'as' prop)? Yes
  • Directory? src/components (or use fzf)

Generates:

src/components/Button/
β”œβ”€β”€ Button.tsx
β”œβ”€β”€ Button.types.ts
β”œβ”€β”€ Button.test.tsx
β”œβ”€β”€ Button.module.css
└── index.ts

Auto-updates src/components/index.ts with barrel exports!

Create Hook

cybercat-plop hook useCounter

Create Context

cybercat-plop context Auth

Choose: Basic (flat) or Advanced (folder structure with provider/hook)

Create Redux Slice

cybercat-plop redux-async users

Create React Query Hook

cybercat-plop react-query-basic useTodos

πŸ”§ Features

  • βœ… TypeScript Support - Full type safety
  • βœ… CSS Modules - Scoped styles
  • βœ… Polymorphic Components - Flexible as prop
  • βœ… Auto Barrel Exports - Updates index.ts automatically
  • βœ… Test Files - Co-located test scaffolding
  • βœ… Redux Toolkit - Modern Redux patterns
  • βœ… React Query - Query/mutation hooks
  • βœ… Interactive Picker - fzf directory selection
  • βœ… Handlebars Helpers - Powerful template logic

πŸ“‚ Project Structure

plop-react-generator/
β”œβ”€β”€ plopfile.cjs           # Main entry (~10 lines!)
β”œβ”€β”€ plop-config/           # Modular configuration
β”‚   β”œβ”€β”€ generators/        # All generator configs
β”‚   β”‚   β”œβ”€β”€ component.js.cjs
β”‚   β”‚   β”œβ”€β”€ context.js.cjs
β”‚   β”‚   β”œβ”€β”€ hooks.js.cjs
β”‚   β”‚   β”œβ”€β”€ reactQuery.js.cjs
β”‚   β”‚   β”œβ”€β”€ reducers.js.cjs
β”‚   β”‚   └── ...
β”‚   β”œβ”€β”€ helpers/           # Handlebars helpers
β”‚   β”‚   β”œβ”€β”€ handlebars-helpers.js.cjs
β”‚   β”‚   └── plop-helpers.js.cjs
β”‚   └── utils/             # Shared utilities
└── plop-templates/        # Handlebars templates
    β”œβ”€β”€ Component/
    β”œβ”€β”€ Context/
    β”œβ”€β”€ ReactQuery/
    β”œβ”€β”€ Redux*/
    └── ...

See plop-config/README.md for architecture details.

πŸ”„ Update Workflow

With Global Link (Method 1)

  • Edit templates or generators
  • Changes reflect immediately in all linked projects! ✨
  • No reinstall needed

With Direct Install (Method 2)

# In your React project after making changes
pnpm install

With pnpm Workspace

# At workspace root
pnpm install

πŸ› οΈ Customization

Add New Generator

  1. Create generator file:
// plop-config/generators/my-generator.js.cjs
module.exports = {
  description: "Create something awesome",
  prompts: [
    {
      type: "input",
      name: "name",
      message: "What's the name?",
    }
  ],
  actions: [
    {
      type: "add",
      path: "{{dir}}/{{pascalCase name}}.tsx",
      templateFile: "plop-templates/MyTemplate.tsx.hbs",
    }
  ]
};
  1. Register in plop-config/generators/index.js.cjs:
const myGenerator = require('./my-generator.js.cjs');

function register(plop) {
  // ... existing generators
  plop.setGenerator("my-generator", myGenerator);
}

Create Custom Template

Create .hbs file in plop-templates/:

import React from "react";

export const {{pascalCase name}} = () => {
  return <div>{{name}}</div>;
};

Available Handlebars Helpers

  • Case transforms: {{pascalCase}}, {{camelCase}}, {{kebabCase}}
  • Conditionals: {{eq a b}}, {{ne a b}}, {{or a b}}, {{and a b}}
  • Utilities: {{includes str substr}}, {{ternary cond a b}}, {{default val fallback}}
  • Arrays: {{reverse arr}}

πŸ§ͺ Testing

# Test in any React project
cd /path/to/react-project
cybercat-plop component TestComponent

# Verify generated files
ls -la src/components/TestComponent/

πŸ“ Troubleshooting

"has no binaries" warning

This is normal! The cybercat-plop binary is available after linking.

Link not working?

# Unlink and relink
pnpm unlink --global
pnpm link --global

# Check if linked
pnpm list --global --depth 0 | grep plop

Command not found?

Try using npx plop instead of cybercat-plop, or add script to package.json.

πŸ“– Help

cybercat-plop help           # Show help
cybercat-plop                # Interactive mode with all generators

πŸ“„ License

Private - For personal use by @cybercat


Made with ❀️ for rapid React development

About

react-plop-template

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published