« 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/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;