« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rwxr-xr-xsrc/upd8.js29
-rw-r--r--src/util/cli.js32
2 files changed, 44 insertions, 17 deletions
diff --git a/src/upd8.js b/src/upd8.js
index b2d5846c..99d55374 100755
--- a/src/upd8.js
+++ b/src/upd8.js
@@ -108,6 +108,7 @@ import {
   logInfo,
   logError,
   parseOptions,
+  progressCallAll,
   progressPromiseAll,
 } from './util/cli.js';
 
@@ -2150,27 +2151,21 @@ async function main() {
 
     // return;
 
-    writes = buildStepsWithTargets.flatMap(({flag, pageSpec, targets}) => {
-      const writes = targets.flatMap(
-        (target) => pageSpec.write(target, {wikiData})?.slice() || []
-      );
-
-      if (!validateWrites(writes, flag + '.write')) {
-        return [];
-      }
+    writes = progressCallAll('Computing page & data writes.', buildStepsWithTargets.flatMap(({flag, pageSpec, targets}) => {
+      const writesFns = targets.map(target => () => {
+        const writes = pageSpec.write(target, {wikiData})?.slice() || [];
+        return validateWrites(writes, flag + '.write') ? writes : [];
+      });
 
       if (pageSpec.writeTargetless) {
-        const writes2 = pageSpec.writeTargetless({wikiData});
-
-        if (!validateWrites(writes2, flag + '.writeTargetless')) {
-          return [];
-        }
-
-        writes.push(...writes2);
+        writesFns.push(() => {
+          const writes = pageSpec.writeTargetless({wikiData});
+          return validateWrites(writes, flag + '.writeTargetless') ? writes : [];
+        });
       }
 
-      return writes;
-    });
+      return writesFns;
+    })).flat();
 
     if (error) {
       return;
diff --git a/src/util/cli.js b/src/util/cli.js
index d28ef40a..cbcbf7cb 100644
--- a/src/util/cli.js
+++ b/src/util/cli.js
@@ -277,3 +277,35 @@ export function progressPromiseAll(msgOrMsgFn, array) {
     )
   );
 }
+
+export function progressCallAll(msgOrMsgFn, array) {
+  if (!array.length) {
+    return [];
+  }
+
+  const msgFn =
+    typeof msgOrMsgFn === 'function' ? msgOrMsgFn : () => msgOrMsgFn;
+
+  let done = 0,
+    total = array.length;
+  process.stdout.write(`\r${msgFn()} [0/${total}]`);
+  const start = Date.now();
+  const vals = [];
+
+  for (const fn of array) {
+    const val = fn();
+    done++;
+    const pc = (Math.round((done / total) * 1000) / 10 + '%').padEnd('99.9%'.length, ' ');
+    if (done === total) {
+      const time = Date.now() - start;
+      process.stdout.write(
+        `\r\x1b[2m${msgFn()} [${pc}] \x1b[0;32mDone! \x1b[0;2m(${time} ms) \x1b[0m\n`
+      );
+    } else {
+      process.stdout.write(`\r${msgFn()} [${pc}] `);
+    }
+    vals.push(val);
+  }
+
+  return vals;
+}