|
| 1 | +// Builds the managed-auth MCP App into a single self-contained HTML bundle. |
| 2 | +// The --check mode is byte-exact, and Bun's minifier output can change between |
| 3 | +// releases, so the bundle is only reproducible with the exact Bun dependency |
| 4 | +// pinned in package.json and matched by CI (currently 1.3.3). Production build |
| 5 | +// checks the committed artifact; regeneration remains an explicit command: |
| 6 | +// bun run build:managed-auth-app |
| 7 | +import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; |
| 8 | +import { tmpdir } from "node:os"; |
| 9 | +import { join, resolve } from "node:path"; |
| 10 | + |
| 11 | +const EXPECTED_BUN_VERSION = "1.3.3"; |
| 12 | +if (Bun.version !== EXPECTED_BUN_VERSION) { |
| 13 | + throw new Error( |
| 14 | + `Managed-auth App bundling requires Bun ${EXPECTED_BUN_VERSION}; got ${Bun.version}. Run bun install and use the project-local bun binary.`, |
| 15 | + ); |
| 16 | +} |
| 17 | + |
| 18 | +const root = resolve(import.meta.dirname, ".."); |
| 19 | +const entrypoint = join(root, "src/lib/mcp/apps/managed-auth-entry.tsx"); |
| 20 | +const generatedPath = join( |
| 21 | + root, |
| 22 | + "src/lib/mcp/apps/generated/managed-auth-app.ts", |
| 23 | +); |
| 24 | +const check = process.argv.includes("--check"); |
| 25 | +const temp = await mkdtemp(join(tmpdir(), "kernel-managed-auth-app-")); |
| 26 | + |
| 27 | +try { |
| 28 | + const build = await Bun.build({ |
| 29 | + entrypoints: [entrypoint], |
| 30 | + outdir: temp, |
| 31 | + target: "browser", |
| 32 | + format: "esm", |
| 33 | + minify: true, |
| 34 | + splitting: false, |
| 35 | + sourcemap: "none", |
| 36 | + define: { |
| 37 | + "process.env.NODE_ENV": JSON.stringify("production"), |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + if (!build.success) { |
| 42 | + for (const log of build.logs) console.error(log); |
| 43 | + process.exitCode = 1; |
| 44 | + } else { |
| 45 | + let javascript = ""; |
| 46 | + let css = ""; |
| 47 | + for (const output of build.outputs) { |
| 48 | + if (output.path.endsWith(".js")) javascript += await output.text(); |
| 49 | + if (output.path.endsWith(".css")) css += await output.text(); |
| 50 | + } |
| 51 | + if (!javascript) |
| 52 | + throw new Error("Bun did not emit managed-auth JavaScript"); |
| 53 | + |
| 54 | + const escapeScript = (value) => value.replaceAll("</script", "<\\/script"); |
| 55 | + const escapeStyle = (value) => value.replaceAll("</style", "<\\/style"); |
| 56 | + const html = `<!DOCTYPE html> |
| 57 | +<html lang="en"> |
| 58 | +<head> |
| 59 | +<meta charset="utf-8" /> |
| 60 | +<meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 61 | +<title>Kernel Managed Authentication</title> |
| 62 | +<style>${escapeStyle(css)} |
| 63 | +html,body,#root{margin:0;min-height:100%} |
| 64 | +html,body{background:transparent} |
| 65 | +.kernel-app-status,.kernel-app-fallback{font-family:ui-sans-serif,system-ui,sans-serif;padding:16px;text-align:center} |
| 66 | +.kernel-app-actions{display:flex;flex-direction:column;align-items:center;gap:8px;padding:0 16px 16px} |
| 67 | +.kernel-app-button{appearance:none;border:0;border-radius:8px;background:#81b300;color:#fff;cursor:pointer;font:600 14px ui-sans-serif,system-ui,sans-serif;padding:10px 16px} |
| 68 | +.kernel-app-button:hover{background:#709c00} |
| 69 | +</style> |
| 70 | +</head> |
| 71 | +<body><div id="root"></div> |
| 72 | +<script type="module">${escapeScript(javascript)}</script> |
| 73 | +</body> |
| 74 | +</html>`; |
| 75 | + const generated = `// Generated by scripts/build-managed-auth-app.mjs. Do not edit.\nexport const MANAGED_AUTH_APP_HTML = ${JSON.stringify(html)};\n`; |
| 76 | + |
| 77 | + if (check) { |
| 78 | + let current = ""; |
| 79 | + try { |
| 80 | + current = await readFile(generatedPath, "utf8"); |
| 81 | + } catch { |
| 82 | + // Report the same actionable stale-bundle error below. |
| 83 | + } |
| 84 | + if (current !== generated) { |
| 85 | + console.error( |
| 86 | + "Managed-auth App bundle is stale. Run: bun run build:managed-auth-app", |
| 87 | + ); |
| 88 | + process.exitCode = 1; |
| 89 | + } |
| 90 | + } else { |
| 91 | + await mkdir(resolve(generatedPath, ".."), { recursive: true }); |
| 92 | + await writeFile(generatedPath, generated); |
| 93 | + console.log(`Generated ${generatedPath}`); |
| 94 | + } |
| 95 | + } |
| 96 | +} finally { |
| 97 | + await rm(temp, { recursive: true, force: true }); |
| 98 | +} |
0 commit comments