Git Product home page Git Product logo

genversion's Introduction

genversion

npm version

Logo

So you want yourmodule.version to follow the version in package.json but are tired of updating it manually every time the version changes? You could import your package.json into the module but you know it is a naughty thing to do! Genversion to the rescue!

Try it out

Usage is simple:

$ cd yourmodule
$ npm install -g genversion
$ genversion lib/version.js

Voilà! The new lib/version.js:

module.exports = '1.2.3'

Integrate to your build

First install via npm.

$ npm install genversion --save-dev

Genversion works by first reading the current version from package.json and then generating a simple CommonJS module file that exports the version string. For safety, the version file begins with a signature that tells genversion that the file can be overwritten.

// generated by genversion
module.exports = '1.2.3'

Now, your job is to 1) choose a path for the version file, 2) require() the new file into your module, and 3) add genversion as a part of your build or release pipeline. For example, let us choose the path 'lib/version.js' and require it in yourmodule/index.js:

...
exports.version = require('./lib/version')
...

Then, let us integrate genversion into your build task.

"scripts": {
  "build": "genversion lib/version.js && other build stuff"
}

The target path is given as the first argument. If the file already exists and has been previously created by genversion, it is replaced with the new one.

Finished! Now your module has a version property that matches with package.json and is updated every time you build the project.

> var yourmodule = require('yourmodule')
> yourmodule.version
'1.2.3'

Great! Having a version property in your module is very convenient for debugging. More than once we have needed to painstakingly debug a module, just to find out that it was a cached old version that caused the error. An inspectable version property would have helped a big time.

Command line API

Directly from $ genversion --help:

Usage: genversion [options] <target>

Generates a version module at the target filepath.


Options:

  -V, --version  output the version number
  -v, --verbose  output the new version
  -s, --semi     use semicolons in generated code
  -e, --es6      use es6 syntax in generated code
  -h, --help     output usage information

Node API

You can also use genversion within your code:

var gv = require('genversion');

The available properties and functions are listed below.

genversion.check(targetPath, callback)

Check if it is possible to generate the version module into targetPath.

Parameters:

  • targetPath: string. An absolute or relative file path. Relative to process.cwd().
  • callback: function (err, doesExist, isByGenversion). Parameter doesExist is a boolean that is true if a file at targetPath already exists. Parameter isByGenversion is a boolean that is true if the existing file seems like it has been generated by genversion.

Example:

gv.check('lib/version.js', function (err, doesExist, isByGenversion) {
  if (err) {
    throw err;
  }

  if (isByGenversion) {
    gv.generate(...)
  }
  ...
});

genversion.generate(targetPath, opts, callback)

Read the version from the nearest package.json along the targetPath and generate a version module into targetPath.

Parameters:

  • targetPath: string. An absolute or relative file path. Relative to process.cwd().
  • opts: optional options. Available keys are:
    • useSemicolon: optional boolean.
  • callback: function (err, version). Parameter version is the version string read from package.json. Parameter err is non-null if package.json cannot be found, its version is not a string, or writing the module fails.

Examples:

gv.generate('lib/version.js', function (err, version) {
  if (err) {
    throw err;
  }
  console.log('Sliding into', version, 'like a sledge.');
});

gv.generate('src/v.js', { useSemicolon: true }, function (err) {
  if (err) { throw err }
  console.log('Generated version file with a semicolon.')
})

genversion.version

The version string of genversion module in semantic versioning format. Generated with genversion itself, of course.

Projects using genversion

Related projects

For developers

Run test suite:

$ npm run test

To make release, bump the version in package.json and run:

$ npm run release

License

MIT

genversion's People

Contributors

axelpale avatar

Watchers

James Cloos avatar James Rac avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.