How to use the hygen code generator

How to use the Hygen code generator

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.md
sh: curl -s https://www.toptal.com/developers/gitignore/api/archive,macos,linux,windows,node,jetbrains,sublimetext,eclipse,netbeans,visualstudiocode > .gitignore
---
<%= message %>
# <%= name %>

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

Conclusion

The sky is the limit when using generators. Hygen is providing 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.

Improve your Javascript Developer Experience by generating code

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


How to use the hygen code generator was originally published in ableneo Technology on Medium, where people are continuing the conversation by highlighting and responding to this story.