« get me outta code hell

artist ref validation, nicer showAggregate output - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2022-02-16 21:48:11 -0400
committer(quasar) nebula <qznebula@protonmail.com>2022-02-16 21:48:11 -0400
commitad079e0c9e6ab32203bc684851e586ed10c3378c (patch)
treef53750079e8b4ac6aad78c83d9b780ac882efccb /src/util
parent2b637c86420eb68bfbc1090ff30f7be3fbb258c8 (diff)
artist ref validation, nicer showAggregate output
Diffstat (limited to 'src/util')
-rw-r--r--src/util/find.js29
-rw-r--r--src/util/sugar.js33
2 files changed, 37 insertions, 25 deletions
diff --git a/src/util/find.js b/src/util/find.js
index 0e220634..a9af2384 100644
--- a/src/util/find.js
+++ b/src/util/find.js
@@ -9,7 +9,7 @@ function findHelper(keys, dataProp, findFns = {}) {
 
     const keyRefRegex = new RegExp(String.raw`^(?:(${keys.join('|')}):(?=\S))?(.*)$`);
 
-    return (fullRef, {wikiData}) => {
+    return (fullRef, {wikiData, quiet = false}) => {
         if (!fullRef) return null;
         if (typeof fullRef !== 'string') {
             throw new Error(`Got a reference that is ${typeof fullRef}, not string: ${fullRef}`);
@@ -26,10 +26,10 @@ function findHelper(keys, dataProp, findFns = {}) {
         const data = wikiData[dataProp];
 
         const found = (key
-            ? byDirectory(ref, data)
-            : byName(ref, data));
+            ? byDirectory(ref, data, quiet)
+            : byName(ref, data, quiet));
 
-        if (!found) {
+        if (!found && !quiet) {
             logWarn`Didn't match anything for ${fullRef}!`;
         }
 
@@ -37,19 +37,22 @@ function findHelper(keys, dataProp, findFns = {}) {
     };
 }
 
-function matchDirectory(ref, data) {
+function matchDirectory(ref, data, quiet) {
     return data.find(({ directory }) => directory === ref);
 }
 
-function matchName(ref, data) {
+function matchName(ref, data, quiet) {
     const matches = data.filter(({ name }) => name.toLowerCase() === ref.toLowerCase());
 
     if (matches.length > 1) {
-        logError`Multiple matches for reference "${ref}". Please resolve:`;
-        for (const match of matches) {
-            logError`- ${match.name} (${match.directory})`;
+        // TODO: This should definitely be a thrown error.
+        if (!quiet) {
+            logError`Multiple matches for reference "${ref}". Please resolve:`;
+            for (const match of matches) {
+                logError`- ${match.name} (${match.directory})`;
+            }
+            logError`Returning null for this reference.`;
         }
-        logError`Returning null for this reference.`;
         return null;
     }
 
@@ -59,15 +62,15 @@ function matchName(ref, data) {
 
     const thing = matches[0];
 
-    if (ref !== thing.name) {
+    if (ref !== thing.name && !quiet) {
         logWarn`Bad capitalization: ${'\x1b[31m' + ref} -> ${'\x1b[32m' + thing.name}`;
     }
 
     return thing;
 }
 
-function matchTagName(ref, data) {
-    return matchName(ref.startsWith('cw: ') ? ref.slice(4) : ref, data);
+function matchTagName(ref, data, quiet) {
+    return matchName(ref.startsWith('cw: ') ? ref.slice(4) : ref, data, quiet);
 }
 
 const find = {
diff --git a/src/util/sugar.js b/src/util/sugar.js
index d6bc3df6..c8f1706c 100644
--- a/src/util/sugar.js
+++ b/src/util/sugar.js
@@ -350,19 +350,28 @@ export function _withAggregate(mode, aggregateOpts, fn) {
     }
 }
 
-export function showAggregate(topError, {pathToFile = p => p} = {}) {
+export function showAggregate(topError, {
+    pathToFile = p => p,
+    showTraces = true
+} = {}) {
     const recursive = (error, {level}) => {
-        const stackLines = error.stack?.split('\n');
-        const stackLine = stackLines?.find(line =>
-            line.trim().startsWith('at')
-            && !line.includes('sugar')
-            && !line.includes('node:internal')
-            && !line.includes('<anonymous>'));
-        const tracePart = (stackLine
-            ? '- ' + stackLine.trim().replace(/file:\/\/(.*\.js)/, (match, pathname) => pathToFile(pathname))
-            : '(no stack trace)');
-
-        const header = `[${error.constructor.name || 'unnamed'}] ${error.message || '(no message)'} ${color.dim(tracePart)}`;
+        let header = (showTraces
+            ? `[${error.constructor.name || 'unnamed'}] ${error.message || '(no message)'}`
+            : (error instanceof AggregateError
+                ? `[${error.message || '(no message)'}]`
+                : error.message || '(no message)'));
+        if (showTraces) {
+            const stackLines = error.stack?.split('\n');
+            const stackLine = stackLines?.find(line =>
+                line.trim().startsWith('at')
+                && !line.includes('sugar')
+                && !line.includes('node:internal')
+                && !line.includes('<anonymous>'));
+            const tracePart = (stackLine
+                ? '- ' + stackLine.trim().replace(/file:\/\/(.*\.js)/, (match, pathname) => pathToFile(pathname))
+                : '(no stack trace)');
+            header += ` ${color.dim(tracePart)}`;
+        }
         const bar = (level % 2 === 0
             ? '\u2502'
             : color.dim('\u254e'));