grunt-version

Grunt task to handle versioning of a project.

npm install grunt-version
38 downloads in the last day
246 downloads in the last week
869 downloads in the last month

grunt-version

Grunt task to handle versioning of a project.

Getting Started

Requires grunt >=0.4.2. If you haven't used grunt before, be sure to check out the Getting Started guide.

From the same directory as your project's Gruntfile and package.json, install this plugin by running the following command:

npm install grunt-version --save-dev

Once that's done, add this line to your project's Gruntfile:

grunt.loadNpmTasks('grunt-version');

If the plugin has been installed correctly, running grunt --help at the command line should list the newly-installed plugin's task. In addition, the plugin should be listed in package.json as a devDependency, which ensures that it will be installed whenever the npm install command is run.

The "version" task

Overview

In your project's Gruntfile, add a section named version to the data object passed into grunt.initConfig().

grunt.initConfig({
  version: {
    options: {
      // Task-specific options go here.
    },
    your_target: {
      // Target-specific file lists and/or options go here.
    },
  },
})

Options

options.pkg

Type: String|Object Default value: 'package.json'

A string representing a package file's path relative to Gruntfile.js, or an object representing a parsed package file.

This package file is where your "canonical" version should be set, in a "version" property. The grunt-version plugin uses that version (either incremented by the release option or not) when it updates version info in other files.

options.prefix

Type: String Default value: '[^\\-]version[\'"]?\\s*[:=]\\s*[\'"]'

A string value representing a regular expression to match text preceding the actual version within the file.

If you're following one of the popular documentation syntaxes in your js files, you might want to set the option like so:

grunt.initConfig({
  version: {
    somejs: {
      options: {
        prefix: '@version\\s*'
      },
      src: ['js/*.js']
    },
  },
})

options.release

Type: String Default value: ''

A string value representing one of the semver 2.x release types ('major', 'minor', 'patch', or 'prerelease') used to increment the value of the specified package version. See node-semver for more information about release incrementing.

Usage Examples

Default Options

In this example, the default options are used to update the version in src/testing.js based on the version property set in a package.json file located in the same directory as your Gruntfile.js. So if the version property in package.json is "0.1.2", and the src/testing.js file has the content var version = '0';, that content would change to var version = '0.1.2';

grunt.initConfig({
  version: {
    // options: {},
    defaults: {
      src: ['src/testing.js']
    }
  }
})

Custom Options

In this example, custom options are used.

grunt.initConfig({
  version: {
    options: {
      pkg: 'myplugin.jquery.json'
    },
    myplugin: {
      options: {
        prefix: 'var version\\s+=\\s+[\'"]'
      },
      src: ['src/testing.js', 'src/123.js']
    },
    myplugin_patch: {
      options: {
        release: 'patch'
      },
      src: ['myplugin.jquery.json', 'src/testing.js', 'src/123.js'],
    }
  }
});

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.

Release History

(Nothing yet)

npm loves you