feat: 展示小程序构建过程和日志
This commit is contained in:
1 parent
8b4d27b0ee
commit
998e32e49c
7 files changed
+182
-14
No files matched your search
@@ -10,6 +10,7 @@ import {
|
||||
} from "@nestjs/common";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import { existsSync } from "node:fs";
|
||||
import { spawn } from "node:child_process";
|
||||
import { resolve } from "node:path";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { getTemplatePackageManifest, templatePackageRegistry } from "@personal-brand/config";
|
||||
@@ -52,6 +53,18 @@ interface MiniprogramCi {
|
||||
}) => Promise<Record<string, unknown>>;
|
||||
}
|
||||
|
||||
interface BuildLog {
|
||||
at: string;
|
||||
message: string;
|
||||
stream: "stdout" | "stderr";
|
||||
}
|
||||
|
||||
interface BuildProcess {
|
||||
stdout: NodeJS.ReadableStream;
|
||||
stderr: NodeJS.ReadableStream;
|
||||
on: (event: "close" | "error", listener: (code: number | null) => void) => void;
|
||||
}
|
||||
|
||||
@Controller("admin/miniapp-releases")
|
||||
export class AdminMiniAppReleaseController {
|
||||
constructor(
|
||||
@@ -170,10 +183,58 @@ export class AdminMiniAppReleaseController {
|
||||
const templateKey = miniAppTemplateKey(brand.templateKey);
|
||||
if (!templateKey) throw new BadRequestException("当前品牌模板没有可上传的小程序包");
|
||||
const projectPath = resolve(MINIAPP_ARTIFACT_ROOT, templateKey, "weapp");
|
||||
if (!existsSync(projectPath) || !existsSync(resolve(projectPath, "app.json")))
|
||||
throw new BadRequestException(
|
||||
`小程序构建产物不存在:${projectPath},请先执行对应模板的微信小程序构建`,
|
||||
);
|
||||
const buildLogs: BuildLog[] = [
|
||||
{
|
||||
at: new Date().toISOString(),
|
||||
message: `开始构建 ${templateKey} 微信小程序`,
|
||||
stream: "stdout",
|
||||
},
|
||||
];
|
||||
await this.releases.updateStatus(identity.brandId, identity.subject, taskId, {
|
||||
status: "building",
|
||||
buildLogs,
|
||||
});
|
||||
const result = await this.buildWeApp(templateKey, (message, stream) => {
|
||||
buildLogs.push({ at: new Date().toISOString(), message, stream });
|
||||
void this.releases
|
||||
.updateStatus(identity.brandId, identity.subject, taskId, { status: "building", buildLogs })
|
||||
.catch(() => undefined);
|
||||
});
|
||||
if (result.exitCode !== 0) {
|
||||
buildLogs.push({
|
||||
at: new Date().toISOString(),
|
||||
message: `构建失败,退出码 ${result.exitCode}`,
|
||||
stream: "stderr",
|
||||
});
|
||||
await this.releases.updateStatus(identity.brandId, identity.subject, taskId, {
|
||||
status: "build_failed",
|
||||
error: `小程序构建失败,请查看构建日志`,
|
||||
buildLogs,
|
||||
});
|
||||
throw new BadRequestException("小程序构建失败,请查看构建日志");
|
||||
}
|
||||
if (!existsSync(projectPath) || !existsSync(resolve(projectPath, "app.json"))) {
|
||||
buildLogs.push({
|
||||
at: new Date().toISOString(),
|
||||
message: `构建完成但缺少产物:${projectPath}/app.json`,
|
||||
stream: "stderr",
|
||||
});
|
||||
await this.releases.updateStatus(identity.brandId, identity.subject, taskId, {
|
||||
status: "build_failed",
|
||||
error: "小程序构建产物缺少 app.json",
|
||||
buildLogs,
|
||||
});
|
||||
throw new BadRequestException("小程序构建产物缺少 app.json");
|
||||
}
|
||||
buildLogs.push({
|
||||
at: new Date().toISOString(),
|
||||
message: "构建完成,开始上传微信",
|
||||
stream: "stdout",
|
||||
});
|
||||
await this.releases.updateStatus(identity.brandId, identity.subject, taskId, {
|
||||
status: "built",
|
||||
buildLogs,
|
||||
});
|
||||
const ci = (await import("miniprogram-ci")) as unknown as MiniprogramCi;
|
||||
const project = new ci.Project({
|
||||
projectPath,
|
||||
@@ -182,7 +243,7 @@ export class AdminMiniAppReleaseController {
|
||||
privateKey,
|
||||
});
|
||||
try {
|
||||
const result = await ci.upload({
|
||||
const uploadResult = await ci.upload({
|
||||
project,
|
||||
version: body.version ?? String(task.version),
|
||||
desc: body.desc ?? `brand content v${task.version}`,
|
||||
@@ -191,14 +252,16 @@ export class AdminMiniAppReleaseController {
|
||||
});
|
||||
return this.releases.updateStatus(identity.brandId, identity.subject, taskId, {
|
||||
status: "uploaded",
|
||||
buildLogs,
|
||||
weappUploadId:
|
||||
typeof result.strUint64Version === "string"
|
||||
? result.strUint64Version
|
||||
typeof uploadResult.strUint64Version === "string"
|
||||
? uploadResult.strUint64Version
|
||||
: String(task.version),
|
||||
});
|
||||
} catch (error) {
|
||||
await this.releases.updateStatus(identity.brandId, identity.subject, taskId, {
|
||||
status: "failed",
|
||||
buildLogs,
|
||||
error: error instanceof Error ? error.message : "微信代码上传失败",
|
||||
});
|
||||
throw error;
|
||||
@@ -226,6 +289,34 @@ export class AdminMiniAppReleaseController {
|
||||
return value;
|
||||
}
|
||||
|
||||
private buildWeApp(
|
||||
templateKey: string,
|
||||
onLog: (message: string, stream: "stdout" | "stderr") => void,
|
||||
): Promise<{ exitCode: number }> {
|
||||
return new Promise((resolvePromise, rejectPromise) => {
|
||||
const child = spawn("pnpm", ["--filter", "@personal-brand/taro", "build:weapp"], {
|
||||
cwd: resolve(process.cwd(), "../.."),
|
||||
env: { ...process.env, TEMPLATE_KEY: templateKey, TARO_ENV: "weapp" },
|
||||
shell: process.platform === "win32",
|
||||
}) as BuildProcess;
|
||||
const readStream = (stream: NodeJS.ReadableStream, name: "stdout" | "stderr") => {
|
||||
let buffer = "";
|
||||
stream.on("data", (chunk: Buffer | string) => {
|
||||
buffer += chunk.toString();
|
||||
for (const message of buffer.split("\n").slice(0, -1)) onLog(message, name);
|
||||
buffer = buffer.slice(buffer.lastIndexOf("\n") + 1);
|
||||
});
|
||||
stream.on("end", () => {
|
||||
if (buffer) onLog(buffer, name);
|
||||
});
|
||||
};
|
||||
readStream(child.stdout, "stdout");
|
||||
readStream(child.stderr, "stderr");
|
||||
child.on("close", (code) => resolvePromise({ exitCode: code ?? 1 }));
|
||||
child.on("error", () => rejectPromise(new Error("小程序构建进程启动失败")));
|
||||
});
|
||||
}
|
||||
|
||||
private jsonObject(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === "object" && !Array.isArray(value)
|
||||
? (value as Record<string, unknown>)
|
||||
|
||||
Reference in new issue
Block a user