关于node.js:使用nodejs修改项目packagejson版本号

4次阅读

共计 2984 个字符,预计需要花费 8 分钟才能阅读完成。

背景:本人的我的项目在部署上传之前须要更新版本号能力胜利部署

代码

具体代码如下(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 批改为指定版本
正文完
 0