Hemanth.HM

A Computer Polyglot, CLI + WEB ♥'r.

Publish Packages to NPM With Yeoman

| Comments

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
npm install -g generator-node # Install the generator.

I shall call the sample node package as exmp

1
2
mkdir exmp && cd $_
yo node

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
The name of your project shouldn't contain "node" or "js" and
should be a unique ID not already in use at search.npmjs.org.
[?] Module Name: exmp
[?] Description: The best module ever.
[?] Homepage:
[?] License: MIT
[?] GitHub username:
[?] Author's Name:
[?] Author's Email:
[?] Author's Homepage:
   create lib/exmp.js
   create test/exmp_test.js
   create .jshintrc
   create .gitignore
   create .travis.yml
   create README.md
   create Gruntfile.js
   create package.json

Digging deeper, we can notice the below dir struct, along with node_modules:

1
2
3
4
5
6
7
8
.
├── Gruntfile.js
├── README.md
├── lib
│   └── exmp.js
├── package.json
└── test
    └── exmp_test.js

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
'use strict';

var exmp = require('../lib/exmp.js');

/*
  ======== A Handy Little Nodeunit Reference ========
  https://github.com/caolan/nodeunit

  Test methods:
    test.expect(numAssertions)
    test.done()
  Test assertions:
    test.ok(value, [message])
    test.equal(actual, expected, [message])
    test.notEqual(actual, expected, [message])
    test.deepEqual(actual, expected, [message])
    test.notDeepEqual(actual, expected, [message])
    test.strictEqual(actual, expected, [message])
    test.notStrictEqual(actual, expected, [message])
    test.throws(block, [error], [message])
    test.doesNotThrow(block, [error], [message])
    test.ifError(value)
*/

exports['exmp'] = {
  setUp: function(done) {
    // setup here
    done();
  },
  'no args': function(test) {
    test.expect(1);
    // tests here
    test.equal(exmp.awesome(), 'awesome', 'should be awesome.');
    test.done();
  }
};

And the lib/exmp.js would be like :

1
2
3
4
5
'use strict';

exports.awesome = function() {
  return 'awesome';
};

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.

Comments