From 5315241480955e7bfe09c1c9ea81be50a10a4318 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sun, 22 May 2022 10:26:25 -0300 Subject: new YAML quick load util, moved code from repl --- src/data/yaml.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/repl.js | 56 ++------------------------------------------------------ 2 files changed, 57 insertions(+), 54 deletions(-) (limited to 'src') diff --git a/src/data/yaml.js b/src/data/yaml.js index e921276..072aca6 100644 --- a/src/data/yaml.js +++ b/src/data/yaml.js @@ -29,12 +29,15 @@ import { import { color, ENABLE_COLOR, + logInfo, + logWarn, } from '../util/cli.js'; import { decorateErrorWithIndex, mapAggregate, openAggregate, + showAggregate, withAggregate, } from '../util/sugar.js'; @@ -1274,3 +1277,55 @@ export function filterReferenceErrors(wikiData) { return aggregate; } + +// Utility function for loading all wiki data from the provided YAML data +// directory (e.g. the root of the hsmusic-data repository). This doesn't +// provide much in the way of customization; it's meant to be used more as +// a boilerplate for more specialized output, or as a quick start in utilities +// where reporting info about data loading isn't as relevant as during the +// main wiki build process. +export async function quickLoadAllFromYAML(dataPath, { + showAggregate: customShowAggregate = showAggregate, +} = {}) { + const showAggregate = customShowAggregate; + + let wikiData; + + { + const { aggregate, result } = await loadAndProcessDataDocuments({ + dataPath, + }); + + wikiData = result; + + try { + aggregate.close(); + logInfo`Loaded data without errors. (complete data)`; + } catch (error) { + showAggregate(error); + logWarn`Loaded data with errors. (partial data)`; + } + } + + linkWikiDataArrays(wikiData); + + try { + filterDuplicateDirectories(wikiData).close(); + logInfo`No duplicate directories found. (complete data)`; + } catch (error) { + showAggregate(error); + logWarn`Duplicate directories found. (partial data)`; + } + + try { + filterReferenceErrors(wikiData).close(); + logInfo`No reference errors found. (complete data)`; + } catch (error) { + showAggregate(error); + logWarn`Duplicate directories found. (partial data)`; + } + + sortWikiDataArrays(wikiData); + + return wikiData; +} diff --git a/src/repl.js b/src/repl.js index 05dbf11..cd4c321 100644 --- a/src/repl.js +++ b/src/repl.js @@ -4,14 +4,7 @@ import * as repl from 'repl'; import { fileURLToPath } from 'url'; import { promisify } from 'util'; -import { - filterDuplicateDirectories, - filterReferenceErrors, - linkWikiDataArrays, - loadAndProcessDataDocuments, - sortWikiDataArrays, -} from './data/yaml.js'; - +import { quickLoadAllFromYAML } from './data/yaml.js'; import { logError, parseOptions } from './util/cli.js'; import { showAggregate } from './util/sugar.js'; @@ -41,54 +34,9 @@ async function main() { return; } - const niceShowAggregate = (error, ...opts) => { - showAggregate(error, { - showTraces: showAggregateTraces, - pathToFile: f => path.relative(__dirname, f), - ...opts - }); - }; - console.log('HSMusic data REPL'); - let wikiData; - - { - const { aggregate, result } = await loadAndProcessDataDocuments({ - dataPath, - }); - - wikiData = result; - - try { - aggregate.close(); - console.log('Loaded data without errors. (complete data)'); - } catch (error) { - niceShowAggregate(error); - console.log('Loaded data with errors. (partial data)'); - } - } - - linkWikiDataArrays(wikiData); - - try { - filterDuplicateDirectories(wikiData).close(); - console.log('No duplicate directories found. (complete data)'); - } catch (error) { - niceShowAggregate(error); - console.log('Duplicate directories found. (partial data)'); - } - - try { - filterReferenceErrors(wikiData).close(); - console.log('No reference errors found. (complete data)'); - } catch (error) { - niceShowAggregate(error); - console.log('Duplicate directories found. (partial data)'); - } - - sortWikiDataArrays(wikiData); - + const wikiData = await quickLoadAllFromYAML(dataPath); const replServer = repl.start(); Object.assign( -- cgit 1.3.0-6-gf8a5