« get me outta code hell

language.js « data « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/language.js
blob: b71e55a2a130b227072b5757cd6a31740271feb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import {readFile} from 'node:fs/promises';

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,
    'meta.languageName': name,

    'meta.languageIntlCode': intlCode = null,
    'meta.hidden': hidden = false,

    ...strings
  } = spec;

  withAggregate({message: `Errors validating language spec`}, ({push}) => {
    if (!code) {
      push(new Error(`Missing language code (file: ${file})`));
    }

    if (!name) {
      push(new Error(`Missing language name (${code})`));
    }
  });

  return {code, intlCode, name, hidden, strings};
}

export function initializeLanguageObject() {
  const language = new Language();

  language.escapeHTML = string =>
    he.encode(string, {useNamedReferences: true});

  return language;
}

export async function processLanguageFile(file) {
  const contents = await readFile(file, 'utf-8');
  const spec = JSON.parse(contents);

  const language = initializeLanguageObject();
  const properties = processLanguageSpec(spec);
  Object.assign(language, properties);

  return language;
}