62 lines
1.4 KiB
TypeScript

// deno-lint-ignore no-import-prefix
import { serveDir } from "jsr:@std/http@1.0.21/file-server";
type Addr = {
hostname: string;
port: number;
};
function alertListening(addr: Addr) {
console.log(`Listening on http://${addr.hostname}:${addr.port}/`);
}
async function buildDocs() {
console.log("Building docs...");
await new Deno.Command("deno", {
args: ["task", "build"],
cwd: "docs",
}).output();
}
async function watchAndBuildDocs(addr: Addr) {
let changeOccurred = true;
let running = false;
setInterval(async () => {
if (!changeOccurred || running) {
return;
}
running = true;
console.clear();
await buildDocs();
alertListening(addr);
changeOccurred = false;
running = false;
}, 250);
const watcher = Deno.watchFs(["docs"]);
for await (const ev of watcher) {
if (ev.paths.every((x) => x.includes("/docs/-/"))) {
continue;
}
changeOccurred = true;
}
}
function serveDist(addr: Addr) {
Deno.serve({
port: addr.port,
hostname: addr.hostname,
onListen: (_) => alertListening(addr),
}, (req: Request) => {
return serveDir(req, { quiet: true, showIndex: true });
});
}
if (import.meta.main) {
const addr = {
hostname: "0.0.0.0",
port: 8180,
};
watchAndBuildDocs(addr);
serveDist(addr);
}