« get me outta code hell

data, write: nice sorting rule messaging, etc - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/write/build-modes/sort.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2025-02-22 17:18:48 -0400
committer(quasar) nebula <qznebula@protonmail.com>2025-02-22 21:12:24 -0400
commite0bf4a7ae3a6ec2f2278f0a62efac45466397803 (patch)
tree04f6c10f16be8e39eb90d0d32df375a8cd44a220 /src/write/build-modes/sort.js
parent6820bb9b53d022394fc350c4f6307aa141954290 (diff)
data, write: nice sorting rule messaging, etc
Diffstat (limited to 'src/write/build-modes/sort.js')
-rw-r--r--src/write/build-modes/sort.js38
1 files changed, 37 insertions, 1 deletions
diff --git a/src/write/build-modes/sort.js b/src/write/build-modes/sort.js
index c444d295..0b759c45 100644
--- a/src/write/build-modes/sort.js
+++ b/src/write/build-modes/sort.js
@@ -1,5 +1,8 @@
 export const description = `Update data files in-place to satisfy custom sorting rules`;
 
+import {logInfo} from '#cli';
+import {empty} from '#sugar';
+
 export const config = {
   fileSizes: {
     applicable: false,
@@ -31,7 +34,40 @@ export function getCLIOptions() {
 }
 
 export async function go({wikiData, dataPath}) {
+  if (empty(wikiData.sortingRules)) {
+    logInfo`There aren't any sorting rules in for this wiki.`;
+    return true;
+  }
+
+  let numUpdated = 0;
+  let numActive = 0;
+
   for (const sortingRule of wikiData.sortingRules) {
-    await sortingRule.apply({wikiData, dataPath});
+    if (!sortingRule.active) continue;
+
+    numActive++;
+
+    const niceMessage = `"${sortingRule.message}"`;
+
+    if (sortingRule.check({wikiData})) {
+      logInfo`Already good: ${niceMessage}`;
+    } else {
+      logInfo`Updating to satisfy ${niceMessage}.`;
+      await sortingRule.apply({wikiData, dataPath});
+
+      numUpdated++;
+    }
   }
+
+  if (numUpdated > 1) {
+    logInfo`Updated data files to satisfy ${numUpdated} sorting rules.`;
+  } else if (numUpdated === 1) {
+    logInfo`Updated data files to satisfy ${1} sorting rule.`
+  } else if (numActive >= 1) {
+    logInfo`All sorting rules were already satisfied. Good to go!`;
+  } else {
+    logInfo`No sorting rules are currently active.`;
+  }
+
+  return true;
 }