修复主题一些细节优化问题

This commit is contained in:
zqlit committed 2026-05-24 12:17:43 +08:00
1 parent 578bac0e12
commit a8f49d8651
24 files changed
+645 -67

No files matched your search

+64 -26
View File
@@ -1,45 +1,83 @@
import { NextRequest, NextResponse } from "next/server";
import { exec } from "child_process";
import { execFile } from "child_process";
import { promisify } from "util";
import { BLOG_ROOT } from "@/lib/config";
const execAsync = promisify(exec);
const execFileAsync = promisify(execFile);
type GitExecError = Error & {
stdout?: string;
stderr?: string;
code?: number;
};
async function runGit(args: string[], allowFailure = false) {
try {
const { stdout, stderr } = await execFileAsync("git", args, {
cwd: BLOG_ROOT,
windowsHide: true,
maxBuffer: 1024 * 1024,
});
return { stdout, stderr, code: 0 };
} catch (error) {
const err = error as GitExecError;
const result = {
stdout: err.stdout || "",
stderr: err.stderr || err.message,
code: typeof err.code === "number" ? err.code : 1,
};
if (allowFailure) return result;
throw new Error((result.stderr || result.stdout || err.message).trim());
}
}
function isRebaseConflict(message: string) {
return /conflict|CONFLICT|Resolve all conflicts|Automatic merge failed|could not apply|rebase/i.test(message);
}
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { title, slug } = body;
const { title } = body;
if (!title) {
return NextResponse.json({ error: "Title is required" }, { status: 400 });
}
const commitMsg = `发布: ${title}`;
// git add, commit, push from the blog root
const commands = [
`cd "${BLOG_ROOT}" && git add -A`,
`cd "${BLOG_ROOT}" && git commit -m "${commitMsg.replace(/"/g, '\\"')}"`,
`cd "${BLOG_ROOT}" && git push`,
];
let output = "";
for (const cmd of commands) {
try {
const { stdout, stderr } = await execAsync(cmd);
output += stdout + (stderr || "");
} catch (err: unknown) {
const msg = err instanceof Error ? err.message : String(err);
// git commit may fail if nothing to commit — that's ok
if (msg.includes("nothing to commit") || msg.includes("nothing added")) {
continue;
}
// git push failure is real
throw new Error(`Git command failed: ${msg}`);
}
const addResult = await runGit(["add", "content/post", "static"]);
output += addResult.stdout + addResult.stderr;
const diffResult = await runGit(["diff", "--cached", "--quiet"], true);
if (diffResult.code === 1) {
const commitResult = await runGit(["commit", "-m", `发布: ${title}`]);
output += commitResult.stdout + commitResult.stderr;
} else if (diffResult.code !== 0) {
throw new Error((diffResult.stderr || diffResult.stdout || "检查暂存区失败").trim());
}
return NextResponse.json({ success: true, output: output.slice(0, 500) });
try {
const pullResult = await runGit(["pull", "--rebase", "--autostash", "origin", "main"]);
output += pullResult.stdout + pullResult.stderr;
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
if (isRebaseConflict(message)) {
return NextResponse.json(
{
error: "远程仓库有新内容,自动合并失败。请在终端解决冲突后执行 git rebase --continue,再重新发布。",
detail: message.slice(0, 1000),
},
{ status: 409 }
);
}
throw error;
}
const pushResult = await runGit(["push"]);
output += pushResult.stdout + pushResult.stderr;
return NextResponse.json({ success: true, output: output.slice(0, 1000) });
} catch (error: unknown) {
return NextResponse.json(
{ error: error instanceof Error ? error.message : "Deploy failed" },