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 🙏

© 2026 – Pkg Stats / Ryan Hefner

code-generator-tool

v1.1.0

Published

A CLI tool for generating boilerplate code from templates

Readme

Code Generator Tool

A CLI tool for generating boilerplate code from templates, inspired by the "Code Generators" principle from The Pragmatic Programmer by Andy Hunt and Dave Thomas.

Overview

This tool helps eliminate repetitive boilerplate code by generating common patterns (like CRUD modules, React components, routes) from templates. As the Pragmatic Programmers say: "Don't write code—generate it."

Features

Template Discovery - Auto-discovers templates in templates/ directory
Template Validation - Validates template syntax before generation
Interactive Mode - Guided generation with prompts
Multiple Template Types - Support for modules, components, routes, and more
CI/CD Compatible - Works seamlessly in CI environments
Safe Overwrite - Prompts before overwriting (or use --force)
NPM Script Integration - Easy npm script aliases

Installation

Install from npm (Recommended)

npm install -g code-generator-tool

After installation, use the gen command from anywhere:

gen module User

Install from Source

git clone https://github.com/KyPython/code-generator-tool.git
cd code-generator-tool
npm install
npm run build
npm link

Usage

List Available Templates

gen list

Shows all available template types and how many templates each has.

Generate Code

Generic Generation

gen generate <type> <name>
# or shorthand
gen g <type> <name>

Examples:

# Generate a module
gen generate module User

# Generate a React component
gen generate component Button

# Generate a route
gen generate route Product

Module Generation (Backward Compatible)

gen module User

This creates:

  • User/UserController.ts
  • User/UserService.ts
  • User/UserModel.ts

Component Generation

gen generate component Button

This creates:

  • Button/Button.tsx
  • Button/Button.test.tsx
  • Button/Button.module.css

Route Generation

gen generate route Product

This creates:

  • Product/ProductRoute.ts

Interactive Mode

For a guided experience:

gen interactive
# or shorthand
gen i

This will:

  1. Show available template types
  2. Let you select a type
  3. Prompt for name
  4. Ask for output directory
  5. Confirm overwrite behavior

Options

  • -o, --output <dir>: Specify output directory (default: current directory)
  • -f, --force: Overwrite existing files without prompting
  • -i, --interactive: Enable interactive mode (default: true, auto-disabled in CI)
  • --no-interactive: Disable interactive mode

Examples:

# Generate in a specific directory
gen module Product -o src/modules

# Force overwrite without confirmation
gen module Order -f

# Non-interactive mode (useful for scripts)
gen generate component Button --no-interactive

NPM Script Integration

Add these scripts to your package.json for easy access:

{
  "scripts": {
    "gen:list": "gen list",
    "gen:module": "gen module",
    "gen:component": "gen generate component",
    "gen:route": "gen generate route",
    "gen:interactive": "gen interactive",
    "gen:help": "gen --help"
  }
}

Then use them like:

npm run gen:module User
npm run gen:component Button
npm run gen:interactive

Template Placeholders

Templates support the following placeholders:

  • __NAME__: Uppercase (e.g., USER)
  • __Name__: PascalCase (e.g., User)
  • __name__: camelCase (e.g., user)
  • __name-plural__: Plural camelCase (e.g., users)
  • __NAME_PLURAL__: Plural uppercase (e.g., USERS)

Available Template Types

Module

Generates a complete CRUD module with Controller, Service, and Model.

gen module User

Component

Generates a React component with test and styles.

gen generate component Button

Route

Generates an Express route with CRUD endpoints.

gen generate route Product

CI/CD Integration

The tool automatically detects CI environments and adjusts behavior:

  • Non-interactive by default - No prompts in CI
  • Skips existing files - Won't overwrite unless --force is used
  • Validates templates - Checks template syntax before generation

GitHub Actions Example

- name: Generate code
  run: |
    npm install -g code-generator-tool
    gen generate component Button --no-interactive -o src/components

Environment Variables

  • CI=true or CI=1 - Automatically disables interactive mode

Project Structure

code-generator-tool/
├── src/
│   ├── cli.ts           # CLI entry point
│   └── generator.ts     # Core generator logic
├── templates/
│   ├── module/          # CRUD module templates
│   │   ├── __Name__Controller.ts.tpl
│   │   ├── __Name__Service.ts.tpl
│   │   └── __Name__Model.ts.tpl
│   ├── component/       # React component templates
│   │   ├── __Name__.tsx.tpl
│   │   ├── __Name__.test.tsx.tpl
│   │   └── __Name__.module.css.tpl
│   └── route/           # Express route templates
│       └── __Name__Route.ts.tpl
├── dist/                # Compiled JavaScript
└── README.md

Safety Features

  • Overwrite Protection: By default, the tool prompts before overwriting existing files
  • Force Flag: Use -f to skip prompts (useful for CI/CD)
  • Input Sanitization: Prevents path traversal attacks
  • Template Validation: Checks template syntax before generation

Extending the Tool

Adding New Template Types

  1. Create a new directory in templates/ (e.g., templates/hook/)
  2. Add .tpl files with placeholders
  3. The generator will automatically discover them

Example:

mkdir templates/hook
cat > templates/hook/__Name__.ts.tpl << 'EOF'
export const use__Name__ = () => {
  // Add your hook logic
  return {};
};
EOF

Now you can use:

gen generate hook useCounter

Customizing Templates

Edit the template files in templates/ to match your project's conventions. The generator will use your custom templates for all future generations.

Template Validation

Templates are automatically validated before generation. The tool checks for:

  • File existence
  • Empty templates
  • Unclosed placeholders
  • Basic syntax issues

Relation to "Code Generators" (The Pragmatic Programmer)

In The Pragmatic Programmer, Hunt and Thomas advocate for using code generators to:

  1. Eliminate Repetition: Don't Repeat Yourself (DRY). When you find yourself writing the same pattern repeatedly, generate it instead.

  2. Maintain Consistency: Generated code follows the same structure and conventions, reducing bugs and improving maintainability.

  3. Save Time: While the initial investment in creating templates pays off quickly, especially for common patterns like CRUD operations.

  4. Document Patterns: Templates serve as living documentation of your architectural patterns.

This tool embodies these principles by:

  • Providing reusable templates for common patterns
  • Ensuring consistent code structure across modules
  • Reducing manual coding errors
  • Making it easy to update patterns by modifying templates
  • Supporting multiple template types for different use cases

Development

# Build TypeScript
npm run build

# Run in development mode
npm run dev module User

# Run compiled version
npm start module User

# Test interactive mode
npm run dev interactive

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT