Last year I wrote about Hitchhikers guide to npm and node-grunt but a long awaited post was to write something about Yeoman a project which I'm closely associated from inception.
This post shall be demonstrating the sheer power of Yeoman in creating and maintaining node packages.
The most interesting part of Yeoman is it's community-generators out the many I have selected generator-node authored by Addy Osamni
This generator helps you to create a node.js module including nodeunit unit tests and is based of grunt-init-node, authored by the magnificent GruntJS team.
Let's get started : First up, if you still haven't installed Yeoman, please go through the Gettng-Started wiki!
Assuming you have got Yeoman up and running, let's paw at node generator!
1
|
|
I shall call the sample node package as exmp
1 2 |
|
Now the magic happens ;) You just need to fill in the required details and the project's scaffold.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Digging deeper, we can notice the below dir struct, along with node_modules:
1 2 3 4 5 6 7 8 |
|
The package.json
would be per-populated with all the information that was provided in step 1.
The Gruntfile.js
default task is ['jshint', 'nodeunit']
.
node-unit is a easy unit testing in node.js and the browser, based on the assert module.
The exmp_test.js
would look like :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
And the lib/exmp.js
would be like :
1 2 3 4 5 |
|
That's it! Once you cook your logic you can just go ahead and publish the module to node repository, with npm publish
give that you have registered and have done a npm adduser
.
So, what are you waiting for?
BTW, this post is a product of the talk I delivered at Google Bangalore as a part of BangaloreJS, on can view bjs the demo app that was created during the talk.