动态

详情 返回 返回

使用grunt腳本創建新分支 - 动态 详情

使用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('恭喜,新分支創建成功!');
            });
          }
        })
      })
    })
  });

Add a new 评论

Some HTML is okay.