Просмотр исходного кода

introduce cache version concept for auto cleanup when breaking cache changes happen

Dax Raad 7 месяцев назад
Родитель
Сommit
4bb8536d34
2 измененных файлов с 17 добавлено и 5 удалено
  1. 6 4
      packages/opencode/src/bun/index.ts
  2. 11 1
      packages/opencode/src/global/index.ts

+ 6 - 4
packages/opencode/src/bun/index.ts

@@ -65,10 +65,12 @@ export namespace BunProc {
     }))
     }))
     if (parsed.dependencies[pkg] === version) return mod
     if (parsed.dependencies[pkg] === version) return mod
     parsed.dependencies[pkg] = version
     parsed.dependencies[pkg] = version
-    await Bun.write(pkgjson, JSON.stringify(parsed, null, 2))
-    await BunProc.run(["install", "--cwd", Global.Path.cache, "--registry=https://registry.npmjs.org"], {
-      cwd: Global.Path.cache,
-    }).catch((e) => {
+    await BunProc.run(
+      ["add", "--exact", "--cwd", Global.Path.cache, "--registry=https://registry.npmjs.org", pkg + "@" + version],
+      {
+        cwd: Global.Path.cache,
+      },
+    ).catch((e) => {
       throw new InstallFailedError(
       throw new InstallFailedError(
         { pkg, version },
         { pkg, version },
         {
         {

+ 11 - 1
packages/opencode/src/global/index.ts

@@ -23,7 +23,17 @@ export namespace Global {
 await Promise.all([
 await Promise.all([
   fs.mkdir(Global.Path.data, { recursive: true }),
   fs.mkdir(Global.Path.data, { recursive: true }),
   fs.mkdir(Global.Path.config, { recursive: true }),
   fs.mkdir(Global.Path.config, { recursive: true }),
-  fs.mkdir(Global.Path.cache, { recursive: true }),
   fs.mkdir(Global.Path.providers, { recursive: true }),
   fs.mkdir(Global.Path.providers, { recursive: true }),
   fs.mkdir(Global.Path.state, { recursive: true }),
   fs.mkdir(Global.Path.state, { recursive: true }),
 ])
 ])
+
+const CACHE_VERSION = "1"
+
+const version = await Bun.file(path.join(Global.Path.cache, "version"))
+  .text()
+  .catch(() => "0")
+
+if (version !== CACHE_VERSION) {
+  await fs.rm(Global.Path.cache, { recursive: true, force: true })
+  await Bun.file(path.join(Global.Path.cache, "version")).write(CACHE_VERSION)
+}