通过 grunt-prompt 任务组合任务(grunt-bump)以在提示后启动

Combining a task (grunt-bump) to start after a prompt via the grunt-prompt task

使用 G运行t 我正在 运行 执行一项任务,该任务会增加我 package.json 文件中的版本号。但我想提示用户 he/she 想要更新哪个版本。如果它是 正常 更新你 运行 一个小的增量 (x.+1.x),当它是一个补丁或修补程序时它应该 运行 (x.x.+1)。为此,我有 2 g运行t 任务:

    /*
     * 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

在此之后我想 运行 它在这样的自定义任务中:

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' 

});

但现在当我 运行 执行 $ grunt test 命令时,我确实会收到提示,之后无论您选择哪个选项,它都会 运行 执行 bump 次要任务。


g运行t bump 选项通常采用以下参数:

$ grunt bump:minor
$ grunt bump:patch

那么你应该 运行 在提示选项或 registerTask 命令中有条件吗?

你可以像这样向registerTask发送参数

grunt.registerTask('test', function (bumptype) {
    if(bumptype)
      grunt.task.run('bumpup:' + bumptype);
});

这样你就可以做到

$ grunt test minor
$ grunt test patch

可以将 grunt 任务添加到 grunt-prompt 的 'then' 属性:

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

}