How to use the Hygen code generator

Marcel Mokos
ableneo Technology
Published in
3 min readMar 13, 2019

There are platforms that list thousands of generators namely yeoman but often you will need something simple and custom. The alternative is to use Hygen for generating custom project sections.

Hygen is using javascript for prompt and EJS for templating but is fully capable of generating code in any language.

Photo by Bank Phrom on Unsplash
https://www.hygen.io/

Start with creating the project

Go to https://codesandbox.io/ and create a new node project

Saving will create your personal copy

Then you can open a new terminal.

1. Save 2. Hide browser 3.add new terminal

Initialize Hygen generator

Write into the following command into the terminal.

npx hygen init self

you should see an output like this.

npx hygen init self

Now we have a generator that will be used to create other generators

You can generate meme as well https://imgflip.com/memegenerator/Yo-Dawg-Heard-You

Generate a with-prompt generator

We will be using prompt to ask questions in the console. See examples of prompts using enquirer.

npx hygen generator with-prompt readme

Now we can change the template files

Update _templates/readme/with-prompt/hello.ejs.t to:

---to: README.md---# <%= name %><%= message %>

Update _templates/readme/with-prompt/prompt.js to:

Run the generator and create README.md

Run following command in Terminal

npx hygen readme with-prompt
The output of the generated file

Run a script in from template frontmatter

Update _templates/readme/with-prompt/hello.ejs.t to:

---to: README.mdsh: curl -s https://www.toptal.com/developers/gitignore/api/archive,macos,linux,windows,node,jetbrains,sublimetext,eclipse,netbeans,visualstudiocode > .gitignore---# <%= name %><%= message %>

The output of the shell script will create .gitignore file in your project. Curl will return it from https://www.toptal.com/developers/gitignore API and pipe it to .gitignore.

Final project

The result of the example

Conclusion

The sky is the limit when using generators. Hygen provides more options like injecting into existing files or running shell scripts, all the examples are in the documentation Hygen.io.

Read more about generators in general in the previous article.

👏Clap, 👂follow for more awesome 💟#Javascript and ⚛️#automatization content.

--

--

Marcel Mokos
ableneo Technology

I'm fanatic to next generation Javascript lambda, yield, async/await everything. I admire typescript and flow types. Javascript will ultimately rule the world.