« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/html.js8
-rw-r--r--src/util/node-utils.js22
2 files changed, 27 insertions, 3 deletions
diff --git a/src/util/html.js b/src/util/html.js
index 459a1647..1c55fb8c 100644
--- a/src/util/html.js
+++ b/src/util/html.js
@@ -63,9 +63,11 @@ export function tag(tagName, ...args) {
 
     const joiner = attrs?.[joinChildren];
     content = content.filter(Boolean).join(
-      (joiner
-        ? `\n${joiner}\n`
-        : '\n'));
+      (joiner === ''
+        ? ''
+        : (joiner
+            ? `\n${joiner}\n`
+            : '\n')));
   }
 
   if (attrs?.[onlyIfContent] && !content) {
diff --git a/src/util/node-utils.js b/src/util/node-utils.js
index 76684827..6c75bab6 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, ''));
+}