The simple, fast, and scalable code generator that lives in your project.
The simple, fast, and scalable code generator that lives in your project.
New in hygen v4.0.0: a positional
NAMEparameter. Now you can use$ hygen component new MyComponentinstead of$ hygen component new --name MyComponent.
Hygen can be used to supercharge your workflow with Redux, React Native, Express and more, by allowing you avoid manual work and generate, add, inject and perform custom operations on your codebase.
If you're on macOS and have Homebrew:
$ brew tap jondot/tap
$ brew install hygenIf you have node.js installed, you can install globally with npm (or Yarn):
$ npm i -g hygenIf you like a no-strings-attached approach, you can use npx without installing globally:
$ npx hygen ...For other platforms, see releases.
Initialize hygen in your project (do this once per project):
$ cd your-project
$ hygen init selfBuild your first generator, called mygen:
$ hygen generator new mygen
Loaded templates: _templates
added: _templates/mygen/new/hello.ejs.tNow you can use it:
$ hygen mygen new
Loaded templates: _templates
added: app/hello.jsYou've generated content into the current working directory in app/. To see how the generator is built, look at _templates (which you should check-in to your project from now on, by the way).
You can build a generator that uses an interactive prompt to fill in a variable:
$ hygen generator with-prompt mygen
Loaded templates: _templates
added: _templates/mygen/with-prompt/hello.ejs.t
added: _templates/mygen/with-prompt/prompt.jsDone! Now let's use mygen:
$ hygen mygen with-prompt
? What's your message? hello
Loaded templates: _templates
added: app/hello.jsWant to start from a repo?
$ hygen init repo antfu/vitesse --to my-folderWant to start from an existing repo on an existing project?
$ mkdir your-project && cd your-project
$ hygen init repo antfu/vitesseGo to the documentation to get to know the rest of Hygen and generators.
If you're in a hurry:
hygen is a scalable generator. It has developer and team ergonomics as first priority.
It avoids "blessed" or dedicated projects that codifies code generation, which before you know it, become a product you build, needs testing, CI, separate pull request reviews, and ultimately sucks up your time and becomes this super hated thing no one likes to touch anymore.
Plus, since they are not the product you are building they become notoriously hard to reason about.
Because hygen templates live in your project, it cuts the time from having an itch for generating code (Redux, anyone?) in your current
project to code generated with it and others benefiting from it.
hygen picks up a local _templates folder
at any folder level of your project you're working from.
This is important because:
And yet, if you don't like project-local templates:
_templates folder (maybe a central git repo you maintain?) by populating an environment variable HYGEN_TMPLShygen at its core, and pack your own templates with it.The templates folder structure maps directly to the command structure:
$ hygen worker new jobrunnerFor this command, hygen worker new maps to _templates/worker/new and all files within worker/new are rendered.
Template parameters are given with --flag VALUE, as many as you'd like. In this example we've set a parameter named name to the value jobrunner.
A subcommand is a file inside a your folder structure. So if the structure is this:
_templates/
worker/
new/
index.html.ejs
setup.html.ejsAnd you only want setup, you can run:
$ hygen worker new:setupYou can also use the subcommand as a regular expression so, these will do the same:
$ hygen worker new:se$ hygen worker new:se.*Here's how a template looks like (in our example, index.ejs.t). Templates bodies are ejs:
---
to: app/workers/<%=name%>.js
---
class <%= h.capitalize(name) %> {
work(){
// your code here!
}
}The first part of the template is a front matter, idea borrowed from Markdown, this is the metadata part of a hygen template and is part of the reason of why your templates will feel more lightweight and flexible.
All frontmatter metadata are also run through the template engine so feel free to use variables in the frontmatter as you wish.
There's one required metadata variable: to.
to points to where this file will be placed (folders are created as needed).
hygen provides ability to semantic case changing with change-case library, it's simple to use and very easy to understand.
There is a usecase for react based components generation:
---
to: components/<%= name %>/index.jsx
---
import React from 'react'
export const <%= name %> = ({ children }) => (
"
)With name HelloWorld will be compiled to:
import React from 'react'
export const HelloWorld = ({ children }) => (
"
)You can see the full list here.
By default templates are 'added' to your project as a new target file, but you can also choose to inject a template into an existing target file.
For this to work, you need to use inject: true with the accompanied inject-specific props.
---
to: package.json
inject: true
after: dependencies
skip_if: react-native-fs
---
"react-native-fs":"*",This template will add the react-native-fs dependency into a package.json file, but it will not add it twice (see skip_if).
Here are the available mutually-exclusive options for where to inject at:
before | after - a regular expression / text to locate. The inject line will appear before or after the located line.prepend | append - add a line to start or end of file respectively.line_at - add a line at this exact line number.You can guard against doub
No open issues yet, or sync has not completed.