« get me outta code hell

boot directly into REPL with --repl - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/repl.js
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-03-04 18:00:10 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-03-04 18:00:10 -0400
commit06e1503a921ca28c4d4f994ee46a3044acfb3a32 (patch)
treea6d150ce782582b963850a4e6b2598f4bdd5d0d9 /src/repl.js
parent79b233cab5853b50717ffb281247485e26101ef0 (diff)
boot directly into REPL with --repl
Diffstat (limited to 'src/repl.js')
-rw-r--r--src/repl.js73
1 files changed, 51 insertions, 22 deletions
diff --git a/src/repl.js b/src/repl.js
index f6f78bf0..f9996453 100644
--- a/src/repl.js
+++ b/src/repl.js
@@ -22,6 +22,8 @@ import urlSpec from './url-spec.js';
 const __dirname = path.dirname(fileURLToPath(import.meta.url));
 
 export async function getContextAssignments({
+  dataPath,
+  mediaPath,
   wikiData,
 }) {
   let urls;
@@ -56,6 +58,9 @@ export async function getContextAssignments({
   }
 
   return {
+    dataPath,
+    mediaPath,
+
     wikiData,
     ...wikiData,
     WD: wikiData,
@@ -77,30 +82,22 @@ export async function getContextAssignments({
   };
 }
 
-async function main() {
-  const miscOptions = await parseOptions(process.argv.slice(2), {
-    'data-path': {
-      type: 'value',
-    },
-
-    'no-history': {
-      type: 'flag',
-    },
-
-    'show-traces': {
-      type: 'flag',
-    },
-  });
-
-  const dataPath = miscOptions['data-path'] || process.env.HSMUSIC_DATA;
-  const disableHistory = miscOptions['no-history'] ?? false;
-  const showTraces = miscOptions['show-traces'] ?? false;
-
+export default async function bootRepl({
+  dataPath = process.env.HSMUSIC_DATA,
+  mediaPath = process.env.HSMUSIC_MEDIA,
+  disableHistory = false,
+  showTraces = false,
+}) {
   if (!dataPath) {
     logError`Expected --data-path option or HSMUSIC_DATA to be set`;
     return;
   }
 
+  if (!mediaPath) {
+    logError`Expected --media-path option or HSMUSIC_MEDIA to be set`;
+    return;
+  }
+
   console.log('HSMusic data REPL');
 
   const wikiData = await quickLoadAllFromYAML(dataPath, {
@@ -117,23 +114,55 @@ async function main() {
   }));
 
   if (disableHistory) {
-    console.log(`\rInput history disabled (--no-history provided)`);
+    console.log(`\rInput history disabled (--no-repl-history provided)`);
     replServer.displayPrompt(true);
   } else {
     const historyFile = path.join(os.homedir(), '.hsmusic_repl_history');
     replServer.setupHistory(historyFile, (err) => {
       if (err) {
         console.error(
-          `\rFailed to begin locally logging input history to ${historyFile} (provide --no-history to disable)`
+          `\rFailed to begin locally logging input history to ${historyFile} (provide --no-repl-history to disable)`
         );
       } else {
         console.log(
-          `\rLogging input history to ${historyFile} (provide --no-history to disable)`
+          `\rLogging input history to ${historyFile} (provide --no-repl-history to disable)`
         );
       }
       replServer.displayPrompt(true);
     });
   }
+
+  // Is this called breaking a promise?
+  await new Promise(() => {});
+
+  return true;
+}
+
+async function main() {
+  const miscOptions = await parseOptions(process.argv.slice(2), {
+    'data-path': {
+      type: 'value',
+    },
+
+    'media-path': {
+      type: 'value',
+    },
+
+    'no-repl-history': {
+      type: 'flag',
+    },
+
+    'show-traces': {
+      type: 'flag',
+    },
+  });
+
+  return bootRepl({
+    dataPath: miscOptions['data-path'],
+    mediaPath: miscOptions['media-path'],
+    disableHistory: miscOptions['no-repl-history'],
+    showTraces: miscOptions['show-traces'],
+  });
 }
 
 if (isMain(import.meta.url)) {