问题
With Grunt I'm running a task which bumps my version number in my package.json file. But I want to prompt the user which versions he/she want's to update. If it's a normal update you run a minor increment (x.+1.x), and when its a patch or hotfix it should run a (x.x.+1). For this I have 2 grunt tasks:
/*
* Bump the version number to a new version
*/
bump: {
options: {
files: ['package.json'],
updateConfigs: [],
commit: true,
commitMessage: 'Release v<%= pkg.version %>',
commitFiles: ['package.json'],
createTag: true,
tagName: 'v<%= pkg.version %>',
tagMessage: 'Version <%= pkg.version %>',
push: true,
pushTo: '<%= aws.gitUrl %>',
gitDescribeOptions: '--tags --always --abbrev=1 --dirty=-d',
globalReplace: false,
prereleaseName: false,
regExp: false
}
},
/*
* Prompt to see which bump should happen
*/
prompt: {
bumptype: {
options: {
questions: [
{
config: 'bump.increment',
type: 'list',
message: 'Bump version from ' + '<%= pkg.version %> to:',
choices: [
{
value: 'patch',
name: 'Patch: Backwards-compatible bug fixes.'
},
{
value: 'minor',
name: 'Minor: Add functionality in a backwards-compatible manner.'
},
],
}
],
then: function(results) {
console.log(results['bump.increment']); // this outputs 'minor' and 'patch' to the console
}
}, // options
} // bumptype
}, // prompt
And after this I want to run it in a custom task like this:
grunt.registerTask('test', '', function () {
grunt.task.run('prompt:bumptype');
// Maybe a conditional which calls the results of prompt here?
// grunt.task.run('bump'); // this is the bump call which should be infuenced by either 'patch' or 'minor'
});
But now as I run a $ grunt test
command, I do get prompted and afterwards it's running the bump minor task no matter which option you choose.
The grunt bump option normally takes the following parameters:
$ grunt bump:minor
$ grunt bump:patch
So should you run a conditional in the prompt options or at the registerTask command?
回答1:
You can send parameters to registerTask like this
grunt.registerTask('test', function (bumptype) {
if(bumptype)
grunt.task.run('bumpup:' + bumptype);
});
This way you can do
$ grunt test minor
$ grunt test patch
回答2:
The grunt task can be added to the 'then' property of the grunt-prompt:
then: function(results) {
// console.log(results['bump.increment']);
// run the correct bump version based on answer
if (results['bump.increment'] === 'patch') {
grunt.task.run('bump:patch');
} else {
grunt.task.run('bump:minor');
}
}
来源:https://stackoverflow.com/questions/30803527/combining-a-task-grunt-bump-to-start-after-a-prompt-via-the-grunt-prompt-task