code-generator-tool
v1.1.0
Published
A CLI tool for generating boilerplate code from templates
Maintainers
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-toolAfter installation, use the gen command from anywhere:
gen module UserInstall from Source
git clone https://github.com/KyPython/code-generator-tool.git
cd code-generator-tool
npm install
npm run build
npm linkUsage
List Available Templates
gen listShows 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 ProductModule Generation (Backward Compatible)
gen module UserThis creates:
User/UserController.tsUser/UserService.tsUser/UserModel.ts
Component Generation
gen generate component ButtonThis creates:
Button/Button.tsxButton/Button.test.tsxButton/Button.module.css
Route Generation
gen generate route ProductThis creates:
Product/ProductRoute.ts
Interactive Mode
For a guided experience:
gen interactive
# or shorthand
gen iThis will:
- Show available template types
- Let you select a type
- Prompt for name
- Ask for output directory
- 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-interactiveNPM 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:interactiveTemplate 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 UserComponent
Generates a React component with test and styles.
gen generate component ButtonRoute
Generates an Express route with CRUD endpoints.
gen generate route ProductCI/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
--forceis 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/componentsEnvironment Variables
CI=trueorCI=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.mdSafety Features
- Overwrite Protection: By default, the tool prompts before overwriting existing files
- Force Flag: Use
-fto 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
- Create a new directory in
templates/(e.g.,templates/hook/) - Add
.tplfiles with placeholders - 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 {};
};
EOFNow you can use:
gen generate hook useCounterCustomizing 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:
Eliminate Repetition: Don't Repeat Yourself (DRY). When you find yourself writing the same pattern repeatedly, generate it instead.
Maintain Consistency: Generated code follows the same structure and conventions, reducing bugs and improving maintainability.
Save Time: While the initial investment in creating templates pays off quickly, especially for common patterns like CRUD operations.
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 interactiveContributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT
