Hygen: Fast, Repeatable Code Scaffolding Without the Boilerplate Drag
Repetitive file creation drains momentum in active codebases. Every new UI component, service module, route handler, or unit test demands a predictable set of files, imports, and naming conventions. Copy-pasting old files leaves stale names and mismatched styles, while heavy code-generation frameworks often lock teams into rigid schemas or sprawling configuration trees.
Hygen solves this with a lightweight, template-driven _templates directory and executes them from the terminal.
Why Teams Reach for Hygen
Modern software teams care about two things when scaling a codebase: speed for individual contributors and consistency across the repository. Hygen targets both without adding runtime dependencies to your application.
- Zero lock-in: Templates live directly in your repository under
_templates/and useEmbedded JavaScript templating syntax that injects variables into text files. , keeping logic transparent and easy to edit. - Fast authoring: Creating a new generator requires adding a folder and one or more template files rather than writing custom build scripts or complex
Tree representation of source code structure used by compilers and linters. transforms. - Interactive or scripted runs: Prompts can ask for missing arguments interactively in the terminal or accept values directly on the command line for fast scripting.
- Targeted file edits: Beyond creating new files, Hygen can inject lines into existing files—such as registering a route, exporting a module, or adding an entry to an index file.
How Hygen Organizes Generators
Hygen maps command arguments directly to a folder hierarchy inside _templates. A standard invocation follows this pattern:
npx hygen <generator> <action> [options]
Each generator represents a domain or resource type (such as component, service, or api), and each action represents a specific operation (such as new, add, or remove). Inside _templates/<generator>/<action>/, template files use .ejs.t extensions and start with a frontmatter header that defines where the rendered output goes.
Here is a minimal example for creating a new feature module:
---
to: src/features/<%= h.changeCase.paramCase(name) %>/index.ts
---
export interface <%= h.changeCase.pascalCase(name) %>Config {
enabled: boolean;
}
export function create<%= h.changeCase.pascalCase(name) %>(
config: <%= h.changeCase.pascalCase(name) %>Config
) {
return {
name: "<%= h.changeCase.paramCase(name) %>",
enabled: config.enabled,
};
}
When run with npx hygen feature new --name billing-sync, Hygen reads the frontmatter to: path, renders the template with name transformed into different cases via built-in helpers, and writes src/features/billing-sync/index.ts.
Practical Workflows Beyond New Files
Generating standalone files is only half the story. Real-world codebases rely on wiring new pieces into existing entry points without forgetting a line.
| Workflow | Frontmatter Behavior | Typical Use Case |
|---|---|---|
| Create new file | Default (to: path/to/file.ts) |
Scaffolding a component, hook, controller, or test file |
| Inject into existing file | inject: true with after: or before: |
Appending an export, route registration, or DI binding |
| Skip if present | skip_if: condition |
Avoiding accidental overwrites of customized files |
| Run shell commands | sh: in frontmatter |
Formatting generated code or opening a file in an editor |
Using inject: true turns repetitive integration steps into a single command. For example, adding a new service can create service.ts and service.spec.ts while inserting a single import and export line into src/services/index.ts.
Designing Maintainable Templates
Templates age poorly when they try to encode every edge case at once. The cleanest Hygen setups stay focused and modular:
- Keep templates focused on structure: Generate clean skeletons, types, and test stubs rather than full business logic that teams will immediately rewrite.
- Use case helpers consistently: Rely on built-in casing utilities so file names, types, and identifiers follow team conventions automatically.
- Version templates alongside code: Commit
_templates/to version control so conventions evolve with architecture changes rather than living in a wiki. - Add a
READMEinside_templates: Document common commands and required flags so new contributors can scaffold files on day one without guessing argument names.
Where Hygen Fits Best—and Where It Does Not
Hygen shines in fast-moving codebases with clear architectural patterns—such as monorepos, design systems, frontend applications, and backend service layers—where engineers create dozens of similar structures every sprint. Because templates are plain text files, code reviewers can read template changes in a pull request just like any other code.
It is a poor fit when code generation depends on reading live database schemas, parsing complex existing files, or enforcing strict multi-step refactoring rules. In those cases, dedicated schema tools,
Used well, Hygen removes friction from routine setup while keeping the generated code ordinary, readable, and completely yours.
Leave a Reply