« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/write
diff options
context:
space:
mode:
Diffstat (limited to 'src/write')
-rw-r--r--src/write/build-modes/index.js1
-rw-r--r--src/write/tidy-modes/format-urls.js34
-rw-r--r--src/write/tidy-modes/index.js2
-rw-r--r--src/write/tidy-modes/sort.js (renamed from src/write/build-modes/sort.js)69
4 files changed, 63 insertions, 43 deletions
diff --git a/src/write/build-modes/index.js b/src/write/build-modes/index.js
index 4b61619d..3ae2cfc6 100644
--- a/src/write/build-modes/index.js
+++ b/src/write/build-modes/index.js
@@ -1,4 +1,3 @@
 export * as 'live-dev-server' from './live-dev-server.js';
 export * as 'repl' from './repl.js';
-export * as 'sort' from './sort.js';
 export * as 'static-build' from './static-build.js';
diff --git a/src/write/tidy-modes/format-urls.js b/src/write/tidy-modes/format-urls.js
new file mode 100644
index 00000000..5771fe3e
--- /dev/null
+++ b/src/write/tidy-modes/format-urls.js
@@ -0,0 +1,34 @@
+export const description = `Update data files in-place to satisfy formatting rules for curated URLs`;
+
+import {logInfo} from '#cli';
+import {reformatCuratedURLs} from '#reformat-urls';
+
+export async function go({
+  dataPath,
+  tidyingOnly,
+}) {
+  const changedFiles =
+    await reformatCuratedURLs({
+      dataPath,
+      showChangedFiles: true,
+      showSatisfiedRules: tidyingOnly,
+    });
+
+  if (changedFiles.size === 0) {
+    if (tidyingOnly) {
+      logInfo`All URL formatting rules were already satisfied. Good to go!`;
+      return 'clean';
+    } else {
+      logInfo`All curated URL formatting rules are satisfied - nice!`;
+      return 'clean';
+    }
+  } else {
+    const filesPart =
+      (changedFiles.size === 1
+        ? `1 file`
+        : `${changedFiles.size} files`);
+
+    logInfo`Updated ${filesPart} to satisfy URL formatting rules.`;
+    return 'updated';
+  }
+}
diff --git a/src/write/tidy-modes/index.js b/src/write/tidy-modes/index.js
new file mode 100644
index 00000000..54e2bbf3
--- /dev/null
+++ b/src/write/tidy-modes/index.js
@@ -0,0 +1,2 @@
+export * as 'format-urls' from './format-urls.js';
+export * as 'sort' from './sort.js';
diff --git a/src/write/build-modes/sort.js b/src/write/tidy-modes/sort.js
index 1a738ac8..967a5be1 100644
--- a/src/write/build-modes/sort.js
+++ b/src/write/tidy-modes/sort.js
@@ -4,48 +4,34 @@ import {logInfo} from '#cli';
 import {empty} from '#sugar';
 import thingConstructors from '#things';
 
-export const config = {
-  fileSizes: {
-    applicable: false,
-  },
-
-  languageReloading: {
-    applicable: false,
-  },
-
-  mediaValidation: {
-    applicable: false,
-  },
-
-  search: {
-    applicable: false,
-  },
-
-  thumbs: {
-    applicable: false,
-  },
+export async function go({
+  wikiData,
+  dataPath,
+  tidyingOnly,
+}) {
+  if (empty(wikiData.sortingRules)) {
+    if (tidyingOnly) {
+      logInfo`There aren't any sorting rules in for this wiki.`;
+    }
 
-  webRoutes: {
-    applicable: false,
-  },
+    return 'clean';
+  }
 
-  sort: {
-    applicable: false,
-  },
-};
+  const {SortingRule} = thingConstructors;
 
-export function getCLIOptions() {
-  return {};
-}
+  if (!tidyingOnly) {
+    const results =
+      await Array.fromAsync(SortingRule.go({dataPath, wikiData}));
 
-export async function go({wikiData, dataPath}) {
-  if (empty(wikiData.sortingRules)) {
-    logInfo`There aren't any sorting rules in for this wiki.`;
-    return true;
+    if (results.some(result => result.changed)) {
+      logInfo`Updated data files to satisfy sorting.`;
+      return 'updated';
+    } else {
+      logInfo`All sorting rules are satisfied - nice!`;
+      return 'clean';
+    }
   }
 
-  const {SortingRule} = thingConstructors;
-
   let numUpdated = 0;
   let numActive = 0;
 
@@ -56,7 +42,7 @@ export async function go({wikiData, dataPath}) {
 
     if (result.changed) {
       numUpdated++;
-      logInfo`Updating to satisfy ${niceMessage}.`;
+      logInfo`Updated to satisfy ${niceMessage}.`;
     } else {
       logInfo`Already good: ${niceMessage}`;
     }
@@ -64,13 +50,12 @@ export async function go({wikiData, dataPath}) {
 
   if (numUpdated > 1) {
     logInfo`Updated data files to satisfy ${numUpdated} sorting rules.`;
+    return 'updated';
   } 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!`;
+    return 'updated';
   } else {
-    logInfo`No sorting rules are currently active.`;
+    logInfo`All sorting rules were already satisfied. Good to go!`;
+    return 'clean';
   }
-
-  return true;
 }