示例工程 57-project-release-pipeline
57-project-release-pipeline/app/src/App.ts
import type { Check, Snapshot } from "../shared/ipc";
import type { Artifact, StepResult } from "../shared/pipeline";
function escapeHtml(text: string): string {
return text.replace(/[&<>"]/g, (char) => {
if (char === "&") return "&";
if (char === "<") return "<";
if (char === ">") return ">";
return """;
});
}
function time(at: number): string {
return new Date(at).toLocaleTimeString("zh-CN", { hour12: false });
}
function bytes(value: number): string {
return value < 1024 ? `${value} B` : `${(value / 1024).toFixed(1)} KB`;
}
function statusTag(status: StepResult["status"]): string {
if (status === "pass") return '<span class="tag ok">PASS</span>';
if (status === "fail") return '<span class="tag bad">FAIL</span>';
if (status === "running") return '<span class="tag warn">RUN</span>';
return '<span class="tag">PENDING</span>';
}
function stepRow(step: StepResult, index: number): string {
return `<li class="step-row" data-testid="step-row" data-step="${step.id}">
<span class="step-no">${index + 1}</span>
<div class="step-main">
<div class="step-head">
<b>${escapeHtml(step.title)}</b>
${statusTag(step.status)}
<span class="step-ms">${step.ms ? `${step.ms}ms` : "—"}</span>
</div>
<code class="step-log">${escapeHtml(step.log || "等待执行")}</code>
</div>
</li>`;
}
function artifactRow(row: Artifact): string {
return `<tr data-testid="artifact-row">
<td><code>${escapeHtml(row.name)}</code></td>
<td>${bytes(row.bytes)}</td>
<td><code>${escapeHtml(row.sha256.slice(0, 16))}…</code></td>
</tr>`;
}
function checkRow(check: Check): string {
return `<li class="check-row" data-testid="check-row">
<span class="tag ${check.pass ? "ok" : "bad"}">${check.pass ? "PASS" : "FAIL"}</span>
<span>${escapeHtml(check.label)}</span>
<code>${escapeHtml(check.detail)}</code>
</li>`;
}
export function mountApp(root: HTMLElement): void {
root.innerHTML = `
<div class="app">
<header class="header">
<div class="brand">
<span class="logo">R</span>
<div>
<h1>发布流水线实验室</h1>
<span class="muted small">预检 → 哈希 → 元数据 → feed → 报告(本地离线)</span>
</div>
</div>
<div class="header-side">
<span class="pill ok-pill">offline</span>
<span class="pill ok-pill" data-testid="score">—</span>
<button class="btn" data-testid="inject">注入失败</button>
<button class="btn primary" data-testid="run">运行流水线</button>
</div>
</header>
<div class="banner" data-testid="banner">—</div>
<main class="layout">
<div class="column">
<section class="card pad pipeline-card">
<div class="card-head">
<h2>流水线</h2>
<span class="muted small" data-testid="step-summary">—</span>
</div>
<ul class="steps" data-testid="steps"></ul>
</section>
<section class="card pad report-card">
<div class="card-head">
<h2>发布报告</h2>
<span class="muted small">JSON</span>
</div>
<ul class="report" data-testid="report"></ul>
</section>
</div>
<div class="column">
<section class="card pad artifacts-card">
<div class="card-head">
<h2>产物与哈希</h2>
<span class="muted small" data-testid="artifact-count">—</span>
</div>
<table class="matrix">
<thead><tr><th>文件</th><th>大小</th><th>SHA-256</th></tr></thead>
<tbody data-testid="artifacts"></tbody>
</table>
</section>
<section class="card pad feed-card">
<div class="card-head">
<h2>生成的 feed</h2>
<span class="muted small">releases.json</span>
</div>
<pre class="feed-raw" data-testid="feed"></pre>
</section>
<section class="card pad checks-card">
<div class="card-head">
<h2>流水线自检</h2>
<span class="muted small" data-testid="check-count">—</span>
</div>
<ul class="checks" data-testid="checks"></ul>
</section>
<section class="card pad">
<div class="card-head">
<h2>活动</h2>
<span class="muted small">步骤 / 失败</span>
</div>
<ul class="activity" data-testid="events"></ul>
</section>
</div>
</main>
</div>
`;
const score = root.querySelector<HTMLElement>('[data-testid="score"]');
const banner = root.querySelector<HTMLElement>('[data-testid="banner"]');
const stepSummary = root.querySelector<HTMLElement>(
'[data-testid="step-summary"]',
);
const stepsEl = root.querySelector<HTMLElement>('[data-testid="steps"]');
const reportEl = root.querySelector<HTMLElement>('[data-testid="report"]');
const artifactCount = root.querySelector<HTMLElement>(
'[data-testid="artifact-count"]',
);
const artifactsEl = root.querySelector<HTMLElement>(
'[data-testid="artifacts"]',
);
const feedEl = root.querySelector<HTMLElement>('[data-testid="feed"]');
const checksEl = root.querySelector<HTMLElement>('[data-testid="checks"]');
const checkCount = root.querySelector<HTMLElement>(
'[data-testid="check-count"]',
);
const eventsEl = root.querySelector<HTMLElement>('[data-testid="events"]');
let state: Snapshot | null = null;
function renderSteps(): void {
if (!state) return;
if (stepsEl) stepsEl.innerHTML = state.steps.map(stepRow).join("");
if (stepSummary) {
const summary = state.reportSummary;
stepSummary.textContent = `${summary.passed}/${state.steps.length} 通过 · ${summary.failed} 失败 · ${summary.pending} 等待`;
}
if (banner) {
banner.textContent =
state.reportSummary.failed > 0
? `流水线在失败步骤停止:${state.steps.find((step) => step.status === "fail")?.title ?? ""}`
: `流水线通过:${state.currentVersion} → ${state.targetVersion},报告已写入`;
banner.className = `banner ${state.reportSummary.failed ? "warn" : "ok"}`;
}
}
function renderReport(): void {
if (!state || !reportEl) return;
reportEl.innerHTML = `
<li><span>报告路径</span><code>${escapeHtml(state.reportPath)}</code></li>
<li><span>当前版本</span><b>${escapeHtml(state.currentVersion)}</b></li>
<li><span>目标版本</span><b>${escapeHtml(state.targetVersion)}</b></li>
<li><span>步骤</span><b>${state.steps.length}</b></li>
<li><span>产物</span><b>${state.artifacts.length}</b></li>`;
}
function renderArtifacts(): void {
if (!state) return;
if (artifactsEl) {
artifactsEl.innerHTML = state.artifacts.map(artifactRow).join("");
}
if (artifactCount) {
artifactCount.textContent = `${state.artifacts.length} 个文件`;
}
if (feedEl) {
feedEl.textContent = state.feedPreview || "(尚未生成)";
}
}
function renderChecks(): void {
if (!state) return;
if (checksEl) checksEl.innerHTML = state.checks.map(checkRow).join("");
if (checkCount) {
const passed = state.checks.filter((check) => check.pass).length;
checkCount.textContent = `${passed}/${state.checks.length} 通过`;
}
if (score) {
score.textContent = `${state.score} / 100`;
score.className = `pill ${state.score === 100 ? "ok-pill" : "bad-pill"}`;
}
}
function renderEvents(): void {
if (!eventsEl || !state) return;
eventsEl.innerHTML =
state.events.length === 0
? `<li class="muted small">暂无活动</li>`
: state.events
.slice(0, 8)
.map(
(row) => `
<li class="event-row" data-testid="event-row">
<span class="muted">${time(row.at)}</span>
<span class="kind">${escapeHtml(row.kind)}</span>
<span>${escapeHtml(row.detail)}</span>
</li>`,
)
.join("");
}
function render(next: Snapshot): void {
state = next;
renderSteps();
renderReport();
renderArtifacts();
renderChecks();
renderEvents();
}
void window.api.pipeline.snapshot().then(render);
window.api.pipeline.onEvent(() => {
void window.api.pipeline.snapshot().then(render);
});
root
.querySelector<HTMLButtonElement>('[data-testid="run"]')
?.addEventListener("click", async () => {
render(await window.api.pipeline.run());
});
root
.querySelector<HTMLButtonElement>('[data-testid="inject"]')
?.addEventListener("click", async () => {
render(await window.api.pipeline.inject());
});
window.__prepareShots = async (stage) => {
document.body.dataset.shot = stage;
if (stage === "failure") {
render(await window.api.pipeline.inject());
document
.querySelector(".pipeline-card")
?.scrollIntoView({ block: "start" });
return;
}
render(await window.api.pipeline.run());
if (stage === "artifacts") {
document
.querySelector(".artifacts-card")
?.scrollIntoView({ block: "start" });
} else {
document
.querySelector(".pipeline-card")
?.scrollIntoView({ block: "start" });
}
};
window.__demo = async () => {
const next = await window.api.pipeline.snapshot();
state = next;
return {
score: next.score,
checks: next.checks.length,
passed: next.checks.filter((check) => check.pass).length,
steps: next.steps.length,
passedSteps: next.steps.filter((step) => step.status === "pass").length,
failedSteps: next.steps.filter((step) => step.status === "fail").length,
pendingSteps: next.steps.filter((step) => step.status === "pending")
.length,
artifacts: next.artifacts.length,
hashes: next.artifacts.filter((row) => row.sha256.length === 64).length,
feedBytes: next.feedPreview.length,
reportExists:
next.checks.find((check) => check.id === "report")?.pass ?? false,
reportSteps:
next.checks.find((check) => check.id === "report-steps")?.detail === "5"
? 5
: 0,
decision:
next.steps.find((step) => step.id === "feed")?.status === "pass"
? "update"
: "none",
noNetwork:
next.checks.find((check) => check.id === "offline")?.pass ?? false,
};
};
}