aboutsummaryrefslogtreecommitdiffhomepage
path: root/nuldoc-src/commands/serve.ts
blob: ffb2020c99944097e4f21f29f7f5ed7ed3144ac5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { serveDir } from "std/http/file_server.ts";
import { Status, STATUS_TEXT } from "std/http/http_status.ts";
import { serve } from "std/http/server.ts";
import { join } from "std/path/mod.ts";
import { Config } from "../config.ts";

export function runServeCommand(config: Config) {
  const rootDir = join(Deno.cwd(), config.locations.destDir);
  serve(async (req) => {
    const pathname = new URL(req.url).pathname;
    if (!pathname.endsWith("css") && !pathname.endsWith("svg")) {
      const p = Deno.run({
        cmd: ["./nuldoc", "build"],
      });
      await p.status();
      console.log("rebuild");
    }
    const res = await serveDir(req, {
      fsRoot: rootDir,
      showIndex: true,
    });
    if (res.status !== Status.NotFound) {
      return res;
    }

    const notFoundHtml = await Deno.readTextFile(join(rootDir, "404.html"));
    return new Response(notFoundHtml, {
      status: Status.NotFound,
      statusText: STATUS_TEXT[Status.NotFound],
      headers: {
        "content-type": "text/html",
      },
    });
  });
}