import type { Check, Snapshot } from "../shared/ipc"; import type { ArtifactRow, Layer, Measurements, ToolRow, } from "../shared/targets"; 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 size(bytes: number): string { if (bytes <= 0) return "未实测"; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; return `${(bytes / 1024 / 1024).toFixed(1)} MB`; } function checkRow(check: Check): string { return `
  • ${check.pass ? "PASS" : "FAIL"} ${escapeHtml(check.label)} ${escapeHtml(check.detail)}
  • `; } function layerRow(layer: Layer, index: number): string { return `
  • ${index + 1}
    ${escapeHtml(layer.title)} → ${escapeHtml(layer.output)}

    ${escapeHtml(layer.note)}

  • `; } function artifactRow(row: ArtifactRow): string { return ` ${escapeHtml(row.id)} ${escapeHtml(row.platform)} ${escapeHtml(row.installer)} ${row.updateSource ? "是" : "否"} ${escapeHtml(row.tool)} `; } function toolRow(row: ToolRow): string { return ` ${escapeHtml(row.name)} ${escapeHtml(row.configLines)} ${escapeHtml(row.targets)} ${escapeHtml(row.update)} `; } function measureRow(label: string, bytes: number, max: number): string { const width = max > 0 ? Math.max(3, Math.round((bytes / max) * 100)) : 3; return `
  • ${escapeHtml(label)}
    ${size(bytes)}
  • `; } export function mountApp(root: HTMLElement): void { root.innerHTML = `

    打包目标与产物

    package · make · distribute · update
    macOS 实测 —

    四个层次

    package → make → distribute → update

      产物形态

      —
      id平台安装器更新源工具

      macOS 实测

      —

          工具对照

          Forge / electron-builder / packager
          工具配置量目标更新

          electron-builder 仅在第 54 章对照;本教程主线仍是 Forge。

          矩阵自检

          —

            活动

            读取 / 实测
              `; const score = root.querySelector('[data-testid="score"]'); const banner = root.querySelector('[data-testid="banner"]'); const artifactCount = root.querySelector( '[data-testid="artifact-count"]', ); const layersEl = root.querySelector('[data-testid="layers"]'); const artifactsEl = root.querySelector( '[data-testid="artifacts"]', ); const measuredAt = root.querySelector( '[data-testid="measured-at"]', ); const measuresEl = root.querySelector( '[data-testid="measures"]', ); const measureMeta = root.querySelector( '[data-testid="measure-meta"]', ); const measureNote = root.querySelector( '[data-testid="measure-note"]', ); const toolsEl = root.querySelector('[data-testid="tools"]'); const checksEl = root.querySelector('[data-testid="checks"]'); const checkCount = root.querySelector( '[data-testid="check-count"]', ); const eventsEl = root.querySelector('[data-testid="events"]'); let state: Snapshot | null = null; function renderLayers(): void { if (!state) return; if (layersEl) { layersEl.innerHTML = state.layers.map(layerRow).join(""); } if (artifactsEl) { artifactsEl.innerHTML = state.artifacts.map(artifactRow).join(""); } if (artifactCount) { artifactCount.textContent = `${state.artifacts.length} 项 · ${state.artifacts.filter((row) => row.updateSource).length} 个更新源`; } if (banner) { const m = state.measurements; banner.textContent = m.appBytes > 0 ? `${m.appName} · ${m.arch} · app ${size(m.appBytes)} / asar ${size(m.asarBytes)} / zip ${size(m.zipBytes)} / dmg ${size(m.dmgBytes)}` : "尚未读取到实测产物;先运行 pnpm package 再刷新"; banner.className = `banner ${m.appBytes > 0 ? "ok" : "warn"}`; } } function renderMeasures(): void { if (!state) return; const m: Measurements = state.measurements; if (measuredAt) { measuredAt.textContent = m.measuredAt || "未记录"; } if (measuresEl) { const max = Math.max(m.appBytes, m.zipBytes, m.dmgBytes, 1); measuresEl.innerHTML = [ measureRow("app 目录", m.appBytes, max), measureRow("app.asar", m.asarBytes, max), measureRow("zip", m.zipBytes, max), measureRow("dmg", m.dmgBytes, max), ].join(""); } if (measureMeta) { measureMeta.innerHTML = `
            • arch${escapeHtml(m.arch || "—")}
            • appName${escapeHtml(m.appName)}
            • `; } if (measureNote) measureNote.textContent = m.note; } function renderTools(): void { if (!state || !toolsEl) return; toolsEl.innerHTML = state.tools.map(toolRow).join(""); } 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 ? `
            • 暂无活动
            • ` : state.events .slice(0, 8) .map( (row) => `
            • ${time(row.at)} ${escapeHtml(row.kind)} ${escapeHtml(row.detail)}
            • `, ) .join(""); } function render(next: Snapshot): void { state = next; renderLayers(); renderMeasures(); renderTools(); renderChecks(); renderEvents(); } void window.api.packaging.snapshot().then(render); window.api.packaging.onEvent(() => { void window.api.packaging.snapshot().then(render); }); root .querySelector('[data-testid="refresh"]') ?.addEventListener("click", async () => { render(await window.api.packaging.refresh()); }); window.__prepareShots = async (stage) => { document.body.dataset.shot = stage; render(await window.api.packaging.refresh()); if (stage === "macos") { document .querySelector(".measure-card") ?.scrollIntoView({ block: "start" }); } else if (stage === "tools") { document.querySelector(".tools-card")?.scrollIntoView({ block: "start" }); } else { document .querySelector(".layers-card") ?.scrollIntoView({ block: "start" }); } }; window.__demo = async () => { const next = await window.api.packaging.snapshot(); state = next; return { score: next.score, checks: next.checks.length, passed: next.checks.filter((check) => check.pass).length, layers: next.layers.length, artifacts: next.artifacts.length, macRows: next.artifacts.filter((row) => row.platform === "macOS").length, updateRows: next.artifacts.filter((row) => row.updateSource).length, appBytes: next.measurements.appBytes, asarBytes: next.measurements.asarBytes, zipBytes: next.measurements.zipBytes, dmgBytes: next.measurements.dmgBytes, arch: next.measurements.arch, tools: next.tools.length, }; }; }