Grunt bump和提示提交消息

时间:2018-04-01 19:02:53

标签: javascript node.js git gruntjs

我是Grunt的新用户,但我尝试将grunt-bumpgrunt-prompt结合使用,以便提示用户输入提交消息,然后将其添加到提交中。

我已根据this post的Gruntfile.js中的代码,但提示元素不起作用。我有什么想法我做错了吗?

module.exports = function(grunt) {

  grunt.loadNpmTasks('grunt-bump');
  grunt.loadNpmTasks('grunt-prompt');

  grunt.initConfig({
    prompt: {
    commit: {
      options: {
        questions: [{
                   config: 'gitmessage',
                   type: 'input',
                   message: 'Commit Message'
                   }]
        }
      }
    },
    bump: {
    options: {
      files: ['package.json'],
      updateConfigs: [],
      commit: true,
      commitMessage: '<%=grunt.config("prompt.gitmessage")%>',
      commitFiles: ['package.json'],
      createTag: true,
      tagName: 'v%VERSION%',
      tagMessage: 'Version %VERSION%',
      push: true,
      pushTo: 'origin',
      gitDescribeOptions: '--tags --always --abbrev=1 --dirty=-d',
      globalReplace: false,
      prereleaseName: false,
      metadata: '',
      regExp: false
    }
  },
  });

};

这是终端输出:

$ grunt bump
Running "bump" task
>> Version bumped to 7.0.39 (in package.json)
>> Committed as " v7.0.39"
>> Tagged as "v7.0.39"
>> Pushed to origin

Done.

1 个答案:

答案 0 :(得分:1)

解决方案A:

您需要对Gruntfile.js进行一些更改,如以下示例所示(请参阅注释1和2)

<强> Gruntfile.js

module.exports = function(grunt) {

  grunt.loadNpmTasks('grunt-bump');
  grunt.loadNpmTasks('grunt-prompt');

  grunt.initConfig({

    prompt: {
      commit: {
        options: {
          questions: [{
            config: 'gitmessage',
            type: 'input',
            message: 'Commit Message'
          }]
        }
      }
    },
    bump: {
      options: {
        files: ['package.json'],
        updateConfigs: [],
        commit: true,
        commitMessage: '<%=grunt.config("gitmessage")%>',// 1) Change this.
        commitFiles: ['package.json'],
        createTag: true,
        tagName: 'v%VERSION%',
        tagMessage: 'Version %VERSION%',
        push: true,
        pushTo: 'origin',
        gitDescribeOptions: '--tags --always --abbrev=1 --dirty=-d',
        globalReplace: false,
        prereleaseName: false,
        metadata: '',
        regExp: false
      }
    }

  });

  grunt.registerTask('myBump', ['prompt:commit', 'bump']);// 2) Register new task.
}

备注

  1. 首先,将commitMessage任务中bump属性的值更改为以下内容:

    '<%=grunt.config("gitmessage")%>'
    

    grunt template中当前拥有的prompt.部分已被省略。它应该是您在config任务中指定的prompt属性的值。

  2. 接下来,注册一个新任务,让我们称之为myBump。即。

    grunt.registerTask('myBump', ['prompt:commit', 'bump']);
    

    重要提示:您可以为任务选择其他名称而不是myBump,但不能将其命名为bump,因为它会与现有任务冲突。

    这个新注册的任务通过执行以下操作确保prompt:commit任务在bump之前运行:

    • Alias Tasks commit任务的prompt Target(即prompt:commit)。

    • 然后将bump任务别名。

  3. 运行任务

    您不需要通过CLI运行grunt bump,而是需要运行; grunt myBump

    通过CLI运行grunt myBump

    1. 首先提示您输入提交消息。例如:

        

      Running "prompt:commit" (prompt) task

           

      ? Commit Message 关于凹凸的消息

    2. 然后运行您的bump任务。例如::

        

      Running "bump" task

           

      >> Version bumped to 1.0.1 (in package.json)

           

      >> Committed as 关于凹凸的消息

           

      >> Tagged as "v1.0.1"

    3. 解决方案B:

      虽然解决方案A 工作正常但它并不适合碰撞所有semver版本。它目前每次运行PATCH时仅会碰到grunt myBump版本。

      也许,您的目的是启用一种方法来处理各种类型的semver凸起。其分类如下:

      •   进行不兼容的API更改时

        MAJOR 版本,

      •   以向后兼容的方式添加功能时

        MINOR 版本,

      •   当您进行向后兼容的错误修复时,

        PATCH 版本。

      以下Gruntfile.js显示了处理上面列出的任一版本类型的配置。

      <强> Gruntfile.js

      module.exports = function(grunt) {
      
        grunt.loadNpmTasks('grunt-bump');
        grunt.loadNpmTasks('grunt-prompt');
      
        grunt.initConfig({
      
          prompt: {
            patch: {
              options: {
                questions: [{
                  config: 'gitmessage',
                  type: 'input',
                  message: 'Commit message for PATCH version bump:'
                }]
              }
            },
            minor: {
              options: {
                questions: [{
                  config: 'gitmessage',
                  type: 'input',
                  message: 'Commit message for MINOR version bump:'
                }]
              }
            },
            major: {
              options: {
                questions: [{
                  config: 'gitmessage',
                  type: 'input',
                  message: 'Commit message for MAJOR version bump:'
                }]
              }
            }
          },
          bump: {
            options: {
              files: ['package.json'],
              updateConfigs: [],
              commit: true,
              commitMessage: '<%=grunt.config("gitmessage")%>',
              commitFiles: ['package.json'],
              createTag: true,
              tagName: 'v%VERSION%',
              tagMessage: 'Version %VERSION%',
              push: true,
              pushTo: 'origin',
              gitDescribeOptions: '--tags --always --abbrev=1 --dirty=-d',
              globalReplace: false,
              prereleaseName: false,
              metadata: '',
              regExp: false
            }
          }
      
        });
      
        grunt.registerTask('bump-patch', ['prompt:patch', 'bump:patch']);
        grunt.registerTask('bump-minor', ['prompt:minor', 'bump:minor']);
        grunt.registerTask('bump-major', ['prompt:major', 'bump:major']);
      }
      

      <强>运行

      使用上面显示的配置,您可以根据需要通过CLI运行以下命令:

      • 要阻止PATCH版本运行:

        grunt bump-patch
        
      • 要阻止MINOR版本运行:

        grunt bump-minor
        
      • 要阻止MAJOR版本运行:

        grunt bump-major