« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/file-size-preloader.js23
-rwxr-xr-xsrc/upd8.js32
-rw-r--r--src/util/cli.js2
3 files changed, 57 insertions, 0 deletions
diff --git a/src/file-size-preloader.js b/src/file-size-preloader.js
index 4eadde7b..a5db1cb0 100644
--- a/src/file-size-preloader.js
+++ b/src/file-size-preloader.js
@@ -20,6 +20,7 @@
 import {stat} from 'node:fs/promises';
 
 import {logWarn} from '#cli';
+import {transposeArrays} from '#sugar';
 
 export default class FileSizePreloader {
   #paths = [];
@@ -101,4 +102,26 @@ export default class FileSizePreloader {
     if (index > this.#loadedPathIndex) return null;
     return this.#sizes[index];
   }
+
+  saveAsCache() {
+    const entries =
+      transposeArrays([
+        this.#paths.slice(0, this.#loadedPathIndex),
+        this.#sizes.slice(0, this.#loadedPathIndex),
+      ]);
+
+    return Object.fromEntries(entries);
+  }
+
+  loadFromCache(cache) {
+    const entries =
+      Object.entries(cache)
+        .filter(([p]) => !this.#paths.includes(p));
+
+    const [newPaths, newSizes] = transposeArrays(entries);
+
+    this.#paths.splice(this.#loadedPathIndex + 1, 0, ...newPaths);
+    this.#sizes.splice(this.#loadedPathIndex + 1, 0, ...newSizes);
+    this.#loadedPathIndex += entries.length;
+  }
 }
diff --git a/src/upd8.js b/src/upd8.js
index 306715dd..b9e71784 100755
--- a/src/upd8.js
+++ b/src/upd8.js
@@ -2614,6 +2614,38 @@ async function main() {
         memory: process.memoryUsage(),
       });
     }
+
+    // TODO: kinda jank that this is out of band of any particular step,
+    // even though it's operationally a follow-up to preloadFileSizes
+
+    let oopsCache = false;
+    saveFileSizeCache: {
+      let cache;
+      try {
+        cache = fileSizePreloader.saveAsCache();
+      } catch (error) {
+        console.error(error);
+        logWarn`Couldn't compute file size preloader's cache.`;
+        oopsCache = true;
+        break saveFileSizeCache;
+      }
+
+      const cacheFile = path.join(mediaCachePath, 'file-size-cache.json');
+
+      try {
+        await writeFile(cacheFile, stringifyCache(cache));
+      } catch (error) {
+        console.error(error);
+        logWarn`Couldn't save preloaded file sizes to a cache file:`;
+        logWarn`${cacheFile}`;
+        oopsCache = true;
+      }
+    }
+
+    if (oopsCache) {
+      logWarn`This won't affect the build, but this build should not be used`;
+      logWarn`as a model for another build accessing its media files online.`;
+    }
   }
 
   if (stepStatusSummary.buildSearchIndex.status === STATUS_NOT_STARTED) {
diff --git a/src/util/cli.js b/src/util/cli.js
index be59b35e..a40a911f 100644
--- a/src/util/cli.js
+++ b/src/util/cli.js
@@ -5,6 +5,8 @@
 
 const {process} = globalThis;
 
+import {sortByName} from './sort.js';
+
 export const ENABLE_COLOR =
   process &&
   ((process.env.CLICOLOR_FORCE && process.env.CLICOLOR_FORCE === '1') ??