« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data
diff options
context:
space:
mode:
Diffstat (limited to 'src/data')
-rw-r--r--src/data/things/sorting-rule.js49
1 files changed, 38 insertions, 11 deletions
diff --git a/src/data/things/sorting-rule.js b/src/data/things/sorting-rule.js
index 890a798b..d21781e2 100644
--- a/src/data/things/sorting-rule.js
+++ b/src/data/things/sorting-rule.js
@@ -4,6 +4,7 @@ import {readFile, writeFile} from 'node:fs/promises';
 import * as path from 'node:path';
 
 import {input} from '#composite';
+import {compareArrays} from '#sugar';
 import Thing from '#thing';
 import {isStringNonEmpty, strictArrayOf} from '#validators';
 
@@ -30,10 +31,16 @@ export class SortingRule extends Thing {
     // Update & expose
 
     active: flag(true),
+
+    message: {
+      flags: {update: true, expose: true},
+      update: {validate: isStringNonEmpty},
+    },
   });
 
   static [Thing.yamlDocumentSpec] = {
     fields: {
+      'Message': {property: 'message'},
       'Active': {property: 'active'},
     },
   };
@@ -127,8 +134,18 @@ export class DocumentSortingRule extends ThingSortingRule {
     // TODO: glob :plead:
     filename: {
       flags: {update: true, expose: true},
-      update: {
-        validate: isStringNonEmpty,
+      update: {validate: isStringNonEmpty},
+    },
+
+    message: {
+      flags: {update: true, expose: true},
+      update: {validate: isStringNonEmpty},
+
+      expose: {
+        dependencies: ['filename'],
+        transform: (value, {filename}) =>
+          value ??
+          `Sort ${filename}`,
       },
     },
   });
@@ -139,24 +156,34 @@ export class DocumentSortingRule extends ThingSortingRule {
     },
   });
 
-  async apply({wikiData, dataPath}) {
-    let layout = getThingLayoutForFilename(this.filename, wikiData);
-    if (!layout) return;
+  check({wikiData}) {
+    const oldLayout = getThingLayoutForFilename(this.filename, wikiData);
+    if (!oldLayout) return;
+
+    const newLayout = this.#processLayout(oldLayout);
 
-    layout = this.#processLayout(layout);
+    const oldOrder = flattenThingLayoutToDocumentOrder(oldLayout);
+    const newOrder = flattenThingLayoutToDocumentOrder(newLayout);
 
-    const order = flattenThingLayoutToDocumentOrder(layout);
+    return compareArrays(oldOrder, newOrder);
+  }
+
+  async apply({wikiData, dataPath}) {
+    const oldLayout = getThingLayoutForFilename(this.filename, wikiData);
+    if (!oldLayout) return;
+
+    const newLayout = this.#processLayout(oldLayout);
+    const newOrder = flattenThingLayoutToDocumentOrder(newLayout);
 
     const realPath =
       path.join(
         dataPath,
         this.filename.split(path.posix.sep).join(path.sep));
 
-    let sourceText = await readFile(realPath, 'utf8');
-
-    sourceText = reorderDocumentsInYAMLSourceText(sourceText, order);
+    const oldSourceText = await readFile(realPath, 'utf8');
+    const newSourceText = reorderDocumentsInYAMLSourceText(oldSourceText, newOrder);
 
-    await writeFile(realPath, sourceText);
+    await writeFile(realPath, newSourceText);
   }
 
   #processLayout(layout) {