Vue 项目打包时提取代码 git commit 信息

业精于勤 荒于嬉

需求描述

前端项目经常打包,发包部署,有可能存在不确定发包的版本是哪一个,于是想要将代码打包时的代码信息也就是commit 信息 提取出来作为参考。

具体实现

1、获取git commit 信息

git 信息的获取指令 参考 官方文档是一切

https://git-scm.com/docs/git

https://git-scm.com/docs/git-show

git show format选项:

  • %H 提交对象(commit)的完整哈希字串
  • %h 提交对象的简短哈希字串
  • %cd 提交日期, RFC2822格式
const child_process = require("child_process")

// git 提交记录信息 https://git-scm.com/docs/git-show    https://git-scm.com/docs/git
const commitHash = child_process.execSync('git show -s --format=%H').toString().trim()
const localBranchName = child_process.execSync('git rev-parse --abbrev-ref HEAD').toString().trim()
const branchName = child_process.execSync(`git rev-parse --abbrev-ref ${localBranchName}@{upstream}`).toString().trim()
const commitDateObj = new Date(child_process.execSync('git show -s --format=%cd').toString())
const commitDate = `${commitDateObj.getFullYear()+'-'+(commitDateObj.getMonth()+1)+'-'+commitDateObj.getDate()+'  '+commitDateObj.getHours()+':'+commitDateObj.getMinutes()}`
const nowDate = new Date()
const buildDate = `${nowDate.getFullYear()+'-'+(nowDate.getMonth()+1)+'-'+nowDate.getDate()+'  '+nowDate.getHours()+':'+nowDate.getMinutes()}`
module.exports = {
  commitHash,
  branchName,
  commitDate,
  buildDate
}

2、在打包时封装在全局中

在 vue.config.js 中,配置打包时的信息

const ProjectVersion = require('./version.js')
console.log('ProjectVersion', ProjectVersion)
module.exports = {
  lintOnSave: false,
  chainWebpack: config => {
    config.plugin('define').tap(args => {
      args[0].ProjectVersion = JSON.stringify(ProjectVersion)
      return args
    })
  }
}

3、在需要的地方 引入封装的全局信息

测试在mian.js中 将打包信息打印 或者 挂载到window 对象上,这样项目部署后 ,便可以看到对应的信息

注意:信息在打包时设置,因此获取也要分环境,本地运行直接获取会有问题。

if (process.env.NODE_ENV == "production") {
  console.log("ProjctVersion", ProjectVersion)
  window.ProjectVersion = Object.freeze(ProjectVersion)
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
创建完Vue项目后,如果想要自动连接到Git,可以按照以下步骤进行操作: 1. 在项目文件夹中打开命令行工具(如cmd或Git Bash)。 2. 输入以下命令来配置Git的用户名和邮箱: ``` git config --global user.name "用户名" git config --global user.email "邮箱" ``` 3. 执行以下命令来初始化Git仓库: ``` git init ``` 4. 使用以下命令将所有文件添加到Git的暂存区: ``` git add . ``` 5. 执行以下命令来提交代码到本地仓库,并添加描述信息: ``` git commit -m "描述信息" ``` 6. 如果还没有创建远程仓库,可以在Git平台上创建一个新的仓库,并获取远程仓库地址。 7. 使用以下命令将本地仓库与远程仓库关联起来: ``` git remote add origin 远程仓库地址 ``` 8. 最后,执行以下命令将本地代码推送到远程仓库的主分支: ``` git push origin master ``` 这样,你的Vue项目就会自动连接到Git,并将代码推送到远程仓库的主分支。 #### 引用[.reference_title] - *1* *2* [本地vue项目连接远程仓库](https://blog.csdn.net/weixin_61032994/article/details/124898313)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Vue新建项目+git托管+初始配置+项目打包优化](https://blog.csdn.net/qq_41550143/article/details/129150254)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值