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.
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
?
Has the generator framework changed at all with the beta of yo
1.0? Have there been breaking changes?
How does a generator register itself with the global yo
binary?
Thanks.
Yeoman and Cookiecutter are dead; long live Copier! RecallStack.
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.
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.
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
helpers.mockPrompt
helpers.assertFiles
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With