let fs = require('fs')
const dayjs = require('dayjs')
const getPackageJson = () => {
let data = fs.readFileSync('./package.json') //fs读取文件
return JSON.parse(data) //转换为json对象
}
let packageData = getPackageJson()
const updateVersion = () => {
let version = packageData.version.split(".");
version[version.length - 1]++;
packageData.version = version.join(".");
packageData.lastBuildTime = dayjs().format('YYYY-MM-DD HH:mm:ss');//最后打包时间
}
updateVersion()
fs.writeFile('./package.json', JSON.stringify(packageData, null, '\t'), err => {
if (err) {
console.log('写入失败', err)
} else {
console.log('写入成功 ' + packageData.version)
}
})
改一下packjson里面,build定义的内容
"scripts": {
"serve": "vue-cli-service serve",
"build": "node ./buildVersion.js && vue-cli-service build"
}
执行npm run build后自动更新version和lastBuildTime