Stephen Cassels bio photo

Stephen Cassels

Learning, Developing and Testing.

Email Github

npm scripts for automation

I wanted an easy process for publishing changes to my simple node module.

My learning…about using npm scripts for build automation

My requirements were very common:

  • Failing tests to prevent publishing.
  • The lib to be compiled to only when tests pass.
  • Simple version management and releases to get tagged in the git repo.
  • Avoiding Gulp as its overkill for my simple workflow (so far!).
  • Avoid custom shell scripts and be platform independent.

So I’ve learned about some of the great features available in the node scripts entry:

  • pre and post triggers
  • version bumping

I added these scripts for automation to my simple node module:

  "scripts": {
"preversion": "npm test",
"prepublish": "coffee -o lib/ -c src/calculator.coffee",
"patch-release": "npm version patch && npm publish && git push --follow-tags",
"minor-release": "npm version minor && npm publish && git push --follow-tags",
"major-release": "npm version major && npm publish && git push --follow-tags",
"test": "./node_modules/.bin/nodeunit tests"
},

Which means I can do a patch version bump, test, publish to npm and tag the release in GitHub with:

  $ npm run patch-release

and get this output:

Output from one command to version bump, test, publish to npm and tag the release on GitHub.

Whilst learning this I came across some great resources on automation: