Combining a task (grunt-bump) to run after prompting with a command line task

With Grunt I have a task running that strikes my version number in my package.json file. But I want to ask the user what versions he wants to update. If it is a normal update, you do a small increment (x. + 1.x), and when its fix or fix should run (xx + 1). For this I have two 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 that 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 when I run the command $ grunt test

, I get a prompt, and then it starts a small task, no matter which option you choose.


The grunt bump parameter usually takes the following parameters:

$ grunt bump:minor
$ grunt bump:patch

      

So, should you run the condition in the prompt parameters or in the registerTask command?

0


source to share


2 answers


A 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');
    }

}

      

+1


source


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


source







All Articles