🕵️‍♀️ "hygen" first impressions

reggi

Tea Reggi

Posted on September 6, 2020

🕵️‍♀️ "hygen" first impressions

A couple of days ago I stumbled upon a new tool called hygen hygen.io on github as jondot/hygen.

Here's what the description from the repo says:

hygen is the simple, fast, and scalable code generator that lives in your project.

The bottom line is it's a cli-based scaffolding tool. It uses an odd combination of ejs and front-matter.

How does it work?

First you install the CLI tool globally using npm.

npm install hygen -g
Enter fullscreen mode Exit fullscreen mode

Then call hygen init self to generate a scaffold, for generating scaffolds, kind of meta right?

This will allow you to run hygen generator new component.

By now you should have a _templates folder with a generator and a component directory inside.

You'll see a temporary file inside _templates/component/hello.ejs.t which you can customize to be be the new component. I would rename hello.ejs.t to simple.ejs.t and put this inside:

---
to: src/components/<%= name %>.tsx
---
import React from 'react'

export function <%= name %>(props) {
  return <h1>Hello, {props.name}</h1>;
}
Enter fullscreen mode Exit fullscreen mode

Then you can run:

hygen component new --name Example
Enter fullscreen mode Exit fullscreen mode

And voila! You will have easily created a new component.

Using Prompts

One super-cool feature is the prompt functionality, just by adding in a prompt.js file to the generator directory you can get prompted when you run the command for different ejs variables rather then supplying them as flags to the command.

The Shell

One nice addition is an option to execute a shell command from the front-matter of a generator. This allows better pipelining of side-effects.

---
sh: echo "hi"
---
Enter fullscreen mode Exit fullscreen mode

Open Questions

Is it possible to get .ejs.t vscode syntax with front matter support? Perhaps just use .ejs?

One possible workaround for vscode picking up the .t file- type is to add a file association in the vscode preferences:

{
  "files.associations": {
    // @see https://dev.to/reggi/using-hygen-3g3i
    "*.ejs.t": "html"
  }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

I love it so far! It's greatly improved my workflow and I continue to find new use cases. ❤️❤️❤️❤️❤️

💖 💪 🙅 🚩
reggi
Tea Reggi

Posted on September 6, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024

How to Use KitOps with MLflow
beginners How to Use KitOps with MLflow

November 29, 2024

Modern C++ for LeetCode 🧑‍💻🚀
leetcode Modern C++ for LeetCode 🧑‍💻🚀

November 29, 2024