From 229f35a2f498e0dd1a5760abc88176043a9af449 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Wed, 15 Mar 2023 01:57:11 +0900 Subject: feat(nuldoc): implement `serve` command --- nuldoc-src/command.ts | 32 +++++++++++++++++++++++++++++++- nuldoc-src/main.ts | 2 +- 2 files changed, 32 insertions(+), 2 deletions(-) (limited to 'nuldoc-src') diff --git a/nuldoc-src/command.ts b/nuldoc-src/command.ts index dd15a77..d07f262 100644 --- a/nuldoc-src/command.ts +++ b/nuldoc-src/command.ts @@ -1,6 +1,8 @@ import { dirname, join, joinGlobs } from "std/path/mod.ts"; import { ensureDir } from "std/fs/mod.ts"; import { expandGlob } from "std/fs/expand_glob.ts"; +import { serveDir } from "std/http/file_server.ts"; +import { serve } from "std/http/server.ts"; import { Config } from "./config.ts"; import { parseDocBookFile } from "./docbook/parse.ts"; import { writeHtmlFile } from "./html.ts"; @@ -9,13 +11,41 @@ import convertPost from "./templates/post.ts"; import convertPostList from "./templates/post_list.ts"; import convertTag from "./templates/tag.ts"; -export async function run(config: Config) { +export async function run(command: string, config: Config) { + if (command === "build") { + await cmdBuild(config); + } else if (command === "serve") { + cmdServe(config); + } else { + console.error(`Unknown command: ${command}`); + } +} + +async function cmdBuild(config: Config) { const posts = await generatePosts(config); await generateTags(posts, config); await generatePostList(posts, config); await copyStaticFiles(config); } +function cmdServe(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"); + } + return serveDir(req, { + fsRoot: rootDir, + showIndex: true, + }); + }); +} + async function generatePosts(config: Config) { const sourceDir = join(Deno.cwd(), config.locations.contentDir, "posts"); const postFiles = await collectPostFiles(sourceDir); diff --git a/nuldoc-src/main.ts b/nuldoc-src/main.ts index 12a4ca9..c7c175b 100644 --- a/nuldoc-src/main.ts +++ b/nuldoc-src/main.ts @@ -2,5 +2,5 @@ import { config } from "./config.ts"; import { run } from "./command.ts"; if (import.meta.main) { - await run(config); + await run(Deno.args[0] ?? "build", config); } -- cgit v1.2.3-70-g09d2