尝试使用 gulp-prompt 实现 gulp-git 时,我遇到了许多错误。我试图让用户能够在键入命令gulp commit时输入自己唯一的git提交消息。该消息应在用户键入 gulp-commit 命令后显示。
这是 gulp 命令
//git commit task with gulp prompt
gulp.task('commit', function(){
var message;
gulp.src('./*', {buffer:false})
.pipe(prompt.prompt({
type: 'input',
name: 'commit',
message: 'Please enter commit message...'
}, function(res){
message = res.commit;
}))
.pipe(git.commit(message));
});
目前,在终端中键入命令后,我收到以下错误。
TypeError: Cannot call method 'indexOf' of undefined
at Object.module.exports [as commit] (/Users/me/Desktop/Example 4/node_modules/gulp-git/lib/commit.js:15:18)
at Gulp.gulp.task.gulp.src.pipe.git.add.args (/Users/me/Desktop/Example 4/gulpfile.js:190:15)
at module.exports (/Users/me/Desktop/Example 4/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:34:7)
at Gulp.Orchestrator._runTask (/Users/me/Desktop/Example 4/node_modules/gulp/node_modules/orchestrator/index.js:273:3)
at Gulp.Orchestrator._runStep (/Users/me/Desktop/Example 4/node_modules/gulp/node_modules/orchestrator/index.js:214:10)
at Gulp.Orchestrator.start (/Users/me/Desktop/Example 4/node_modules/gulp/node_modules/orchestrator/index.js:134:8)
at /usr/local/lib/node_modules/gulp/bin/gulp.js:129:20
at process._tickCallback (node.js:442:13)
at Function.Module.runMain (module.js:499:11)
at startup (node.js:119:16)
at node.js:929:3
[?] Please enter commit message...
gulp-prompt不能很好地处理流,所以gulp-git(这里:git.commit
)将在message
仍然undefined
时执行。因此,gulp-git 块需要在 gulp-prompt 的回调中移动:
// git commit task with gulp prompt
gulp.task('commit', function(){
// just source anything here - we just wan't to call the prompt for now
gulp.src('package.json')
.pipe(prompt.prompt({
type: 'input',
name: 'commit',
message: 'Please enter commit message...'
}, function(res){
// now add all files that should be committed
// but make sure to exclude the .gitignored ones, since gulp-git tries to commit them, too
return gulp.src([ '!node_modules/', './*' ], {buffer:false})
.pipe(git.commit(res.commit));
}));
});