« get me outta code hell

write: support writeTargetless - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/write/build-modes
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-06-24 11:45:19 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-06-24 11:45:19 -0300
commit6fc9a17aad2346177fb3e6e27694a5997e970347 (patch)
treedb465429d088e919729cd8e36bd133cf187931be /src/write/build-modes
parent7d33f79430e72f8c15b2359e652b1b0afef0c1f4 (diff)
write: support writeTargetless
Diffstat (limited to 'src/write/build-modes')
-rw-r--r--src/write/build-modes/live-dev-server.js14
-rw-r--r--src/write/build-modes/static-build.js44
2 files changed, 34 insertions, 24 deletions
diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js
index 8af37d3a..d39b6fdf 100644
--- a/src/write/build-modes/live-dev-server.js
+++ b/src/write/build-modes/live-dev-server.js
@@ -99,10 +99,14 @@ export async function go({
       pageSpec,
       target,
       targetless,
-    }) => () =>
-      targetless
-        ? [pageSpec.writeTargetless({wikiData})]
-        : pageSpec.pathsForTarget(target))).flat();
+    }) => () => {
+      if (targetless) {
+        const result = pageSpec.writeTargetless({wikiData});
+        return Array.isArray(result) ? result : [result];
+      } else {
+        return pageSpec.pathsForTarget(target);
+      }
+    })).flat();
 
   logInfo`Will be serving a total of ${pages.length} pages.`;
 
@@ -337,7 +341,7 @@ export async function go({
         urls,
       });
 
-      const {name, args} = page.contentFunction;
+      const {name, args = []} = page.contentFunction;
 
       const bound = bindUtilities({
         absoluteTo,
diff --git a/src/write/build-modes/static-build.js b/src/write/build-modes/static-build.js
index 67a4df67..f4131a3d 100644
--- a/src/write/build-modes/static-build.js
+++ b/src/write/build-modes/static-build.js
@@ -167,35 +167,41 @@ export async function go({
     // TODO: Port this to aggregate error
     writes = buildSteps
       .map(([flag, pageSpec]) => {
-        // Condition not met: skip this build step altogether.
         if (pageSpec.condition && !pageSpec.condition({wikiData})) {
           return null;
         }
 
-        // May still call writeTargetless if present.
-        if (!pageSpec.targets) {
-          return {flag, pageSpec, targets: []};
-        }
+        const paths = [];
 
-        if (!pageSpec.pathsForTarget) {
-          logError`${flag + '.targets'} is specified, but ${flag + '.pathsForTarget'} is missing!`;
-          error = true;
-          return null;
+        if (pageSpec.writeTargetless) {
+          const result = pageSpec.writeTargetless({wikiData});
+          if (Array.isArray(result)) {
+            paths.push(...result);
+          } else {
+            paths.push(result);
+          }
         }
 
-        const targets = pageSpec.targets({wikiData});
+        if (pageSpec.targets) {
+          if (!pageSpec.pathsForTarget) {
+            logError`${flag + '.targets'} is specified, but ${flag + '.pathsForTarget'} is missing!`;
+            error = true;
+            return null;
+          }
 
-        if (!Array.isArray(targets)) {
-          logError`${flag + '.targets'} was called, but it didn't return an array! (${targets})`;
-          error = true;
-          return null;
-        }
+          const targets = pageSpec.targets({wikiData});
 
-        const pathsForTargets = targets.flatMap(target => pageSpec.pathsForTarget(target));
+          if (!Array.isArray(targets)) {
+            logError`${flag + '.targets'} was called, but it didn't return an array! (${targets})`;
+            error = true;
+            return null;
+          }
 
-        // TODO: Validate each pathsForTargets entry
+          paths.push(...targets.flatMap(target => pageSpec.pathsForTarget(target)));
+          // TODO: Validate each pathsForTargets entry
+        }
 
-        return pathsForTargets;
+        return paths;
       })
       .filter(Boolean)
       .flat();
@@ -322,7 +328,7 @@ export async function go({
           appendIndexHTML: false,
         };
 
-        const {name, args} = page.contentFunction;
+        const {name, args = []} = page.contentFunction;
         const treeInfo = getRelationsTree(allContentDependencies, name, wikiData, ...args);
         const flatTreeInfo = flattenRelationsTree(treeInfo);
         const {root, relationIdentifier, flatRelationSlots} = flatTreeInfo;