« 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/gen-thumbs.js25
-rwxr-xr-xsrc/upd8.js1
-rw-r--r--src/util/node-utils.js35
3 files changed, 46 insertions, 15 deletions
diff --git a/src/gen-thumbs.js b/src/gen-thumbs.js
index dfa2c2ab..655ab6d0 100644
--- a/src/gen-thumbs.js
+++ b/src/gen-thumbs.js
@@ -208,6 +208,7 @@ export async function clearThumbs(mediaPath, {
   logInfo`Looking for thumbnails to clear out...`;
 
   const thumbFiles = await traverse(mediaPath, {
+    pathStyle: 'device',
     filterFile: file => isThumb(file),
     filterDir: name => name !== '.git',
   });
@@ -512,15 +513,33 @@ export async function verifyImagePaths(mediaPath, {urls, wikiData}) {
 // Recursively traverses the provided (extant) media path, filtering so only
 // "source" images are returned - no thumbnails and no non-images. Provide
 // target as 'generate' or 'verify' to indicate the desired use of the results.
-// Under 'verify', all source files are returned, so that their existence can
-// be verified against a list of expected source files. Under 'generate', files
-// which shouldn't actually get thumbnails are excluded.
+//
+// Under 'verify':
+//
+// * All source files are returned, so that their existence can be verified
+//   against a list of expected source files.
+//
+// * Source files are returned in "wiki" path style, AKA with POSIX-style
+//   forward slashes, regardless the system being run on.
+//
+// Under 'generate':
+//
+// * All files which shouldn't actually have thumbnails generated are excluded.
+//
+// * Source files are returned in device-style, with backslashes on Windows.
+//   These are suitable to be passed as command-line arguments to ImageMagick.
+//
+// Both modes return paths relative to mediaPath, with no ./ or .\ at the
+// front.
+//
 export async function traverseSourceImagePaths(mediaPath, {target}) {
   if (target !== 'verify' && target !== 'generate') {
     throw new Error(`Expected target to be 'verify' or 'generate', got ${target}`);
   }
 
   return await traverse(mediaPath, {
+    pathStyle: (target === 'verify' ? 'posix' : 'device'),
+
     filterFile(name) {
       const ext = path.extname(name);
 
diff --git a/src/upd8.js b/src/upd8.js
index f659b360..51957225 100755
--- a/src/upd8.js
+++ b/src/upd8.js
@@ -708,6 +708,7 @@ async function main() {
   // cheat and get file sizes for all images under media. (This includes
   // additional files which are images.)
   const imageFilePaths = (await traverse(mediaPath, {
+    pathStyle: 'device',
     filterDir: dir => dir !== '.git',
     filterFile: file => (
       ['.png', '.gif', '.jpg'].includes(path.extname(file)) &&
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, ''));
 }