include-version

1.0.2 • Public • Published

include-version

Adds package version to the built file before semantic release publishes it

NPM

Build status semantic-release js-standard-style

Why?

If you use semantic release (and you should!) then the version inside package.json is no longer controlled by you directly. Instead, the automated step on CI server determines the version bump and sets it right before publishing to NPM registry. Typically I keep the placeholder version in package.json anyway - a lot of tools display a warning or break if this file is missing version property completely.

{
  "name": "my-tool",
  "version": "0.0.0-semantic-release",
  "description": "..."
}

When publishing from CI, the command goes like this

{
  "scripts": {
    "semantic-release": "semantic-release pre && npm publish && semantic-release post"
  }
}

The semantic-release pre looks at the last published version, computes the version bump (major, minor or patch), then writes the new version into package.json before proceeding to npm publish.

A lof of time I build a bundle of JavaScript in my libraries. I love embedding the version in the bundle because it gives the user a very simple way to determine which software is running. Other libraries do it too, for example Angular has version property

angular.version
//  {full: "1.3.14", major: 1, minor: 3, dot: 14, codeName: "..."}

Yet, I cannot embed the version when building the bundle, even on CI, because the version has NOT been determined yet! Thus this tool include-version. Just add it before npm publish command and it will grab the version from the package.json (already set by semantic-release pre) and will replace the placeholder text in the bundle files.

npm install --save-dev include-version
{
  "scripts": {
    "semantic-release": "semantic-release pre && include-version && npm publish && semantic-release post"
  },
  "config": {
    "include-version": ["dist/bundle.js", "dist/bundle-min.js"]
  }
}

As shown above, you configure which files to update in package.json "config" block. All file paths are relative to the current working folder, which is the root folder of the NPM package. In all files, the tool will replace string 1.0.2 if found. Thus I usually have something like this in my library source

module.exports = {
  // bunch of code
  VERSION: '1.0.2'
}

which will get set just before publishing to NPM.

Example

For example, this file has the version in the line below

version 1.0.2

On Github, this file shows the original raw text. On NPM registry it shows the current published version.

Small print

Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2016

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

MIT License

Copyright (c) 2016 Gleb Bahmutov <gleb.bahmutov@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package Sidebar

Install

npm i include-version

Weekly Downloads

11

Version

1.0.2

License

MIT

Last publish

Collaborators

  • bahmutov