From 7820a87e247d57272f2805cdc6299486e22299d1 Mon Sep 17 00:00:00 2001 From: lcc <805383944@qq.com> Date: Sun, 27 Jul 2025 16:18:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E8=AF=91=E5=90=8E=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=8F=90=E4=BA=A4git?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pingan/deploy.js | 50 ++++++++++++++++++++++++++++++++++++++++++++ pingan/package.json | 3 ++- pingan/vue.config.js | 14 +++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 pingan/deploy.js diff --git a/pingan/deploy.js b/pingan/deploy.js new file mode 100644 index 0000000..c123f9c --- /dev/null +++ b/pingan/deploy.js @@ -0,0 +1,50 @@ +const { execSync } = require('child_process'); +const path = require('path'); +const fs = require('fs'); + +// 配置目标Git仓库信息 +const config = { + repoUrl: 'http://git2.haodian.cn/lcc/pinan_dist.git', // 目标仓库地址 + branch: 'master', // 目标分支 + distPath: path.resolve(__dirname, './dist'), // 构建产物目录 + commitMessage: `Auto-deploy at ${new Date().toISOString()}` // 提交信息 +}; + +try { + // 检查dist目录是否存在 + if (!fs.existsSync(config.distPath)) { + throw new Error('dist目录不存在,请先执行构建命令'); + } + + // 进入dist目录 + process.chdir(config.distPath); + + // 初始化git仓库(如果需要) + if (!fs.existsSync(path.join(config.distPath, '.git'))) { + execSync('git init'); + execSync(`git remote add origin ${config.repoUrl}`); + } + + // 拉取最新代码 + execSync(`git pull origin ${config.branch} --rebase`, { stdio: 'ignore' }); + + // 添加所有文件 + execSync('git add .'); + + // 提交更改 + try { + execSync(`git commit -m "${config.commitMessage}"`); + } catch (e) { + console.log('没有需要提交的更改'); + process.exit(0); + } + + // 推送到目标仓库 + execSync(`git push origin ${config.branch}`); + + console.log('部署成功!'); + process.exit(0); +} catch (error) { + console.error('部署失败:', error.message); + process.exit(1); +} diff --git a/pingan/package.json b/pingan/package.json index 99d1bc4..7b741a6 100644 --- a/pingan/package.json +++ b/pingan/package.json @@ -9,7 +9,8 @@ "build:preview": "vue-cli-service build --mode preview", "build:report": "vue-cli-service build --report", "lint": "vue-cli-service lint", - "clean:lib": "rimraf node_modules" + "clean:lib": "rimraf node_modules", + "build:deploy": "vue-cli-service build && node deploy.js" }, "dependencies": { "@amap/amap-jsapi-loader": "^1.0.1", diff --git a/pingan/vue.config.js b/pingan/vue.config.js index f6adb03..94370e6 100644 --- a/pingan/vue.config.js +++ b/pingan/vue.config.js @@ -1,5 +1,6 @@ const CompressionWebpackPlugin = require('compression-webpack-plugin'); const { transformElementScss } = require('ele-admin/lib/utils/dynamic-theme'); +const path = require('path'); // 添加这一行引入 path 模块 module.exports = { devServer: { @@ -30,6 +31,19 @@ module.exports = { threshold: 10240 }) ); + } else if (process.env.NODE_ENV === 'production') { + config.plugin('done').use(require('webpack/lib/ProgressPlugin'), [ + { + handler: (percentage) => { + // 当构建完成时执行部署脚本 + if (percentage === 1) { + console.log('构建完成,开始部署到Git仓库...'); + const deployScript = path.resolve(__dirname, './deploy.js'); + require(deployScript); + } + } + } + ]); } }, css: {