簡體   English   中英

發出咕bump聲並提示提交消息

[英]Grunt bump and prompt for commit message

我是Grunt的新手,但是我試圖將grunt-bumpgrunt-prompt結合使用,以便提示用戶輸入提交消息,然后將其添加到提交中。

我從這篇文章中基於我的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.

解決方案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. 首先,將您的bump任務中的commitMessage屬性值更改為以下內容:

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

    prompt. 您當前在grunt模板中擁有的部分已被省略。 它應該是僅在prompt任務中指定的config屬性的值。

  2. 接下來,注冊一個新任務,我們將其myBump

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

    重要提示:您可以選擇另一種名稱為任務,而不是myBump ,但它不能被命名bump ,因為它會與現有的任務沖突。

    此新注冊的任務通過執行以下操作來確保在bump之前運行prompt:commit任務:


運行任務

而不是運行的grunt bump通過您的CLI,你需要運行; grunt myBump

通過CLI運行grunt myBump將:

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

    Running "prompt:commit" (prompt) task

    ? Commit Message ? Commit Message 我關於顛簸的消息

  2. 隨后運行的bump任務。 例如::

    Running "bump" task

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

    >> Committed as 我關於凹凸的信息而 >> Committed as

    >> Tagged as "v1.0.1"


解決方案B:

雖然解決方案A可以正常工作,但不能容納所有semver版本。 當前,每次運行grunt myBump時,它只會增加PATCH版本。

也許,您的意圖是啟用一種處理各種類型的semver顛簸的方法。 分為以下幾類:

  • 當您進行不兼容的API更改時的主要版本,

  • 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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM