about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorAlan Pearce2024-04-01 19:40:45 +0200
committerAlan Pearce2024-04-01 19:41:36 +0200
commited1d0c35da6be9d9aadbdacb38fff36b90742de1 (patch)
tree396c11eada635b61a2a7e3f3ca7db07a8fdd8161 /src
parent038ba7ae4f6b56ae334d2351ac810c1ef23d8705 (diff)
downloadwebsite-ed1d0c35da6be9d9aadbdacb38fff36b90742de1.tar.lz
website-ed1d0c35da6be9d9aadbdacb38fff36b90742de1.tar.zst
website-ed1d0c35da6be9d9aadbdacb38fff36b90742de1.zip
Simplify walkDirectory by using recursive readdir
Diffstat (limited to 'src')
-rw-r--r--src/app.ts24
1 files changed, 9 insertions, 15 deletions
diff --git a/src/app.ts b/src/app.ts
index ac11c4d..e0fe2e8 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -1,5 +1,6 @@
 import path from "node:path";
-import fs, { Stats } from "node:fs";
+import fs from "node:fs/promises";
+import type { Stats } from "node:fs";
 import type { BunFile, Serve } from "bun";
 import * as Sentry from "@sentry/node";
 import prom from "bun-prometheus-client";
@@ -73,21 +74,14 @@ function registerFile(
   });
 }
 
-function walkDirectory(root: string, dir: string) {
-  const absDir = path.join(root, dir);
-  for (let pathname of fs.readdirSync(absDir)) {
-    const relPath = path.join(dir, pathname);
-    const absPath = path.join(absDir, pathname);
-    const stat = fs.statSync(absPath);
-    if (stat.isDirectory()) {
-      walkDirectory(root, relPath + path.sep);
-    } else if (stat.isFile()) {
-      if (pathname.startsWith("index.html")) {
+async function walkDirectory(root: string) {
+  for (let relPath of await fs.readdir(root, { recursive: true })) {
+    const absPath = path.join(root, relPath);
+    const stat = await fs.stat(absPath);
+    if (stat.isFile()) {
+      if (relPath.endsWith("index.html")) {
         const dir = relPath.replace("index.html", "");
         registerFile(relPath, dir, absPath, stat);
-        if (dir !== "") {
-          registerFile(relPath, dir + path.sep, absPath, stat);
-        }
       } else {
         registerFile(relPath, relPath, absPath, stat);
       }
@@ -95,7 +89,7 @@ function walkDirectory(root: string, dir: string) {
   }
 }
 
-walkDirectory("public/", "");
+await walkDirectory("public/");
 
 async function serveFile(
   file: File,