« get me outta code hell

data: more language loading refactoring - 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>2023-11-01 09:12:23 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-11-06 17:56:06 -0400
commitcc3a6e32b957c60aa29027fa575e4b3ca0c05c64 (patch)
tree25e6c6a289840b31cbd2a69490fbd70b9f6d7edf /src
parent0768953f9538f0bbd65835b0a4293e2ba438ce52 (diff)
data: more language loading refactoring
Diffstat (limited to 'src')
-rw-r--r--src/data/language.js40
1 files changed, 23 insertions, 17 deletions
diff --git a/src/data/language.js b/src/data/language.js
index 34de8779..b71e55a2 100644
--- a/src/data/language.js
+++ b/src/data/language.js
@@ -1,10 +1,13 @@
 import {readFile} from 'node:fs/promises';
 
-// It stands for "HTML Entities", apparently. Cursed.
-import he from 'he';
+import chokidar from 'chokidar';
+import he from 'he'; // It stands for "HTML Entities", apparently. Cursed.
 
+import {withAggregate} from '#sugar';
 import T from '#things';
 
+const {Language} = T;
+
 export function processLanguageSpec(spec) {
   const {
     'meta.languageCode': code,
@@ -16,23 +19,21 @@ export function processLanguageSpec(spec) {
     ...strings
   } = spec;
 
-  if (!code) {
-    throw new Error(`Missing language code (file: ${file})`);
-  }
+  withAggregate({message: `Errors validating language spec`}, ({push}) => {
+    if (!code) {
+      push(new Error(`Missing language code (file: ${file})`));
+    }
 
-  if (!name) {
-    throw new Error(`Missing language name (${code})`);
-  }
+    if (!name) {
+      push(new Error(`Missing language name (${code})`));
+    }
+  });
 
-  const language = new T.Language();
+  return {code, intlCode, name, hidden, strings};
+}
 
-  Object.assign(language, {
-    code,
-    intlCode,
-    name,
-    hidden,
-    strings,
-  });
+export function initializeLanguageObject() {
+  const language = new Language();
 
   language.escapeHTML = string =>
     he.encode(string, {useNamedReferences: true});
@@ -43,5 +44,10 @@ export function processLanguageSpec(spec) {
 export async function processLanguageFile(file) {
   const contents = await readFile(file, 'utf-8');
   const spec = JSON.parse(contents);
-  return processLanguageSpec(spec);
+
+  const language = initializeLanguageObject();
+  const properties = processLanguageSpec(spec);
+  Object.assign(language, properties);
+
+  return language;
 }