124 lines
3.5 KiB
JavaScript
124 lines
3.5 KiB
JavaScript
|
//build.js文件
|
|||
|
// let exec = require('child_process').exec // 异步子进程
|
|||
|
let fs = require('fs')
|
|||
|
const path = "./build-templates/version.json"
|
|||
|
let packageJSON = require(path)
|
|||
|
/** package.json文件的version参数 */
|
|||
|
let version = packageJSON.version
|
|||
|
// /** 命令行的所有参数 */
|
|||
|
// let options = process.argv
|
|||
|
// /** 命令行的type参数 */
|
|||
|
// let type = null
|
|||
|
// /** 新的version参数 */
|
|||
|
// let newVersion = null
|
|||
|
|
|||
|
const dt = new Date();
|
|||
|
const Year = +((dt.getFullYear() + "")[2] + (dt.getFullYear() + "")[3]);
|
|||
|
const Month = +(dt.getMonth() + 1);
|
|||
|
const Day = +(dt.getDate());
|
|||
|
let MajorVersion = +(version.MajorVersion);
|
|||
|
let MinorVersion = +(version.MinorVersion);
|
|||
|
let BuildVersion = +(version.BuildVersion);
|
|||
|
let Revision = +(version.Revision);
|
|||
|
|
|||
|
if (!MajorVersion || !MinorVersion || !BuildVersion || !Revision || Year != MajorVersion) {
|
|||
|
MajorVersion = Year;
|
|||
|
MinorVersion = Month;
|
|||
|
BuildVersion = Day;
|
|||
|
Revision = 1;
|
|||
|
} else if (Month != MinorVersion) {
|
|||
|
MinorVersion = Month;
|
|||
|
BuildVersion = Day;
|
|||
|
Revision = 1;
|
|||
|
} else if (Day != BuildVersion) {
|
|||
|
BuildVersion = Day;
|
|||
|
Revision = 1;
|
|||
|
} else {
|
|||
|
Revision++;
|
|||
|
}
|
|||
|
var data_new = {
|
|||
|
version: {
|
|||
|
MajorVersion,
|
|||
|
MinorVersion,
|
|||
|
BuildVersion,
|
|||
|
Revision
|
|||
|
}
|
|||
|
}
|
|||
|
let VersionNew = JSON.stringify(data_new);
|
|||
|
console.log(`version: ${MajorVersion}.${MinorVersion}.${BuildVersion}.${Revision}`)
|
|||
|
//同步寫入package.json文件
|
|||
|
fs.writeFileSync(path, VersionNew)
|
|||
|
return;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
// //判断命令行是否存在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) {
|
|||
|
// let oldVersionArr = oldVersion.split('.')
|
|||
|
// //版本号第一位 如:1.2.3 则为 1
|
|||
|
// let firstNum = +oldVersionArr[0]
|
|||
|
// //版本号第二位 如:1.2.3 则为 2
|
|||
|
// let secondNum = +oldVersionArr[1]
|
|||
|
// //版本号第三位 如:1.2.3 则为 3
|
|||
|
// let thirdNum = +oldVersionArr[2]
|
|||
|
// switch (type) {
|
|||
|
// case 'release':
|
|||
|
// //release分支的处理逻辑
|
|||
|
// ++secondNum
|
|||
|
// thirdNum = 0
|
|||
|
// break
|
|||
|
|
|||
|
// case 'hotfix':
|
|||
|
// //hotfix分支的处理逻辑
|
|||
|
// ++thirdNum
|
|||
|
// break
|
|||
|
|
|||
|
// default:
|
|||
|
// // 默认按照最小版本处理
|
|||
|
// ++thirdNum
|
|||
|
// break
|
|||
|
// }
|
|||
|
|
|||
|
// return firstNum + '.' + secondNum + '.' + thirdNum
|
|||
|
// }
|