NPM version bumping and extra files
NPM has a handy little feature for bumping the versions on your packages, and it works like this:
npm version [major | minor | patch]
Want to run tests first, minify your code, and only publish the new version if everything works? Sure:
// package.json
{
...
"scripts": {
"preversion": "npm test",
"version": "npm run dist && git add -A dist",
"postversion": "git push && git push --tags && npm publish"
}
}
Ok, but what if the package has a bower.json, too? Or if other files need to include the current version number? Gulp and Grunt both have plugins to automate this, but NPM scripts don't.
In my case, I wanted to include a RawGit CDN link in my README, and have it always include the latest stable version. Here's what I did:
npm install --save-dev replace
// package.json
{
...
"scripts": {
"preversion": "npm test",
"preversion:readme": "replace '/v\\d+\\.\\d+\\.\\d+' \"/v$npm_package_version\" ./README.md",
"version": "npm run preversion:readme && npm run dist && git add -A dist README.md",
"postversion": "git push && git push --tags && npm publish"
}
}
With those four tasks, npm version patch
will now:
- Run your tests, and abort if they fail.
- Build your dist/ scripts.
- Bump the version in package.json.
- Bump the version in README.md.
- Commit package.json, README.md, and the dist/ output.
- Create a new tag.
- Push the commit and tag to GitHub.
- Publish the new version on NPM.
For more on how this works, see the full documentation on NPM scripts.