« get me outta code hell

more REPL stuff, prepare default-strings Language - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2022-12-06 14:57:43 -0400
committer(quasar) nebula <qznebula@protonmail.com>2022-12-06 14:57:43 -0400
commit04acee7dfdaf81f8c548cd8ed1031cd01b1396b4 (patch)
treee01e26b2d988994efdf3b7f9defde280f290b436 /src
parent0bc52b8d8fb15c4420e49a2503f82e799a18e20d (diff)
more REPL stuff, prepare default-strings Language
Diffstat (limited to 'src')
-rw-r--r--src/repl.js110
1 files changed, 74 insertions, 36 deletions
diff --git a/src/repl.js b/src/repl.js
index a60d7809..08e83a5c 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);
+    }
+  });
+}