#!/usr/bin/env bun /** * PPTX Preview — long-running server process. * Watches the directory containing a .pptx file for changes, converts to * PDF via LibreOffice, serves in the browser's native viewer with WebSocket * refresh. * * Not meant to be run directly — use preview.ts to start/stop. * * SCOPE: for the MiMoCode CLI only. GUI hosts embedding MiMoCode via * the SDK should use their own native preview mechanism instead. * * PATHS: this script sits in the skill's bundle directory, not * necessarily in your cwd. Invoke via the absolute path * (/scripts/preview_server.ts). * * Usage: * bun run /scripts/preview_server.ts /path/to/output.pptx --port 4200 */ import { watch } from "node:fs" import { resolve, basename, dirname } from "node:path" import { mkdirSync, existsSync } from "node:fs" import { parseArgs } from "node:util" const { values, positionals } = parseArgs({ args: Bun.argv.slice(2), options: { port: { type: "string", default: "4200" } }, allowPositionals: true, }) const source = positionals[0] if (!source) { console.error("Usage: bun run /scripts/preview_server.ts /path/to/file.pptx [--port N]") process.exit(2) } const pptxPath = resolve(source) if (!existsSync(pptxPath)) { console.error(`File not found: ${pptxPath}`) process.exit(1) } const port = parseInt(values.port!, 10) const previewDir = resolve(dirname(pptxPath), ".pptx-preview") mkdirSync(previewDir, { recursive: true }) const pdfPath = resolve(previewDir, basename(pptxPath, ".pptx") + ".pdf") function findSoffice(): string { const candidates = [ "/Applications/LibreOffice.app/Contents/MacOS/soffice", "/usr/bin/soffice", "/usr/bin/libreoffice", "/usr/local/bin/soffice", ] for (const c of candidates) { if (existsSync(c)) return c } const which = Bun.spawnSync(["which", "soffice"]) if (which.exitCode === 0) return which.stdout.toString().trim() console.error("soffice not found. Install LibreOffice.") process.exit(1) } const soffice = findSoffice() const sofficeProfile = resolve(previewDir, "libreoffice_profile") mkdirSync(sofficeProfile, { recursive: true }) let converting = false let pendingConvert = false let debounceTimer: ReturnType | null = null const clients = new Set<{ send(msg: string): void }>() async function convertToPdf() { if (converting) { pendingConvert = true; return } converting = true broadcast(JSON.stringify({ status: "converting" })) if (!await Bun.file(pptxPath).exists()) { converting = false; return } const proc = Bun.spawn([ soffice, "--headless", "--invisible", "--norestore", `-env:UserInstallation=file://${sofficeProfile}`, "--convert-to", "pdf", "--outdir", previewDir, pptxPath, ], { stdout: "pipe", stderr: "pipe" }) await proc.exited converting = false if (proc.exitCode === 0 && existsSync(pdfPath)) { broadcast(JSON.stringify({ status: "updated", ts: Date.now() })) } else { const err = await new Response(proc.stderr).text() broadcast(JSON.stringify({ status: "error", message: err.slice(0, 200) })) } if (pendingConvert) { pendingConvert = false; scheduleConvert() } } function broadcast(msg: string) { for (const ws of clients) ws.send(msg) } function scheduleConvert() { if (debounceTimer) clearTimeout(debounceTimer) debounceTimer = setTimeout(convertToPdf, 800) } watch(dirname(pptxPath), (_, filename) => { if (filename === basename(pptxPath)) scheduleConvert() }) const HTML = ` PPTX Preview
Watching — ${basename(pptxPath)}
`; Bun.serve({ port, fetch(req, server) { const url = new URL(req.url) if (url.pathname === "/ws") { if (server.upgrade(req)) return undefined return new Response("WebSocket upgrade failed", { status: 400 }) } if (url.pathname === "/preview.pdf") { if (!existsSync(pdfPath)) return new Response("Not ready — waiting for first conversion", { status: 404 }) return new Response(Bun.file(pdfPath), { headers: { "Content-Type": "application/pdf", "Cache-Control": "no-store" } }) } return new Response(HTML, { headers: { "Content-Type": "text/html" } }) }, websocket: { open(ws) { clients.add(ws) }, close(ws) { clients.delete(ws) }, message() {}, }, }) console.log(`PPTX Preview Server running`) console.log(` Watching: ${pptxPath}`) console.log(` URL: http://localhost:${port}`) process.on("SIGTERM", () => process.exit(0)) process.on("SIGINT", () => process.exit(0)) // Initial conversion convertToPdf()