« 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/node-utils.js35
1 files changed, 23 insertions, 12 deletions
diff --git a/src/util/node-utils.js b/src/util/node-utils.js
index 6c75bab6..3c0dd4cd 100644
--- a/src/util/node-utils.js
+++ b/src/util/node-utils.js
@@ -58,21 +58,32 @@ export function isMain(importMetaURL) {
 
 // Like readdir... but it's recursive!
 export function traverse(startDirPath, {
+  pathStyle = 'device',
   filterFile = () => true,
   filterDir = () => true
 } = {}) {
+  const pathJoin = {
+    'device': path.join,
+    'posix': path.posix.join,
+    'win32': path.win32.join,
+  }[pathStyle];
+
+  if (!pathJoin) {
+    throw new Error(`Expected pathStyle to be device, posix, or win32`);
+  }
+
   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));
+    Promise.all(names.map(name =>
+      readdir(pathJoin(startDirPath, subDirPath, name)).then(
+        names =>
+          (filterDir(name)
+            ? recursive(names, pathJoin(subDirPath, name))
+            : []),
+        () =>
+          (filterFile(name)
+            ? [pathJoin(subDirPath, name)]
+            : []))))
+      .then(pathArrays => pathArrays.flat());
 
-  return readdir(startDirPath).then((names) => recursive(names, ''));
+  return readdir(startDirPath).then(names => recursive(names, ''));
 }