javascript - Grunt 碰撞并提示提交消息

标签 javascript node.js git gruntjs

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

我的 Gruntfile.js 中的代码来自 this post但提示元素不起作用。你知道我做错了什么吗?

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")%>'
    

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

  2. 接下来,注册一个新任务,我们将其命名为 myBump。即

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

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

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

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

    • 然后为 bump 任务设置别名。

<小时/>

运行任务

您需要运行以下命令,而不是通过 CLI 运行 grunt bubble咕噜myBump

通过 CLI 运行 grunt myBump 将:

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

    Running "prompt:commit" (prompt) task

    ? Commit Message My message about the bump

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

    Running "bump" task

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

    >> Committed as My message about the bump

    >> Tagged as "v1.0.1"

<小时/>

解决方案B:

虽然解决方案 A 工作正常,但它无法适应所有 semver 的碰撞。版本。目前,每次运行 grunt myBump 时,它只会更改 PATCH 版本。

也许,您的目的是提供一种方法来处理各种类型的 semver 碰撞。其分类如下:

  • MAJOR version when you make incompatible API changes,

  • MINOR version when you add functionality in a backwards-compatible manner,

  • PATCH version when you make backwards-compatible bug fixes.

以下 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
    

关于javascript - Grunt 碰撞并提示提交消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49601623/

相关文章:

javascript - 如何强制用户从下拉菜单和按钮中选择一个值以启用保存按钮?

javascript - node.js 中的 Snake_case 或 camelCase

git subtree 简化如果从不贡献上游?

git - 如何在 Groovyscript 中导入 Jenkins 插件?

javascript - 变量未传递到 jQuery 滚动函数中

javascript - 过滤 2 个数组中的唯一元素

javascript - jQuery 代码不更新数据库中的字段或更新下拉列表

node.js - 错误 : ENOENT: no such file or directory, 打开 'dist/index.html'

node.js - Lambda 函数无法在 S3 上创建文件

node.js - 加速 Jenkins 构建