应用 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('祝贺,新分支创立胜利!');
});
}
})
})
})
});