« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/data/language.js3
-rwxr-xr-xsrc/upd8.js43
2 files changed, 37 insertions, 9 deletions
diff --git a/src/data/language.js b/src/data/language.js
index ec38cbde..5ab3936e 100644
--- a/src/data/language.js
+++ b/src/data/language.js
@@ -120,6 +120,8 @@ export function watchLanguageFile(file, {
     try {
       properties = await processLanguageSpecFromFile(file);
     } catch (error) {
+      events.emit('error', error);
+
       if (logging) {
         if (successfullyAppliedLanguage) {
           logWarn`Failed to load language ${basename} - using existing version`;
@@ -128,6 +130,7 @@ export function watchLanguageFile(file, {
         }
         showAggregate(error, {showTraces: false});
       }
+
       return;
     }
 
diff --git a/src/upd8.js b/src/upd8.js
index b1340989..9714d166 100755
--- a/src/upd8.js
+++ b/src/upd8.js
@@ -39,7 +39,7 @@ import {fileURLToPath} from 'node:url';
 import wrap from 'word-wrap';
 
 import {displayCompositeCacheAnalysis} from '#composite';
-import {processLanguageFile} from '#language';
+import {processLanguageFile, watchLanguageFile} from '#language';
 import {isMain, traverse} from '#node-utils';
 import bootRepl from '#repl';
 import {empty, showAggregate, withEntries} from '#sugar';
@@ -1086,17 +1086,34 @@ async function main() {
 
   let internalDefaultLanguage;
 
-  try {
-    internalDefaultLanguage =
-      await processLanguageFile(path.join(__dirname, DEFAULT_STRINGS_FILE));
+  const internalDefaultLanguageWatcher =
+    watchLanguageFile(path.join(__dirname, DEFAULT_STRINGS_FILE));
 
-    Object.assign(stepStatusSummary.loadInternalDefaultLanguage, {
-      status: STATUS_DONE_CLEAN,
-      timeEnd: Date.now(),
+  try {
+    await new Promise((resolve, reject) => {
+      const watcher = internalDefaultLanguageWatcher;
+
+      const onReady = () => {
+        watcher.removeListener('ready', onReady);
+        watcher.removeListener('error', onError);
+        resolve();
+      };
+
+      const onError = error => {
+        watcher.removeListener('ready', onReady);
+        watcher.removeListener('error', onError);
+        watcher.close();
+        reject(error);
+      };
+
+      watcher.on('ready', onReady);
+      watcher.on('error', onError);
     });
-  } catch (error) {
-    console.error(error);
 
+    internalDefaultLanguage = internalDefaultLanguageWatcher.language;
+  } catch (_error) {
+    // No need to display the error here - it's already printed by
+    // watchLanguageFile.
     logError`There was an error reading the internal language file.`;
     fileIssue();
 
@@ -1109,6 +1126,14 @@ async function main() {
     return false;
   }
 
+  // Bypass node.js special-case handling for uncaught error events
+  internalDefaultLanguageWatcher.on('error', () => {});
+
+  Object.assign(stepStatusSummary.loadInternalDefaultLanguage, {
+    status: STATUS_DONE_CLEAN,
+    timeEnd: Date.now(),
+  });
+
   let languages;
 
   if (langPath) {