From 04acee7dfdaf81f8c548cd8ed1031cd01b1396b4 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Tue, 6 Dec 2022 14:57:43 -0400 Subject: more REPL stuff, prepare default-strings Language --- src/repl.js | 110 ++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 74 insertions(+), 36 deletions(-) diff --git a/src/repl.js b/src/repl.js index a60d780..08e83a5 100644 --- a/src/repl.js +++ b/src/repl.js @@ -1,12 +1,17 @@ import * as os from 'os'; import * as path from 'path'; import * as repl from 'repl'; +import {fileURLToPath} from 'url'; +import thingConstructors from './data/things/index.js'; import {quickLoadAllFromYAML} from './data/yaml.js'; import {logError, logWarn, parseOptions} from './util/cli.js'; -import {showAggregate} from './util/sugar.js'; +import {isMain} from './util/node-utils.js'; +import {bindOpts, showAggregate} from './util/sugar.js'; import {generateURLs} from './util/urls.js'; +import {processLanguageFile} from './upd8.js'; + import * as serialize from './util/serialize.js'; import * as sugar from './util/sugar.js'; import * as wikiDataUtils from './util/wiki-data.js'; @@ -14,30 +19,9 @@ import _find, {bindFind} from './util/find.js'; import urlSpec from './url-spec.js'; -async function main() { - const miscOptions = await parseOptions(process.argv.slice(2), { - 'data-path': { - type: 'value', - }, - - 'no-history': { - type: 'flag', - }, - }); - - const dataPath = miscOptions['data-path'] || process.env.HSMUSIC_DATA; - const disableHistory = miscOptions['no-history'] ?? false; - - if (!dataPath) { - logError`Expected --data-path option or HSMUSIC_DATA to be set`; - return; - } - - console.log('HSMusic data REPL'); - - const wikiData = await quickLoadAllFromYAML(dataPath); - const replServer = repl.start(); - +export async function getContextAssignments({ + wikiData, +}) { let urls; try { urls = generateURLs(urlSpec); @@ -56,22 +40,74 @@ async function main() { logWarn`\`find\` variable will be missing`; } - Object.assign(replServer.context, wikiData, { + let language; + try { + language = await processLanguageFile( + path.join( + path.dirname(fileURLToPath(import.meta.url)), + 'strings-default.json')); + } catch (error) { + console.error(error); + logWarn`Failed to create Language object`; + logWarn`\`language\` variable will be missing`; + language = undefined; + } + + return { wikiData, + ...wikiData, WD: wikiData, + ...thingConstructors, + language, + + ...sugar, + ...wikiDataUtils, + serialize, S: serialize, + urls, + _find, find, bindFind, - urls, + }; +} - ...sugar, - ...wikiDataUtils, +async function main() { + const miscOptions = await parseOptions(process.argv.slice(2), { + 'data-path': { + type: 'value', + }, + + 'no-history': { + type: 'flag', + }, + }); + + const dataPath = miscOptions['data-path'] || process.env.HSMUSIC_DATA; + const disableHistory = miscOptions['no-history'] ?? false; + + if (!dataPath) { + logError`Expected --data-path option or HSMUSIC_DATA to be set`; + return; + } + + console.log('HSMusic data REPL'); + + const wikiData = await quickLoadAllFromYAML(dataPath, { + showAggregate: bindOpts(showAggregate, { + showTraces: false, + }), }); + const replServer = repl.start(); + + Object.assign(replServer.context, await getContextAssignments({ + wikiData, + })); + if (disableHistory) { console.log(`\rInput history disabled (--no-history provided)`); replServer.displayPrompt(true); @@ -92,10 +128,12 @@ async function main() { } } -main().catch((error) => { - if (error instanceof AggregateError) { - showAggregate(error); - } else { - console.error(error); - } -}); +if (isMain(import.meta.url)) { + main().catch((error) => { + if (error instanceof AggregateError) { + showAggregate(error); + } else { + console.error(error); + } + }); +} -- cgit 1.3.0-6-gf8a5