Introduction
Hygen is a code generator that lives inside your project repository. It uses simple EJS templates and a convention-over-configuration approach to let developers scaffold files, components, and modules with a single command, keeping boilerplate consistent across the team.
What Hygen Does
- Generates files and directories from EJS templates stored in your repo
- Injects code into existing files at marked positions using regex or string anchors
- Supports interactive prompts for collecting input like names and options
- Runs as a CLI tool with a generate-action-name convention
- Stores all templates in a _templates directory under version control
Architecture Overview
Hygen scans the _templates directory for generators organized by name and action. Each generator contains a prompt file for interactive input and one or more EJS template files with frontmatter headers specifying the output path and injection targets. At runtime, Hygen resolves the template, evaluates EJS expressions against the collected variables, and writes or injects the result into the project tree.
Self-Hosting & Configuration
- Install globally via npm or add as a devDependency
- Run hygen init self to create the _templates directory
- Organize templates as _templates/GENERATOR/ACTION/files.ejs.t
- Use frontmatter to set output paths with to: and injection with inject: true
- Add prompt.js files for interactive CLI prompts using Enquirer
Key Features
- Templates live in the repo so the team shares and versions them together
- Frontmatter-driven output with to, inject, after, and before directives
- Shell command execution via sh: for post-generation setup steps
- Fast startup with lazy loading and no heavy framework overhead
- Works with any language or framework since it generates plain text files
Comparison with Similar Tools
- Plop — similar template-based generator with Handlebars instead of EJS
- Yeoman — full scaffolding framework with a plugin ecosystem, heavier setup
- Angular CLI schematics — powerful but tightly coupled to Angular
- Nx generators — monorepo-aware generation, requires the Nx toolchain
- cookiecutter — Python-based project templating, oriented toward full project setup
FAQ
Q: Can Hygen inject into existing files? A: Yes. Set inject: true in the template frontmatter and use after or before with a regex to specify the insertion point.
Q: Does it work with TypeScript projects? A: Yes. Hygen generates any text file. Templates can produce .ts, .tsx, .py, or any other file type.
Q: How do I share templates across multiple repositories? A: Point the HYGEN_TMPLS environment variable to a shared directory, or publish templates as an npm package and symlink.
Q: Is it framework-specific? A: No. Hygen is language-agnostic and framework-agnostic. It generates files from templates regardless of your stack.