|
| 1 | +import { rm, stat } from "node:fs/promises"; |
| 2 | +import type { ServeOptions } from "bun"; |
| 3 | +import * as path from "path"; |
| 4 | + |
| 5 | +import cosmosConfig from "./cosmos.config.json"; |
| 6 | + |
| 7 | +const PROJECT_ROOT = import.meta.dir; |
| 8 | +const BUILD_DIR = path.resolve(PROJECT_ROOT, "build"); |
| 9 | + |
| 10 | +const waitForCosmosImports = async () => { |
| 11 | + const fpath = `${PROJECT_ROOT}/cosmos.imports.ts`; |
| 12 | + try { |
| 13 | + const cosmosImports = await stat(fpath); |
| 14 | + if (!cosmosImports.isFile()) { |
| 15 | + throw new Error(` |
| 16 | + file doesnt exist yet |
| 17 | + `); |
| 18 | + } |
| 19 | + } catch { |
| 20 | + return new Promise((resolve) => { |
| 21 | + setTimeout(() => resolve(waitForCosmosImports()), 1000); |
| 22 | + }); |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +const buildApp = async () => |
| 27 | + rm(BUILD_DIR, { force: true, recursive: true }).then(() => |
| 28 | + Bun.build({ |
| 29 | + entrypoints: ["./cosmos.entrypoint.tsx"], |
| 30 | + target: "browser", |
| 31 | + outdir: "build", |
| 32 | + }) |
| 33 | + .then((output) => output) |
| 34 | + .catch((e) => { |
| 35 | + console.info("\n\n error in build", e); |
| 36 | + }) |
| 37 | + ); |
| 38 | + |
| 39 | +await waitForCosmosImports(); |
| 40 | +await buildApp().then((output) => { |
| 41 | + if (output.success) |
| 42 | + console.info( |
| 43 | + `app built: ${output.success}; ${output.outputs.length} files ` |
| 44 | + ); |
| 45 | + else { |
| 46 | + for (const message of output.logs) { |
| 47 | + // Bun will pretty print the message object |
| 48 | + console.error(message); |
| 49 | + } |
| 50 | + throw new Error(`build failed`); |
| 51 | + } |
| 52 | +}); |
| 53 | + |
| 54 | +const returnIndex = () => { |
| 55 | + const index = ` |
| 56 | + <!DOCTYPE html> |
| 57 | + <html lang="en"> |
| 58 | + <body> |
| 59 | + <script src="${BUILD_DIR}/cosmos.entrypoint.js" type="module"> |
| 60 | + </script> |
| 61 | + </body> |
| 62 | + </html> |
| 63 | + `; |
| 64 | + |
| 65 | + return new Response(index, { |
| 66 | + headers: { |
| 67 | + "Content-Type": "text/html", |
| 68 | + "Access-Control-Allow-Origin": "*", |
| 69 | + }, |
| 70 | + }); |
| 71 | +}; |
| 72 | + |
| 73 | +async function serveFromDir(config: { |
| 74 | + directory?: string; |
| 75 | + path: string; |
| 76 | +}): Promise<Response | null> { |
| 77 | + const filepath = path.join(config.directory || "", config.path); |
| 78 | + |
| 79 | + try { |
| 80 | + const fd = await stat(filepath); |
| 81 | + if (fd && fd.isFile()) { |
| 82 | + return new Response(Bun.file(filepath), { |
| 83 | + headers: { "Access-Control-Allow-Origin": "*" }, |
| 84 | + }); |
| 85 | + } |
| 86 | + } catch (err) {} |
| 87 | + |
| 88 | + return null; |
| 89 | +} |
| 90 | +export default { |
| 91 | + port: cosmosConfig.rendererUrl.split(":").pop(), |
| 92 | + hostname: "0.0.0.0", |
| 93 | + async fetch(req) { |
| 94 | + const reqPath = new URL(req.url).pathname; |
| 95 | + console.log(req.method, reqPath); |
| 96 | + |
| 97 | + if (reqPath === "/") return returnIndex(); |
| 98 | + else { |
| 99 | + const filepath = req.url.replace(cosmosConfig.rendererUrl, ""); |
| 100 | + |
| 101 | + const exactResponse = await serveFromDir({ path: filepath }); |
| 102 | + if (exactResponse) return exactResponse; |
| 103 | + |
| 104 | + const buildResponse = await serveFromDir({ |
| 105 | + directory: BUILD_DIR, |
| 106 | + path: filepath, |
| 107 | + }); |
| 108 | + if (buildResponse) return buildResponse; |
| 109 | + |
| 110 | + return new Response("File not found", { |
| 111 | + status: 404, |
| 112 | + }); |
| 113 | + } |
| 114 | + }, |
| 115 | +} satisfies ServeOptions; |
| 116 | + |
| 117 | +// watch imports |
| 118 | +await import("./cosmos.imports.ts").catch((e) => e); |
0 commit comments