Files
spaceagent/pingan/deploy.js
T
2025-07-27 17:00:07 +08:00

46 lines
1.3 KiB
JavaScript

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()}`
commitMessage: `Auto-deploy at ${new Date().toLocaleString('sv-SE')}` // 提交信息
};
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 add .');
// 提交更改
try {
execSync(`git commit -m "${config.commitMessage}"`);
} catch (e) {
console.log('没有需要提交的更改');
process.exit(0);
}
// 推送到目标仓库
execSync(`git push -f origin ${config.branch}`);
console.log('推送仓库成功!');
process.exit(0);
} catch (error) {
console.error('推送仓库失败:', error.message);
process.exit(1);
}