From ad079e0c9e6ab32203bc684851e586ed10c3378c Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Wed, 16 Feb 2022 21:48:11 -0400 Subject: artist ref validation, nicer showAggregate output --- src/util/find.js | 29 ++++++++++++++++------------- src/util/sugar.js | 33 +++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 25 deletions(-) (limited to 'src/util') 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('')); - 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('')); + 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')); -- cgit 1.3.0-6-gf8a5