我是Grunt的新用户,但我尝试将grunt-bump与grunt-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.
答案 0 :(得分:1)
您需要对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.
}
备注强>
首先,将commitMessage
任务中bump
属性的值更改为以下内容:
'<%=grunt.config("gitmessage")%>'
您grunt template中当前拥有的prompt.
部分已被省略。它应该是您在config
任务中指定的prompt
属性的值。
接下来,注册一个新任务,让我们称之为myBump
。即。
grunt.registerTask('myBump', ['prompt:commit', 'bump']);
重要提示:您可以为任务选择其他名称而不是myBump
,但不能将其命名为bump
,因为它会与现有任务冲突。
这个新注册的任务通过执行以下操作确保prompt:commit
任务在bump
之前运行:
Alias Tasks commit
任务的prompt
Target(即prompt:commit
)。
然后将bump
任务别名。
运行任务
您不需要通过CLI运行grunt bump
,而是需要运行; grunt myBump
。
通过CLI运行grunt myBump
:
首先提示您输入提交消息。例如:
Running "prompt:commit" (prompt) task
? Commit Message
关于凹凸的消息
然后运行您的bump
任务。例如::
Running "bump" task
>> Version bumped to 1.0.1 (in package.json)
>> Committed as
关于凹凸的消息
>> Tagged as "v1.0.1"
虽然解决方案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