« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/file-size-preloader.js2
-rw-r--r--src/node-utils.js3
-rw-r--r--src/quickstat.js52
-rwxr-xr-xsrc/upd8.js5
4 files changed, 60 insertions, 2 deletions
diff --git a/src/file-size-preloader.js b/src/file-size-preloader.js
index b2a55407..76d4cf82 100644
--- a/src/file-size-preloader.js
+++ b/src/file-size-preloader.js
@@ -17,10 +17,10 @@
 // This only processes files one at a time because I'm lazy and stat calls
 // are very, very fast.
 
-import {stat} from 'node:fs/promises';
 import {relative, resolve, sep} from 'node:path';
 
 import {logWarn} from '#cli';
+import {stat} from '#quickstat';
 import {filterMultipleArrays, transposeArrays} from '#sugar';
 
 export default class FileSizePreloader {
diff --git a/src/node-utils.js b/src/node-utils.js
index 345d10aa..59e7372c 100644
--- a/src/node-utils.js
+++ b/src/node-utils.js
@@ -1,11 +1,12 @@
 // Utility functions which are only relevant to particular Node.js constructs.
 
-import {readdir, stat} from 'node:fs/promises';
 import * as path from 'node:path';
 import {fileURLToPath} from 'node:url';
 
 import _commandExists from 'command-exists';
 
+import {readdir, stat} from '#quickstat';
+
 // This package throws an error instead of returning false when the command
 // doesn't exist, for some reason. Yay for making logic more difficult!
 // Here's a straightforward workaround.
diff --git a/src/quickstat.js b/src/quickstat.js
new file mode 100644
index 00000000..e8714b2a
--- /dev/null
+++ b/src/quickstat.js
@@ -0,0 +1,52 @@
+import * as fs from 'node:fs/promises';
+import * as path from 'node:path';
+
+export const requested = {};
+export const recorded = {};
+
+export function track(pathArg, {
+  readdir = false,
+  stat = false,
+}) {
+  pathArg = path.resolve(pathArg);
+  requested[pathArg] = {readdir, stat};
+}
+
+export function tracking(pathArg, resolved = false) {
+  if (!resolved) pathArg = path.resolve(pathArg);
+  for (const key of Object.keys(requested)) {
+    if (pathArg.startsWith(key + '/')) {
+      return requested[key];
+    }
+  }
+
+  return null;
+}
+
+async function go(fn, key, pathArg) {
+  pathArg = path.resolve(pathArg);
+
+  const info = tracking(pathArg, true);
+  if (!info?.[key]) return fn(pathArg);
+
+  const record = recorded[pathArg]?.[key];
+  if (record) return record;
+
+  const result = await fn(pathArg);
+  recorded[pathArg] ??= {};
+  recorded[pathArg][key] = result;
+  return result;
+}
+
+export function stat(pathArg) {
+  return go(fs.stat, 'stat', pathArg);
+}
+
+export function readdir(pathArg) {
+  return go(fs.readdir, 'readdir', pathArg);
+}
+
+export function reset() {
+  for (const key in requested) delete requested[key];
+  for (const key in recorded) delete recorded[key];
+}
\ No newline at end of file
diff --git a/src/upd8.js b/src/upd8.js
index 7ffcc406..2091e5ba 100755
--- a/src/upd8.js
+++ b/src/upd8.js
@@ -49,6 +49,7 @@ import find, {bindFind, getAllFindSpecs} from '#find';
 import {processLanguageFile, watchLanguageFile, internalDefaultStringsFile}
   from '#language';
 import {isMain, traverse} from '#node-utils';
+import * as quickstat from '#quickstat';
 import {bindReverse} from '#reverse';
 import {writeSearchData} from '#search';
 import {sortByName} from '#sort';
@@ -1176,6 +1177,8 @@ async function main() {
     html.disableTagTracing();
   }
 
+  await quickstat.track(mediaPath, {readdir: true, stat: true});
+
   Object.assign(stepStatusSummary.determineMediaCachePath, {
     status: STATUS_STARTED_NOT_DONE,
     timeStart: Date.now(),
@@ -3192,6 +3195,8 @@ async function main() {
           .some(({to}) => to[0].startsWith('searchData'))
       : null);
 
+  quickstat.reset();
+
   if (stepStatusSummary.performBuild.status === STATUS_NOT_APPLICABLE) {
     return true;
   }