From 0db1c3b90a664f789efabc43a4bc09915590c2b7 Mon Sep 17 00:00:00 2001 From: sfja Date: Wed, 7 May 2025 13:16:26 +0200 Subject: [PATCH] add dev.ts --- deno.lock | 36 +++++++++++++++++++++++++++++- dev.ts | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 dev.ts diff --git a/deno.lock b/deno.lock index 571acf6..3ad6883 100644 --- a/deno.lock +++ b/deno.lock @@ -3,8 +3,15 @@ "specifiers": { "jsr:@luca/esbuild-deno-loader@0.11": "0.11.1", "jsr:@std/bytes@^1.0.2": "1.0.4", + "jsr:@std/cli@^1.0.6": "1.0.6", "jsr:@std/encoding@^1.0.5": "1.0.5", + "jsr:@std/fmt@^1.0.3": "1.0.7", + "jsr:@std/http@*": "1.0.10", + "jsr:@std/media-types@^1.1.0": "1.1.0", + "jsr:@std/net@^1.0.4": "1.0.4", "jsr:@std/path@^1.0.6": "1.0.8", + "jsr:@std/path@^1.0.8": "1.0.8", + "jsr:@std/streams@^1.0.8": "1.0.9", "npm:esbuild@0.20.2": "0.20.2" }, "jsr": { @@ -13,17 +20,44 @@ "dependencies": [ "jsr:@std/bytes", "jsr:@std/encoding", - "jsr:@std/path" + "jsr:@std/path@^1.0.6" ] }, "@std/bytes@1.0.4": { "integrity": "11a0debe522707c95c7b7ef89b478c13fb1583a7cfb9a85674cd2cc2e3a28abc" }, + "@std/cli@1.0.6": { + "integrity": "d22d8b38c66c666d7ad1f2a66c5b122da1704f985d3c47f01129f05abb6c5d3d" + }, "@std/encoding@1.0.5": { "integrity": "ecf363d4fc25bd85bd915ff6733a7e79b67e0e7806334af15f4645c569fefc04" }, + "@std/fmt@1.0.7": { + "integrity": "2a727c043d8df62cd0b819b3fb709b64dd622e42c3b1bb817ea7e6cc606360fb" + }, + "@std/http@1.0.10": { + "integrity": "4e32d11493ab04e3ef09f104f0cb9beb4228b1d4b47c5469573c2c294c0d3692", + "dependencies": [ + "jsr:@std/cli", + "jsr:@std/encoding", + "jsr:@std/fmt", + "jsr:@std/media-types", + "jsr:@std/net", + "jsr:@std/path@^1.0.8", + "jsr:@std/streams" + ] + }, + "@std/media-types@1.1.0": { + "integrity": "c9d093f0c05c3512932b330e3cc1fe1d627b301db33a4c2c2185c02471d6eaa4" + }, + "@std/net@1.0.4": { + "integrity": "2f403b455ebbccf83d8a027d29c5a9e3a2452fea39bb2da7f2c04af09c8bc852" + }, "@std/path@1.0.8": { "integrity": "548fa456bb6a04d3c1a1e7477986b6cffbce95102d0bb447c67c4ee70e0364be" + }, + "@std/streams@1.0.9": { + "integrity": "a9d26b1988cdd7aa7b1f4b51e1c36c1557f3f252880fa6cc5b9f37078b1a5035" } }, "npm": { diff --git a/dev.ts b/dev.ts new file mode 100644 index 0000000..e1e4b6f --- /dev/null +++ b/dev.ts @@ -0,0 +1,66 @@ +import { serveDir } from "jsr:@std/http/file-server"; +import { bundle } from "./bundle.ts"; + +function listening(addr: Addr) { + console.log(`Listening on http://${addr.hostname}:${addr.port}/`); +} + +type Addr = { + hostname: string; + port: number; +}; + +async function check() { + const command = new Deno.Command("deno", { + args: ["check", "src"], + stdout: "piped", + }); + const process = command.spawn(); + const output = await process.output(); + await Deno.stdout.write(output.stdout); +} + +async function watchAndBundle(addr: Addr) { + let changeOccurred = true; + let running = false; + setInterval(async () => { + if (!changeOccurred || running) { + return; + } + running = true; + console.clear(); + await bundle().catch((err) => console.error(`bundle: ${err}`)); + await check(); + listening(addr); + changeOccurred = false; + running = false; + }, 250); + const watcher = Deno.watchFs(["src", "static"]); + for await (const _ of watcher) { + changeOccurred = true; + } +} + +function serveBuild(addr: Addr) { + Deno.serve({ + port: addr.port, + hostname: addr.hostname, + onListen: (_) => listening(addr), + }, (req: Request) => { + return serveDir(req, { + fsRoot: "build", + urlRoot: "", + quiet: true, + }); + }); +} + +if (import.meta.main) { + const addr = { + hostname: "0.0.0.0", + port: 8432, + }; + await bundle(); + watchAndBundle(addr); + serveBuild(addr); +}