diff options
| author | nsfisis <nsfisis@gmail.com> | 2023-03-15 01:57:11 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2023-03-15 02:14:35 +0900 |
| commit | 229f35a2f498e0dd1a5760abc88176043a9af449 (patch) | |
| tree | c3696b6603630a738fa885f22e28e38bc84b5755 /nuldoc-src | |
| parent | cfc3f6027d063ea41b54dce5aea07e4af78b5bad (diff) | |
| download | blog.nsfisis.dev-229f35a2f498e0dd1a5760abc88176043a9af449.tar.gz blog.nsfisis.dev-229f35a2f498e0dd1a5760abc88176043a9af449.tar.zst blog.nsfisis.dev-229f35a2f498e0dd1a5760abc88176043a9af449.zip | |
feat(nuldoc): implement `serve` command
Diffstat (limited to 'nuldoc-src')
| -rw-r--r-- | nuldoc-src/command.ts | 32 | ||||
| -rw-r--r-- | nuldoc-src/main.ts | 2 |
2 files changed, 32 insertions, 2 deletions
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); } |
