51 lines
1.3 KiB
JavaScript
51 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()}` // 提交信息
|
|
};
|
|
|
|
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);
|
|
}
|