60 lines
1.4 KiB
TypeScript
60 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();
|
|
console.log("Done!");
|
|
}
|
|
|
|
async function watchAndBuildDocs() {
|
|
let changeOccurred = true;
|
|
let running = false;
|
|
setInterval(async () => {
|
|
if (!changeOccurred || running) {
|
|
return;
|
|
}
|
|
running = true;
|
|
await buildDocs();
|
|
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) {
|
|
watchAndBuildDocs();
|
|
serveDist({
|
|
hostname: "0.0.0.0",
|
|
port: 8180,
|
|
});
|
|
}
|