Convert copy-res to typescript

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2023-09-08 10:33:57 +01:00
parent 501f01cdd5
commit c944a273d0
No known key found for this signature in database
GPG key ID: A2B008A5F49F5D0D
8 changed files with 127 additions and 38 deletions

View file

@ -1,16 +1,27 @@
#!/usr/bin/env node
const loaderUtils = require("loader-utils");
// copies the resources into the webapp directory.
import parseArgs from "minimist";
import * as chokidar from "chokidar";
import * as fs from "node:fs";
import * as _ from "lodash";
import * as Cpx from "cpx";
import * as loaderUtils from "loader-utils";
const I18N_BASE_PATH = "src/i18n/strings/";
const INCLUDE_LANGS = fs.readdirSync(I18N_BASE_PATH).filter((fn) => fn.endsWith(".json"));
// cpx includes globbed parts of the filename in the destination, but excludes
// common parents. Hence, "res/{a,b}/**": the output will be "dest/a/..." and
// "dest/b/...".
const COPY_LIST = [
const COPY_LIST: [
sourceGlob: string,
outputPath: string,
opts?: {
directwatch?: 1;
},
][] = [
["res/apple-app-site-association", "webapp"],
["res/manifest.json", "webapp"],
["res/sw.js", "webapp"],
@ -24,19 +35,12 @@ const COPY_LIST = [
["./config.json", "webapp", { directwatch: 1 }],
["contribute.json", "webapp"],
];
const parseArgs = require("minimist");
const Cpx = require("cpx");
const chokidar = require("chokidar");
const fs = require("fs");
const _ = require("lodash");
const argv = parseArgs(process.argv.slice(2), {});
const watch = argv.w;
const verbose = argv.v;
function errCheck(err) {
function errCheck(err?: Error): void {
if (err) {
console.error(err.message);
process.exit(1);
@ -52,7 +56,7 @@ if (!fs.existsSync("webapp/i18n/")) {
fs.mkdirSync("webapp/i18n/");
}
function next(i, err) {
function next(i: number, err?: Error): void {
errCheck(err);
if (i >= COPY_LIST.length) {
@ -63,13 +67,9 @@ function next(i, err) {
const source = ent[0];
const dest = ent[1];
const opts = ent[2] || {};
let cpx = undefined;
const cpx = new Cpx.Cpx(source, dest);
if (!opts.lang) {
cpx = new Cpx.Cpx(source, dest);
}
if (verbose && cpx) {
if (verbose) {
cpx.on("copy", (event) => {
console.log(`Copied: ${event.srcPath} --> ${event.dstPath}`);
});
@ -78,7 +78,7 @@ function next(i, err) {
});
}
const cb = (err) => {
const cb = (err?: Error): void => {
next(i + 1, err);
};
@ -88,7 +88,7 @@ function next(i, err) {
// which in the case of config.json is '.', which inevitably takes
// ages to crawl. So we create our own watcher on the files
// instead.
const copy = () => {
const copy = (): void => {
cpx.copy(errCheck);
};
chokidar.watch(source).on("add", copy).on("change", copy).on("ready", cb).on("error", errCheck);
@ -102,7 +102,7 @@ function next(i, err) {
}
}
function genLangFile(lang, dest) {
function genLangFile(lang: string, dest: string): string {
const reactSdkFile = "node_modules/matrix-react-sdk/src/i18n/strings/" + lang + ".json";
const riotWebFile = I18N_BASE_PATH + lang + ".json";
@ -120,7 +120,7 @@ function genLangFile(lang, dest) {
const json = JSON.stringify(translations, null, 4);
const jsonBuffer = Buffer.from(json);
const digest = loaderUtils.getHashDigest(jsonBuffer, null, null, 7);
const digest = loaderUtils.getHashDigest(jsonBuffer, null, "hex", 7);
const filename = `${lang}.${digest}.json`;
fs.writeFileSync(dest + filename, json);
@ -131,8 +131,8 @@ function genLangFile(lang, dest) {
return filename;
}
function genLangList(langFileMap) {
const languages = {};
function genLangList(langFileMap: Record<string, string>): void {
const languages: Record<string, string> = {};
INCLUDE_LANGS.forEach(function (lang) {
const normalizedLanguage = lang.toLowerCase().replace("_", "-");
const languageParts = normalizedLanguage.split("-");
@ -144,7 +144,7 @@ function genLangList(langFileMap) {
});
fs.writeFile("webapp/i18n/languages.json", JSON.stringify(languages, null, 4), function (err) {
if (err) {
console.error("Copy Error occured: " + err);
console.error("Copy Error occured: " + err.message);
throw new Error("Failed to generate languages.json");
}
});
@ -158,15 +158,15 @@ function genLangList(langFileMap) {
* regenerate the file, adding its content-hashed filename to langFileMap
* and regenerating languages.json with the new filename
*/
function watchLanguage(lang, dest, langFileMap) {
function watchLanguage(lang: string, dest: string, langFileMap: Record<string, string>): void {
const reactSdkFile = "node_modules/matrix-react-sdk/src/i18n/strings/" + lang + ".json";
const riotWebFile = I18N_BASE_PATH + lang + ".json";
// 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;
const makeLang = () => {
let makeLangDebouncer: number;
const makeLang = (): void => {
if (makeLangDebouncer) {
clearTimeout(makeLangDebouncer);
}
@ -184,7 +184,7 @@ function watchLanguage(lang, dest, langFileMap) {
// language resources
const I18N_DEST = "webapp/i18n/";
const I18N_FILENAME_MAP = INCLUDE_LANGS.reduce((m, l) => {
const I18N_FILENAME_MAP = INCLUDE_LANGS.reduce<Record<string, string>>((m, l) => {
const filename = genLangFile(l, I18N_DEST);
m[l] = filename;
return m;
@ -192,7 +192,7 @@ const I18N_FILENAME_MAP = INCLUDE_LANGS.reduce((m, l) => {
genLangList(I18N_FILENAME_MAP);
if (watch) {
INCLUDE_LANGS.forEach((l) => watchLanguage(l.value, I18N_DEST, I18N_FILENAME_MAP));
INCLUDE_LANGS.forEach((l) => watchLanguage(l, I18N_DEST, I18N_FILENAME_MAP));
}
// non-language resources