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.
CodePudding user response:
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);
});
};