Working with Yeoman Generator

What is Yeoman?

Yeoman is a generic scaffolding system that allows you to create any kind of application. It allows for rapidly getting started on new projects and streamlines the maintenance of existing projects. Yeoman is a language-agnostic tool. It can generate projects in any language (Web, Java, Python, C#, etc.). A generator is basically a part of the building block of yeoman that plugs into the yeoman environment, as yeoman does not make any decisions by itself. A generator is, at its core, a Node.js module. It is run using the ‘yo’ command to scaffold a project or a portion of a project.

Yeoman isn’t a single tool, It more of a workflow consisting of 3 tools that work very well together. These 3 tools may be used individually. They are yo(the scaffolding tool), grunt/gulp(the build tool), and bower/npm(the package manager)

yo runs a generator module that gives a folder structure, tools to be developed with, and test frameworks.

usually npm or bower the frontend. we can run it from the frontend and specify tools and frameworks that our application depends on. It can be used to install/save without opening the browser

grunt and gulp are javascript task runners eg for running scripts, building sass, linting, etc

All three of these tools are developed and maintained separately, but work well together as part of our prescribed workflow for keeping you effective.

Writing a custom Yeoman generator

“Yeoman helps you to kickstart new projects, prescribing best practices and tools to help you stay productive.”

  1. Install yeoman, run the following command

npm install -g yo

this command will install the two necessary dependencies (yeoman scaffolding tool and the generator plugin that helps bootstrap a generator) to set up a generator.

2. Setup your generator, run the command

this command will start the generator-generator in the terminal where you will provide the relevant answers to the prompts.

3. Create a new sub-generator, run the command to generate an

yo generator:subgenerator <name>

this generates a sub-generator with the name <name>

It scaffolds out a complete generator directory structure for you.

When creating a custom generator it is important to take note of the following key functions in the run sequence of the generator.

  1. initializing ()— The initialization method is responsible for the detection state of the current directory, access to configuration, etc. from a set of templates
  2. prompting() — It’s used to show the users the option prompt (callthis.prompt())which the user then responds to with an input.
  3. configuring() — This saves the user configuration items and configures project (created.editorconfigFiles or other metadata files)
  4. default() — This consists of default values preset by the generator.
  5. writing() — this is the function is responsible for generating files and associated generators (such as routes, controllers, etc.) from a set of templates.
  6. conflicts() — for processing violation exception (Internal)
  7. install() — All other external dependencies are specified in this function for installation using the relevant library (npm, bower)
  8. end() — This is the last function called, it is commonly used for cleaning up, or goodbye messages.

Conclusion

Yeoman is a useful tool that can boost development speed and productivity whether it is used for creating generators for repetitive tasks or to scaffold a project boilerplate. It also helps to keep your code consistent and less prone to errors.

Yeoman also provides many well maintained open-source generator ecosystem with many official generators comprising tools and frameworks that can help developers quickly build beautiful web applications.

This example shows a node API blueprint generator.

Frontend Engineer