使用grunt 写一个创建新分支的task
- 首选安装必要的修饰库
npm i ora inquirer chalk --save
- 编写task
这一步主要为了给用户展示最近的几个分支用来判断新建是否重复,如果不用也可以,那就在拉取的时候通过shell判断下输入的分支号是否存在,不存在提示就行
function getBranchCurrent(callback) {
exec('git branch -a', function (err, stdout, stderr, cb) {
const branchList = stdout.split('\n')
const branchCurrentList = []
branchList.map(v => {
const trimBranch = trim(v)
if (trimBranch.includes('remotes')) {
branchCurrentList.push(trimBranch)
}
})
callback(branchCurrentList.slice(branchCurrentList.length - 3))
});
}
// 创建最新分支
grunt.registerTask('creatBranch', '创建新的分支', function (type) {
grunt.log.writeln('创建新branh-start'.green);
var done = this.async();
var currentBranch = ''
getBranchCurrent((versionList) => {
grunt.log.writeln(('最近的3个远程分支' + versionList).blue);
inquirer.prompt([
{
type: 'input',
name: 'newVersion',
message: '请输入新的将要从master Check的分支号:',
},
]).then((answers) => {
grunt.config.set('currentBranch', answers.newVersion);
currentBranch = answers.newVersion
inquirer.prompt([
{
type: 'confirm',
name: 'useNewVersion',
message: '是否确认将此分支号: ' + answers.newVersion + '】 作为新的分支checkout到本地?',
default: false
},
]).then((answers) => {
if (answers.useNewVersion) {
const spinner = ora('新分支 ' + currentBranch + '】 创建中。。。。。。').start()
grunt.log.writeln('');
exec(`git checkout -b ${currentBranch} && git push --set-upstream origin ${currentBranch}`, function (err, stdout, stderr, cb) {
if (err) {
spinner.fail();
console.error(`创建失败: ${err}`);
done()
return;
}
done()
spinner.succeed('恭喜,新分支创建成功!');
});
}
})
})
})
});