Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yeoman custom generator

Tags:

yeoman

I would like to make a custom generator with the latest version of yeoman, but I'm having some difficulty. I've been digging around various sources of documentation and examples (like the webapp and angular generators), but I still have a few questions.

  1. What is the workflow for testing a generator? If I have one project that is the generator itself, do I keep making new directories to run yo my-generator?

  2. Has the generator framework changed at all with the beta of yo 1.0? Have there been breaking changes?

  3. How does a generator register itself with the global yo binary?

Thanks.

like image 459
Nick Heiner Avatar asked Feb 19 '13 16:02

Nick Heiner


People also ask

Is Yeoman still used?

Yeoman and Cookiecutter are dead; long live Copier! RecallStack.

How do I publish a yeoman generator?

Go to your github account and create a new repository. Please note that the repository name must start with generator- , and be followed by a unique name to npm. This is required to publish our generator.

How do I download Yeoman generator?

You can install Yeoman generators using the npm command and there are over 3500+ generators now available, many of which have been written by the open-source community. This will start to install the Node packages required for the generator.


1 Answers

This question is a few months old, but I think it's useful for anyone who finds it now to know that much more has been explained in http://yeoman.io/generators.html

Question 1:

Testing your generators locally can be easily done. For instance if your generator is called "generator-mtv-raps"

$ cd ~/dev/generator-mtv-raps/
$ npm link

Now if you do:

$ cd ~/dev/mytest/
$ yo mtv-raps

It will build.

Additionally, unit testing your generator with mocha is pretty straight forward. There is a set of test helpers built in. The basics of what you need to be testing against can be found in generator-webapp's tests https://github.com/yeoman/generator-webapp/blob/master/test/test.js

  1. Test that the generator can be required without breaking.
  2. Mock your prompts with helpers.mockPrompt
  3. Test that all the files you want created are created via helpers.assertFiles
  4. Test that the prompt values you mocked are inserted into the created files.

Number 4 is kind of tricky, the array of expected files can be passed a regular expression to check against. If you want to know more about the test helpers, the source is the best place. https://github.com/yeoman/generator/blob/master/lib/test/helpers.js

Question 2: already answered by btford

Question 3:

Generators are bound to yo by installing them globally (or linking them as above) and using a naming convention. All generators start with "generator" then "-" then "name".

hence generator-mtv-raps accessed via $ yo mtv-raps

like image 104
nackjicholson Avatar answered Sep 24 '22 04:09

nackjicholson