Files
author/app/api/update-source/route.js
T
YuanShiJiLoong 3f0f8d9bcf feat: 一键自动更新 + 删除确认免提醒 + 清空设定集
- 新增一键自动更新: Electron客户端自动下载安装, 源码部署git pull+build
- NSIS改为oneClick覆盖安装, 无需手动卸载
- 删除设定集弹窗增加今日不再提醒/不再提醒选项
- 新增清空当前作品所有设定条目功能
- 修复handleDeleteNode/handleDeleteWork逻辑穿透bug
- 修复check-update require/import混用问题
2026-02-28 01:43:15 +09:00

62 lines
2.4 KiB
JavaScript

import { NextResponse } from 'next/server';
import { execSync } from 'child_process';
import { existsSync } from 'fs';
import { join } from 'path';
/**
* POST /api/update-source
* 一键更新源码部署:git pull → npm install → npm run build
* 仅在本地源码部署(存在 .git 目录)时可用
*/
export async function POST() {
const cwd = process.cwd();
const gitDir = join(cwd, '.git');
if (!existsSync(gitDir)) {
return NextResponse.json(
{ error: '非源码部署环境,无法执行自动更新' },
{ status: 400 }
);
}
const logs = [];
const log = (msg) => logs.push({ time: new Date().toISOString(), msg });
try {
// Step 1: git pull
log('🔄 正在拉取最新代码...');
const pullResult = execSync('git pull', { cwd, encoding: 'utf-8', timeout: 60000 });
log(pullResult.trim() || 'git pull 完成');
// 检查是否已经是最新
if (pullResult.includes('Already up to date') || pullResult.includes('已经是最新')) {
log('✅ 当前已是最新版本,无需更新');
return NextResponse.json({ success: true, alreadyUpToDate: true, logs });
}
// Step 2: npm install
log('📦 正在安装依赖...');
const installResult = execSync('npm install', { cwd, encoding: 'utf-8', timeout: 300000 });
// 只保留最后几行,避免输出过长
const installLines = installResult.trim().split('\n');
log(installLines.slice(-3).join('\n') || 'npm install 完成');
// Step 3: npm run build
log('🔨 正在构建项目...');
const buildResult = execSync('npm run build', { cwd, encoding: 'utf-8', timeout: 300000 });
const buildLines = buildResult.trim().split('\n');
log(buildLines.slice(-5).join('\n') || 'npm run build 完成');
log('✅ 更新完成!请刷新页面或重启服务以应用更新。');
return NextResponse.json({ success: true, alreadyUpToDate: false, logs });
} catch (err) {
log(`❌ 更新失败: ${err.message}`);
if (err.stdout) log(`stdout: ${err.stdout.toString().slice(-500)}`);
if (err.stderr) log(`stderr: ${err.stderr.toString().slice(-500)}`);
return NextResponse.json(
{ success: false, error: err.message, logs },
{ status: 500 }
);
}
}