博客 / 詳情

返回

使用nodejs修改項目package.json版本號

背景:自己的項目在部署上傳之前需要更新版本號才能成功部署

代碼

詳細代碼如下(nodejs簡陋); 註釋了自動創建分支+提交動作;可根據需求自行使用

//build.js文件
var exec = require('child_process').exec // 異步子進程
var fs = require('fs')

var packageJSON = require('./package.json')
/** package.json文件的version參數 */
var version = packageJSON.version
/** 命令行的所有參數 */
var options = process.argv
/** 命令行的type參數 */
var type = null
/** 新的version參數 */
var newVersion = null

//判斷命令行是否存在type參數或version參數進行邏輯處理
for (let i = 0; i < options.length; i++) {
  if (options[i].indexOf('type') > -1) {
    //存在type參數
    type = options[i].split('=')[1]
  } else if (options[i].indexOf('version') > -1) {
    //存在version參數
    newVersion = options[i].split('=')[1]
  } else {
    //code
  }
}

if (newVersion) {
  //存在設置version參數則改變原來的version
  version = newVersion
} else if (type) {
  //不設置version則根據type來進行修改version
  version = handleType(version, type)
} else {
  version = null
  console.log('-----------沒有改變version-----------')
}

//修改了version則寫入
if (version) {
  packageJSON.version = version
  //同步寫入package.json文件
  fs.writeFileSync('package.json', JSON.stringify(packageJSON, null, 2))
  console.log('-----------更新package的version為:%s參數成功-----------', version)
  // handleGitAdd('./package.json')
  // pullRemote()
}

/**
 * 根據分支類型處理版本號version
 * @param {string} oldVersion 舊的版本號
 * @param {string} type 分支類型
 * @private
 */
function handleType(oldVersion, type) {
  var oldVersionArr = oldVersion.split('.')
  //版本號第一位 如:1.2.3 則為 1
  var firstNum = +oldVersionArr[0]
  //版本號第二位 如:1.2.3 則為 2
  var secondNum = +oldVersionArr[1]
  //版本號第三位 如:1.2.3 則為 3
  var thirdNum = +oldVersionArr[2]
  switch (type) {
    case 'release':
      //release分支的處理邏輯
      ++secondNum
      thirdNum = 0
      break

    case 'hotfix':
      //hotfix分支的處理邏輯
      ++thirdNum
      break

    default:
      // 默認按照最小版本處理
      ++thirdNum
      break
  }

  return firstNum + '.' + secondNum + '.' + thirdNum
}

// /**
//  * 將package.json推送到遠程新創建的分支
//  */
// function pullRemote() {
//   var branch = type + '/' + version
//   //創建新分支
//   handleGitCreate(branch)
// }

// /**
//  * 創建新分支
//  * @param {string} branch 分支名
//  */
// function handleGitCreate(branch) {
//   exec('git branch ' + branch, function (error, stdout, stderr) {
//     console.log('-----------創建新分支:%s DONE-----------', branch)
//     //切換分支
//     handleGitCheckOut(branch)
//   })
// }

// /**
//  * 切換分支
//  * @param {string} branch 分支名
//  */
// function handleGitCheckOut(branch) {
//   exec('git checkout ' + branch, function (error, stdout, stderr) {
//     console.log('-----------切換新分支:%s DONE-----------', branch)
//     //添加修改文件
//     handleGitAdd('./package.json')
//   })
// }

/**
 * 添加修改文件
 * @param {string} filePath 文件路徑
 */
function handleGitAdd(filePath) {
  exec('git add ' + filePath, function (error, stdout, stderr) {
    console.log('[添加修改文件輸出:%s]', stdout)
    //提交文件
    handleGitCommit('更新package.json文件')
  })
}

/**
 * 提交文件
 * @param {string} prompt commit文字備註
 */
function handleGitCommit(prompt) {
  // var branch = type + '/' + version
  exec('git commit -m "' + prompt + '"', function (error, stdout, stderr) {
    console.log('[提交修改文件輸出:%s]', stdout)
    //推送分支
    handleGitPush()
  })
}

/**
 * 推送分支
 */
function handleGitPush() {
  exec('git push ', function (error, stdout, stderr) {
    console.log('[推送至分支:%s輸出:%s]', stdout || error || stderr)
    console.log('-----------提交修改文件完成-----------')
  })
}

/**
 * 自動生成版本號腳本思路
 * 1.獲取傳進來的參數 √
 * 2.根據參數進行邏輯處理 √
 * 3.獲取package.json中的version參數 √
 * 4.修改version的值寫入package.json文件 √
 * 5.git提交package.json文件 √
 */

使用

  • node build.js --type=hotfix 只修改最小版本
  • node build.js --type=release 修改功能版本
  • node build.js --type=release --version=0.0.8 修改為指定版本
user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.