فهرست منبع

chore: refactor log.ts, go back to glob but add sort (#22107)

Aiden Cline 5 روز پیش
والد
کامیت
74b14a2d4e
1فایلهای تغییر یافته به همراه10 افزوده شده و 2 حذف شده
  1. 10 2
      packages/opencode/src/util/log.ts

+ 10 - 2
packages/opencode/src/util/log.ts

@@ -3,6 +3,7 @@ import fs from "fs/promises"
 import { createWriteStream } from "fs"
 import { Global } from "../global"
 import z from "zod"
+import { Glob } from "./glob"
 
 export namespace Log {
   export const Level = z.enum(["DEBUG", "INFO", "WARN", "ERROR"]).meta({ ref: "LogLevel", description: "Log level" })
@@ -15,7 +16,6 @@ export namespace Log {
     ERROR: 3,
   }
   const keep = 10
-  const rx = /^\d{4}-\d{2}-\d{2}T\d{6}\.log$/
 
   let level: Level = "INFO"
 
@@ -79,7 +79,15 @@ export namespace Log {
   }
 
   async function cleanup(dir: string) {
-    const files = (await fs.readdir(dir).catch(() => [])).filter((file) => rx.test(file)).sort()
+    const files = (
+      await Glob.scan("????-??-??T??????.log", {
+        cwd: dir,
+        absolute: false,
+        include: "file",
+      }).catch(() => [])
+    )
+      .filter((file) => path.basename(file) === file)
+      .sort()
     if (files.length <= keep) return
 
     const doomed = files.slice(0, -keep)