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.
Start with creating the project
Saving will create your personal copy
Then you can open a new terminal.
Initialize Hygen generator
Write into the following command into the terminal.
npx hygen init self
you should see an output like this.
Now we have a generator that will be used to create other generators
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
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
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.