使用grunt编写任务创建新分支。首选是安装必要的装饰库npmiorainquirerchalk--save。这一步写任务主要是给用户展示最新的几个分支,判断新的是否重复,如果不重复,再拉取时用shell判断输入的分支号是否存在。如果没有提示,就functiongetBranchCurrent(callback){exec('gitbranch-a',function(err,stdout,stderr,cb){constbranchList=stdout.split('\n')constbranchCurrentList=[]branchList.map(v=>{consttrimBranch=trim(v)if(trimBranch.includes('remotes')){branchCurrentList.push(trimBranch)}})回调(branchCurrentList.slice(branchCurrentList.length-3))});}//创建最新分支grunt.registerTask('creatBranch','创建新分支',function(type){grunt.log.writeln('createnewbranh-start'.green);vardone=this.async();varcurrentBranch=''getBranchCurrent((versionList)=>{grunt.log.writeln(('最近的3个远程分支'+versionList).blue);inquirer.prompt([{type:'input',name:'newVersion',message:'请输入一个新的分支,该分支将从master中检查No:',},]).then((answers)=>{grunt.config.set('currentBranch',answers.newVersion);currentBranch=answers.newVersioninquirer.prompt([{type:'confirm',名称:'useNewVersion',message:'Areyousuretocheckoutthisbranchnumber:'+answers.newVersion+']asanewbranchtothelocal?',default:false},]).then((answers)=>{if(answers.useNewVersion){constspinner=ora('新分支'+currentBranch+']创建...').start()grunt.log.writeln('');exec(`gitcheckout-b${currentBranch}&&gitpush--set-upstreamorigin${currentBranch}`,function(err,stdout,stderr,cb){if(err){spinner.fail();控制台。error(`创建失败:${err}`);done()return;}done()spinner.succeed('恭喜,新分支创建成功!');});}})})})});
