« get me outta code hell

extract processLanguageFile from upd8.js, fix building - 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-07 07:35:40 -0400
committer(quasar) nebula <qznebula@protonmail.com>2022-12-07 07:35:40 -0400
commit2532ec9ef61146deadb2a49159b92ef50c5d8835 (patch)
treed14112ae88ebea310bf7094767fc1bfdb2ff04ec /src
parentbf14c334235513c0188d71fdb53bd72f34e22e39 (diff)
extract processLanguageFile from upd8.js, fix building
Diffstat (limited to 'src')
-rw-r--r--src/data/language.js40
-rw-r--r--src/repl.js2
-rwxr-xr-xsrc/upd8.js50
3 files changed, 45 insertions, 47 deletions
diff --git a/src/data/language.js b/src/data/language.js
new file mode 100644
index 00000000..da9528f2
--- /dev/null
+++ b/src/data/language.js
@@ -0,0 +1,40 @@
+import {readFile} from 'fs/promises';
+
+// It stands for "HTML Entities", apparently. Cursed.
+import he from 'he';
+
+import T from './things/index.js';
+
+// TODO: define somewhere besides upd8.js obviously
+export async function processLanguageFile(file) {
+  const contents = await readFile(file, 'utf-8');
+  const json = JSON.parse(contents);
+
+  const code = json['meta.languageCode'];
+  if (!code) {
+    throw new Error(`Missing language code (file: ${file})`);
+  }
+  delete json['meta.languageCode'];
+
+  const intlCode = json['meta.languageIntlCode'] ?? null;
+  delete json['meta.languageIntlCode'];
+
+  const name = json['meta.languageName'];
+  if (!name) {
+    throw new Error(`Missing language name (${code})`);
+  }
+  delete json['meta.languageName'];
+
+  const hidden = json['meta.hidden'] ?? false;
+  delete json['meta.hidden'];
+
+  const language = new T.Language();
+  language.code = code;
+  language.intlCode = intlCode;
+  language.name = name;
+  language.hidden = hidden;
+  language.escapeHTML = (string) =>
+    he.encode(string, {useNamedReferences: true});
+  language.strings = json;
+  return language;
+}
diff --git a/src/repl.js b/src/repl.js
index 08e83a5c..69e2fe05 100644
--- a/src/repl.js
+++ b/src/repl.js
@@ -10,7 +10,7 @@ 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 {processLanguageFile} from './data/language.js';
 
 import * as serialize from './util/serialize.js';
 import * as sugar from './util/sugar.js';
diff --git a/src/upd8.js b/src/upd8.js
index a70864de..0de724f2 100755
--- a/src/upd8.js
+++ b/src/upd8.js
@@ -34,15 +34,11 @@
 import * as path from 'path';
 import {fileURLToPath} from 'url';
 
-// It stands for "HTML Entities", apparently. Cursed.
-import he from 'he';
-
 import chroma from 'chroma-js';
 
 import {
   copyFile,
   mkdir,
-  readFile,
   stat,
   symlink,
   writeFile,
@@ -64,10 +60,9 @@ import {isMain} from './util/node-utils.js';
 
 import CacheableObject from './data/things/cacheable-object.js';
 
+import {processLanguageFile} from './data/language.js';
 import {serializeThings} from './data/serialize.js';
 
-import T from './data/things/index.js';
-
 import {
   filterDuplicateDirectories,
   filterReferenceErrors,
@@ -1602,45 +1597,6 @@ function generateRedirectHTML(title, target, {language}) {
   ]);
 }
 
-// TODO: define somewhere besides upd8.js obviously
-export async function processLanguageFile(file) {
-  const contents = await readFile(file, 'utf-8');
-  const json = JSON.parse(contents);
-
-  const code = json['meta.languageCode'];
-  if (!code) {
-    throw new Error(`Missing language code (file: ${file})`);
-  }
-  delete json['meta.languageCode'];
-
-  const intlCode = json['meta.languageIntlCode'] ?? null;
-  delete json['meta.languageIntlCode'];
-
-  const name = json['meta.languageName'];
-  if (!name) {
-    throw new Error(`Missing language name (${code})`);
-  }
-  delete json['meta.languageName'];
-
-  const hidden = json['meta.hidden'] ?? false;
-  delete json['meta.hidden'];
-
-  if (json['meta.baseDirectory']) {
-    logWarn`(${code}) Language JSON still has unused meta.baseDirectory`;
-    delete json['meta.baseDirectory'];
-  }
-
-  const language = new T.Language();
-  language.code = code;
-  language.intlCode = intlCode;
-  language.name = name;
-  language.hidden = hidden;
-  language.escapeHTML = (string) =>
-    he.encode(string, {useNamedReferences: true});
-  language.strings = json;
-  return language;
-}
-
 // Wrapper function for running a function once for all languages.
 async function wrapLanguages(fn, {languages, writeOneLanguage = null}) {
   const k = writeOneLanguage;
@@ -2616,7 +2572,9 @@ async function main() {
   logInfo`Written!`;
 }
 
-if (isMain(import.meta.url) || path.basename(process.argv[1]) === 'hsmusic') {
+// TODO: isMain detection isn't consistent across platforms here
+/* eslint-disable-next-line no-constant-condition */
+if (true || isMain(import.meta.url) || path.basename(process.argv[1]) === 'hsmusic') {
   main()
     .catch((error) => {
       if (error instanceof AggregateError) {