【问题标题】:Combining a task (grunt-bump) to start after a prompt via the grunt-prompt task通过 grunt-prompt 任务组合一个任务(grunt-bump)在提示后启动
【发布时间】:2015-06-12 12:49:01
【问题描述】:

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

    /*
     * 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 命令时,我确实会收到提示,然后无论您选择哪个选项,它都会运行小凹凸任务。


grunt bump 选项通常采用以下参数:

$ grunt bump:minor
$ grunt bump:patch

那么您应该在提示选项中还是在 registerTask 命令中运行条件?

【问题讨论】:

    标签: gruntjs grunt-prompt


    【解决方案1】:

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

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

    这样就可以了

    $ grunt test minor
    $ grunt test patch
    

    【讨论】:

    • 但也需要调用提示中的答案对吗?场景是$ grunt test。然后终端以“次要”或“补丁”的问题提示用户。通过选择一个或另一个,您可以定义运行的任务。
    • 我认为像你说的那样调用参数是朝着正确方向迈出的一步
    • 这种方法的优点是你甚至不需要提示。由于用户无论如何都会在终端提示符中键入版本,因此他们不妨将其写入命令$ grunt test
    • 但是这种提示方法的想法是您只需要一个命令,然后使用用户友好的选择。以后不会只有这2个了。不过建议很好。
    • 调用方式应该是:grunt test:arg
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 2016-05-24
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多