简体   繁体   中英

In Grunt, how can I set package.json version to a specific version using Grunt plugins?

grunt-cli v1.4.2

Using a Gruntfile.js , how can I set my package.json to a specific version? I use the grunt-bump plugin, so I do this

Gruntfile.js

module.exports = function(grunt) {
  grunt.initConfig({
    bump: {
      options: {
        files: ['package.json', 'package-lock.json'],
        updateConfigs: ['pkg'],
        versionType: 'patch',
        ...
      },
      ...
  });
  ...

  var myTasks = [
    "task1",
    "clean",
    ....
    "compress"
  ];

  grunt.registerTask('build', 'Build version', function () {
      grunt.config('bump.options.setVersion', "2.0.0");
      grunt.config('bump.options.regExp', true);
      grunt.config('bump.options.versionType', 'release');
      grunt.task.run(myTasks);
  });
};

...then do this on the command line...

$ npm grunt build

However, package.json is not updated to "version": "2.0.0" . What am I missing?

In Gulp I use the gulp-pipe to write to a file.

I ended up using the grunt-text-replace plugin.

I now have this.

Gruntfile.js

module.exports = function(grunt) {
  gruntfile.initConfig ({
    replace: {
      pkg_versions: {
        src: ['./package*.json'],
        overwrite: true,  // overwrite matched source files in dist
        replacements: [{
          from: '<%= pkg.version %>', 
          to: '2.0.0'
        }]
      }
    }
  });
  ...
  var myTasks = [
    "task1",
    "clean",
    "replaceZ:pkg_version",
    ....
    "compress"
  ];
  ...
  grunt.registerTask('build', 'Build version', function () {
      grunt.task.run(myTasks);
  });
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM