« get me outta code hell

display original file size in image overlay - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util/node-utils.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-03-01 21:11:36 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-03-01 21:11:36 -0400
commit62cd6e574b89a5bfa75dc52ef2383fddf5cbc87a (patch)
treed0c79910842e977847b125fed8c8d7170e630b25 /src/util/node-utils.js
parent62f64b3aa019747e9c764eda853591b321850ca0 (diff)
display original file size in image overlay
Diffstat (limited to 'src/util/node-utils.js')
-rw-r--r--src/util/node-utils.js22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/util/node-utils.js b/src/util/node-utils.js
index 7668482..6c75bab 100644
--- a/src/util/node-utils.js
+++ b/src/util/node-utils.js
@@ -1,5 +1,6 @@
 // Utility functions which are only relevant to particular Node.js constructs.
 
+import {readdir} from 'fs/promises';
 import {fileURLToPath} from 'url';
 import * as path from 'path';
 
@@ -54,3 +55,24 @@ export function isMain(importMetaURL) {
     isIndexJS && 'index.js'
   ].includes(relative);
 }
+
+// Like readdir... but it's recursive!
+export function traverse(startDirPath, {
+  filterFile = () => true,
+  filterDir = () => true
+} = {}) {
+  const recursive = (names, subDirPath) =>
+    Promise.all(
+      names.map((name) =>
+        readdir(path.join(startDirPath, subDirPath, name)).then(
+          (names) =>
+            filterDir(name)
+              ? recursive(names, path.join(subDirPath, name))
+              : [],
+          () => (filterFile(name) ? [path.join(subDirPath, name)] : [])
+        )
+      )
+    ).then((pathArrays) => pathArrays.flatMap((x) => x));
+
+  return readdir(startDirPath).then((names) => recursive(names, ''));
+}