|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { createReadStream, existsSync, readFileSync, statSync } from "node:fs"; |
| 4 | +import { createServer } from "node:http"; |
| 5 | +import { dirname, extname, join, normalize, resolve } from "node:path"; |
| 6 | +import { fileURLToPath } from "node:url"; |
| 7 | + |
| 8 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 9 | +const rootDir = resolve(__dirname, ".."); |
| 10 | +const buildDir = resolve(rootDir, "build"); |
| 11 | +const packageJsonPath = resolve(rootDir, "package.json"); |
| 12 | +const packageVersion = JSON.parse(readFileSync(packageJsonPath, "utf8")).version; |
| 13 | + |
| 14 | +const mimeTypes = { |
| 15 | + ".css": "text/css; charset=utf-8", |
| 16 | + ".gif": "image/gif", |
| 17 | + ".html": "text/html; charset=utf-8", |
| 18 | + ".ico": "image/x-icon", |
| 19 | + ".jpeg": "image/jpeg", |
| 20 | + ".jpg": "image/jpeg", |
| 21 | + ".js": "text/javascript; charset=utf-8", |
| 22 | + ".json": "application/json; charset=utf-8", |
| 23 | + ".map": "application/json; charset=utf-8", |
| 24 | + ".png": "image/png", |
| 25 | + ".svg": "image/svg+xml", |
| 26 | + ".txt": "text/plain; charset=utf-8", |
| 27 | + ".webmanifest": "application/manifest+json; charset=utf-8", |
| 28 | + ".woff": "font/woff", |
| 29 | + ".woff2": "font/woff2" |
| 30 | +}; |
| 31 | + |
| 32 | +function printHelp() { |
| 33 | + console.log(`nullclaw-chat-ui ${packageVersion} |
| 34 | +
|
| 35 | +Usage: |
| 36 | + nullclaw-chat-ui run [--host <host>] [--port <port>] |
| 37 | + nullclaw-chat-ui version |
| 38 | + nullclaw-chat-ui help |
| 39 | +
|
| 40 | +Commands: |
| 41 | + run Serve the built UI from the local package. |
| 42 | + version Print the installed package version. |
| 43 | + help Show this help text. |
| 44 | +
|
| 45 | +Options for "run": |
| 46 | + --host Bind address (default: 127.0.0.1) |
| 47 | + --port Port number (default: 4173) |
| 48 | +`); |
| 49 | +} |
| 50 | + |
| 51 | +function parseRunOptions(args) { |
| 52 | + const options = { host: "127.0.0.1", port: 4173 }; |
| 53 | + |
| 54 | + for (let i = 0; i < args.length; i += 1) { |
| 55 | + const arg = args[i]; |
| 56 | + if (arg === "--host") { |
| 57 | + options.host = args[i + 1]; |
| 58 | + i += 1; |
| 59 | + } else if (arg === "--port") { |
| 60 | + const parsed = Number.parseInt(args[i + 1], 10); |
| 61 | + options.port = parsed; |
| 62 | + i += 1; |
| 63 | + } else if (arg === "-h" || arg === "--help") { |
| 64 | + options.help = true; |
| 65 | + } else { |
| 66 | + throw new Error(`Unknown option: ${arg}`); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (!options.help) { |
| 71 | + if (!options.host) { |
| 72 | + throw new Error("Missing value for --host"); |
| 73 | + } |
| 74 | + if (!Number.isInteger(options.port) || options.port < 1 || options.port > 65535) { |
| 75 | + throw new Error("Port must be an integer between 1 and 65535"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return options; |
| 80 | +} |
| 81 | + |
| 82 | +function resolveStaticFile(requestPath) { |
| 83 | + let decodedPath = "/"; |
| 84 | + try { |
| 85 | + decodedPath = decodeURIComponent(requestPath || "/"); |
| 86 | + } catch { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + const cleanPath = decodedPath.split("?")[0].split("#")[0].replace(/^\/+/, ""); |
| 91 | + const normalizedPath = normalize(cleanPath); |
| 92 | + if (normalizedPath.includes("\0") || normalizedPath.startsWith("..")) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + let candidate = join(buildDir, normalizedPath); |
| 97 | + try { |
| 98 | + if (statSync(candidate).isDirectory()) { |
| 99 | + candidate = join(candidate, "index.html"); |
| 100 | + } |
| 101 | + } catch { |
| 102 | + // Keep candidate as-is; we'll fall through to SPA fallback below. |
| 103 | + } |
| 104 | + |
| 105 | + if (existsSync(candidate)) { |
| 106 | + return candidate; |
| 107 | + } |
| 108 | + |
| 109 | + return join(buildDir, "index.html"); |
| 110 | +} |
| 111 | + |
| 112 | +function serveBuiltUi({ host, port }) { |
| 113 | + const entryHtml = join(buildDir, "index.html"); |
| 114 | + if (!existsSync(entryHtml)) { |
| 115 | + console.error("Build output was not found."); |
| 116 | + console.error("Run `npm run build` in this package, then retry `nullclaw-chat-ui run`."); |
| 117 | + process.exit(1); |
| 118 | + } |
| 119 | + |
| 120 | + const server = createServer((req, res) => { |
| 121 | + if (req.method !== "GET" && req.method !== "HEAD") { |
| 122 | + res.statusCode = 405; |
| 123 | + res.setHeader("Allow", "GET, HEAD"); |
| 124 | + res.end("Method Not Allowed"); |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + const filePath = resolveStaticFile(req.url || "/"); |
| 129 | + if (!filePath) { |
| 130 | + res.statusCode = 400; |
| 131 | + res.end("Bad Request"); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + const ext = extname(filePath).toLowerCase(); |
| 136 | + res.statusCode = 200; |
| 137 | + res.setHeader("Content-Type", mimeTypes[ext] || "application/octet-stream"); |
| 138 | + |
| 139 | + if (req.method === "HEAD") { |
| 140 | + res.end(); |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + const stream = createReadStream(filePath); |
| 145 | + stream.on("error", () => { |
| 146 | + res.statusCode = 500; |
| 147 | + res.end("Internal Server Error"); |
| 148 | + }); |
| 149 | + stream.pipe(res); |
| 150 | + }); |
| 151 | + |
| 152 | + server.listen(port, host, () => { |
| 153 | + console.log(`nullclaw-chat-ui running at http://${host}:${port}`); |
| 154 | + console.log("Press Ctrl+C to stop."); |
| 155 | + }); |
| 156 | + |
| 157 | + const shutdown = () => { |
| 158 | + server.close(() => process.exit(0)); |
| 159 | + }; |
| 160 | + process.on("SIGINT", shutdown); |
| 161 | + process.on("SIGTERM", shutdown); |
| 162 | +} |
| 163 | + |
| 164 | +function main() { |
| 165 | + const [command, ...rest] = process.argv.slice(2); |
| 166 | + |
| 167 | + if (!command || command === "help" || command === "-h" || command === "--help") { |
| 168 | + printHelp(); |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + if (command === "version" || command === "-v" || command === "--version") { |
| 173 | + console.log(packageVersion); |
| 174 | + return; |
| 175 | + } |
| 176 | + |
| 177 | + if (command === "run") { |
| 178 | + const options = parseRunOptions(rest); |
| 179 | + if (options.help) { |
| 180 | + printHelp(); |
| 181 | + return; |
| 182 | + } |
| 183 | + serveBuiltUi(options); |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + console.error(`Unknown command: ${command}`); |
| 188 | + console.error("Use `nullclaw-chat-ui help` for usage."); |
| 189 | + process.exit(1); |
| 190 | +} |
| 191 | + |
| 192 | +try { |
| 193 | + main(); |
| 194 | +} catch (error) { |
| 195 | + console.error(error instanceof Error ? error.message : String(error)); |
| 196 | + process.exit(1); |
| 197 | +} |
0 commit comments