* Remove stale max-len disablements * Remove stale camelCase & naming-convention disablements * Remove stale ban-ts-comment disablements * Remove stale no-var disablements * Remove stale no-empty-property disablements * Remove stale react rule disablements * Remove stale no-constant-condition disablements * Remove stale no-unused-vars disablements * Remove stale disablements for disabled rules * fixup camelcase * Remove dead code * Tidy code * Tweak oxlint config * Use oxlint to apply jsx/tsx extension consistently * Fix import * Fix imports * Rename affected snapshots * Update more imports * Enable restriction ruleset * Make code comply with new rules * Make code comply with react/button-has-type * Make code comply with typescript/non-nullable-type-assertion-style * Comply with node/no-process-env * Comply with unicorn/prefer-node-protocol * Comply with unicorn/import-style * Comply with unicorn/no-process-exit * Comply with no-proto * Comply with node/handle-callback-err * Comply with import/no-commonjs * Comply with node/no-path-concat * Comply with unicorn/no-length-as-slice-end * Comply with unicorn/no-document-cookie * Comply with unicorn/prefer-module * Comply with typescript/prefer-literal-enum-member * Comply with jsx-a11y/anchor-ambiguous-text * Tweak oxlint config * Fix resolves * Iterate * Iterate * Iterate * Iterate * Iterate * Iterate * Iterate
89 lines
2.6 KiB
JavaScript
Executable File
89 lines
2.6 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
/*
|
|
Copyright 2026 Element Creations Ltd.
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
Please see LICENSE in the repository root for full details.
|
|
*/
|
|
|
|
// copies resources into the lib directory.
|
|
|
|
import parseArgs from "minimist";
|
|
import * as chokidar from "chokidar";
|
|
import path from "node:path";
|
|
import * as fs from "node:fs";
|
|
|
|
const argv = parseArgs(process.argv.slice(2), {});
|
|
|
|
const watch = argv.w;
|
|
const verbose = argv.v;
|
|
|
|
function errCheck(err: unknown): void {
|
|
if (err) {
|
|
console.error(err instanceof Error ? err.message : err);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
const I18N_BASE_PATH = "src/i18n/strings/";
|
|
const INCLUDE_LANGS = fs.readdirSync(I18N_BASE_PATH).filter((fn) => fn.endsWith(".json"));
|
|
|
|
// Ensure lib, lib/i18n and lib/i18n/strings all exist
|
|
fs.mkdirSync("lib/i18n/strings", { recursive: true });
|
|
|
|
type Translations = Record<string, Record<string, string> | string>;
|
|
|
|
function genLangFile(file: string, dest: string): void {
|
|
const translations: Translations = {};
|
|
[file].forEach(function (f) {
|
|
if (fs.existsSync(f)) {
|
|
try {
|
|
Object.assign(translations, JSON.parse(fs.readFileSync(f).toString()));
|
|
} catch (e) {
|
|
console.error("Failed: " + f, e);
|
|
throw e;
|
|
}
|
|
}
|
|
});
|
|
|
|
const json = JSON.stringify(translations, null, 4);
|
|
const filename = path.basename(file);
|
|
|
|
fs.writeFileSync(dest + filename, json);
|
|
if (verbose) {
|
|
console.log("Generated language file: " + filename);
|
|
}
|
|
}
|
|
|
|
/*
|
|
watch the input files for a given language,
|
|
regenerate the file, and regenerating languages.json with the new filename
|
|
*/
|
|
function watchLanguage(file: string, dest: string): void {
|
|
// XXX: Use a debounce because for some reason if we read the language
|
|
// file immediately after the FS event is received, the file contents
|
|
// appears empty. Possibly https://github.com/nodejs/node/issues/6112
|
|
let makeLangDebouncer: NodeJS.Timeout | undefined;
|
|
const makeLang = (): void => {
|
|
if (makeLangDebouncer) {
|
|
clearTimeout(makeLangDebouncer);
|
|
}
|
|
makeLangDebouncer = setTimeout(() => {
|
|
genLangFile(file, dest);
|
|
}, 500);
|
|
};
|
|
|
|
chokidar.watch(file).on("add", makeLang).on("change", makeLang).on("error", errCheck);
|
|
}
|
|
|
|
// language resources
|
|
const I18N_DEST = "lib/i18n/strings/";
|
|
INCLUDE_LANGS.forEach((file): void => {
|
|
genLangFile(I18N_BASE_PATH + file, I18N_DEST);
|
|
}, {});
|
|
|
|
if (watch) {
|
|
INCLUDE_LANGS.forEach((file) => watchLanguage(I18N_BASE_PATH + file, I18N_DEST));
|
|
}
|