当前位置: 首页 > 后端技术 > Node.js

使用nodejs修改项目package.json版本号

时间:2023-04-03 20:18:10 Node.js

背景:自己的项目在部署上传前需要先更新版本号才能成功部署代码。详细代码如下(nodejs简单);注释了自动分支创建+提交动作;你可以根据需要自己使用//build.js文件。varexec=require('child_process').exec//异步子进程varfs=require('fs')varpackageJSON=require('./package.json')/**package.json文件的版本参数*/var版本=packageJSON。version/**命令行所有参数*/varoptions=process.argv/**命令行类型参数*/vartype=null/**新版本参数*/varnewVersion=null//判断命令行是否有逻辑处理的类型参数或版本参数for(leti=0;i-1){//有一个类型参数type=options[i].split('=')[1]}elseif(options[i].indexOf('version')>-1){//有一个版本参数newVersion=options[i].split('=')[1]}else{//code}}if(newVersion){//如果有version参数,会改变原来的versionversion=newVersion}elseif(type){//如果没有设置版本,则根据类型修改version=handleType(version,type)}else{version=nullconsole.log('----------nochangetoversion------------')}//如果修改了版本,写if(version){packageJSON.version=version//同步写入package.json文件fs.writeFileSync('package.json',JSON.stringify(packageJSON,null,2))console.log('------------update包的版本为:%s参数成功------------',version)//handleGitAdd('./package.json')//pullRemote()}/***根据处理分支类型版本号version*@param{string}oldVersion旧版本号*@param{string}type分支类型*@private*/functionhandleType(oldVersion,type){varoldVersionArr=oldVersion.split('.')//version版本号的第一位,如:1.2.3,为1varfirstNum=+oldVersionArr[0]//版本号的第二位,如:1.2.3,为2varsecondNum=+oldVersionArr[1]//版本号的第三位例如:1.2.3就是3varthirdNum=+oldVersionArr[2]switch(type){case'release'//release分支的处理逻辑++secondNumthirdNum=0breakcase'hotfix'://hotfix分支处理逻辑++thirdNumbreakdefault://默认处理最小版本++thirdNumbreak}returnfirstNum+'.'+secondNum+'.'+thirdNum}///**//*将package.json推送到远程新创建的分支//*///functionpullRemote(){//varbranch=type+'/'+version////创建一个新分支//handleGitCreate(branch)//}///**//*创建一个新分支//*@param{string}branch分支名称//*///functionhandleGitCreate(branch){//exec('gitbranch'+branch,function(error,stdout,stderr){//console.log('------------创建新分支:%sDONE------------',branch)////切换分支//handleGitCheckOut(branch)//})//}///**//*切换分支//*@param{string}branch分支名称//*///functionhandleGitCheckOut(branch){//exec('gitcheckout'+branch,function(error,stdout,stderr){//console.log('-----------切换新分支:%sDONE------------',branch)////添加修改后的文件//handleGitAdd('./package.json')//})//}/***添加修改后的文件*@param{string}filePath文件路径*/functionhandleGitAdd(filePath){exec('gitadd'+filePath,function(error,stdout,stderr){console.log('[Addmodifiedfileoutput:%s]',stdout)//提交文件handleGitCommit('updatepackage.jsonfile')})}/***提交文件*@param{string}promptcommittextnote*/functionhandleGitCommit(prompt){//varbranch=type+'/'+versionexec('gitcommit-m"'+prompt+'"',function(error,stdout,stderr){console.log('[提交修改后的文件输出:%s]',stdout)//推送分支handleGitPush()})}/***推送分支*/functionhandleGitPush(){exec('gitpush',function(error,stdout,stderr){console.log('[推送到分支:%s输出:%s]',stdout||error||stderr)console.log('------------提交修改文件完成---------')})}/***自动生成版本号脚本idea*1.获取传入参数√*2.根据参数进行逻辑处理√*3.获取package.json中的版本参数√*4.修改版本将package.json文件的值写入package.json文件√*5.git提交package.json文件√*/使用nodebuild.js--type=hotfix只修改最低版本nodebuild.js--type=release修改功能版本节点构建。js--type=release--version=0.0.8修改为指定版本