2017-01-06 18:43:13 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
// copies the resources into the webapp directory.
|
2023-09-08 17:14:31 +08:00
|
|
|
|
2023-09-08 17:33:57 +08:00
|
|
|
import parseArgs from "minimist";
|
|
|
|
import * as chokidar from "chokidar";
|
|
|
|
import * as fs from "node:fs";
|
2023-09-08 17:49:41 +08:00
|
|
|
import _ from "lodash";
|
2023-11-08 17:06:13 +08:00
|
|
|
import { util } from "webpack";
|
2023-09-08 18:24:22 +08:00
|
|
|
import { Translations } from "matrix-web-i18n";
|
2023-09-08 17:33:57 +08:00
|
|
|
|
2023-09-08 17:14:31 +08:00
|
|
|
const I18N_BASE_PATH = "src/i18n/strings/";
|
2024-10-16 20:31:55 +08:00
|
|
|
const INCLUDE_LANGS = [...new Set([...fs.readdirSync(I18N_BASE_PATH)])]
|
2023-09-08 18:24:22 +08:00
|
|
|
.filter((fn) => fn.endsWith(".json"))
|
|
|
|
.map((f) => f.slice(0, -5));
|
2017-05-26 23:48:21 +08:00
|
|
|
|
2022-12-09 20:28:29 +08:00
|
|
|
const argv = parseArgs(process.argv.slice(2), {});
|
2017-01-06 18:43:13 +08:00
|
|
|
|
2019-02-18 23:11:41 +08:00
|
|
|
const watch = argv.w;
|
|
|
|
const verbose = argv.v;
|
2017-01-06 18:43:13 +08:00
|
|
|
|
2023-09-08 17:33:57 +08:00
|
|
|
function errCheck(err?: Error): void {
|
2017-01-06 18:43:13 +08:00
|
|
|
if (err) {
|
|
|
|
console.error(err.message);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 23:48:21 +08:00
|
|
|
// Check if webapp exists
|
2022-12-09 20:28:29 +08:00
|
|
|
if (!fs.existsSync("webapp")) {
|
|
|
|
fs.mkdirSync("webapp");
|
2017-05-26 23:48:21 +08:00
|
|
|
}
|
|
|
|
// Check if i18n exists
|
2022-12-09 20:28:29 +08:00
|
|
|
if (!fs.existsSync("webapp/i18n/")) {
|
|
|
|
fs.mkdirSync("webapp/i18n/");
|
2017-05-26 23:48:21 +08:00
|
|
|
}
|
|
|
|
|
2023-11-15 21:19:44 +08:00
|
|
|
const logWatch = (path: string) => {
|
2023-09-08 17:33:57 +08:00
|
|
|
if (verbose) {
|
2023-11-15 21:19:44 +08:00
|
|
|
console.log(`Watching: ${path}`);
|
2017-01-06 18:43:13 +08:00
|
|
|
}
|
2023-11-15 21:19:44 +08:00
|
|
|
};
|
2017-01-06 18:43:13 +08:00
|
|
|
|
2023-11-16 04:43:00 +08:00
|
|
|
function prepareLangFile(lang: string, dest: string): [filename: string, json: string] {
|
2024-10-16 20:31:55 +08:00
|
|
|
const path = I18N_BASE_PATH + lang + ".json";
|
2017-05-26 23:48:21 +08:00
|
|
|
|
2023-09-08 18:24:22 +08:00
|
|
|
let translations: Translations = {};
|
2024-10-16 20:31:55 +08:00
|
|
|
[path].forEach(function (f) {
|
2017-05-26 23:48:21 +08:00
|
|
|
if (fs.existsSync(f)) {
|
2017-10-11 16:56:38 +08:00
|
|
|
try {
|
2023-09-06 00:17:25 +08:00
|
|
|
translations = _.merge(translations, JSON.parse(fs.readFileSync(f).toString()));
|
2017-10-11 16:56:38 +08:00
|
|
|
} catch (e) {
|
2018-03-02 23:30:06 +08:00
|
|
|
console.error("Failed: " + f, e);
|
2017-10-11 16:56:38 +08:00
|
|
|
throw e;
|
|
|
|
}
|
2017-05-26 23:48:21 +08:00
|
|
|
}
|
|
|
|
});
|
2017-09-05 00:12:13 +08:00
|
|
|
|
2019-02-18 23:11:41 +08:00
|
|
|
const json = JSON.stringify(translations, null, 4);
|
|
|
|
const jsonBuffer = Buffer.from(json);
|
2023-11-08 17:04:09 +08:00
|
|
|
const digest = util.createHash("xxhash64").update(jsonBuffer).digest("hex").slice(0, 7);
|
2019-02-18 23:11:41 +08:00
|
|
|
const filename = `${lang}.${digest}.json`;
|
|
|
|
|
2023-11-16 04:43:00 +08:00
|
|
|
return [filename, json];
|
|
|
|
}
|
|
|
|
|
|
|
|
function genLangFile(dest: string, filename: string, json: string) {
|
2019-02-18 23:11:41 +08:00
|
|
|
fs.writeFileSync(dest + filename, json);
|
2017-05-26 23:48:21 +08:00
|
|
|
if (verbose) {
|
2019-02-18 23:11:41 +08:00
|
|
|
console.log("Generated language file: " + filename);
|
2017-05-26 23:48:21 +08:00
|
|
|
}
|
2017-05-23 21:12:53 +08:00
|
|
|
}
|
|
|
|
|
2023-09-08 17:33:57 +08:00
|
|
|
function genLangList(langFileMap: Record<string, string>): void {
|
|
|
|
const languages: Record<string, string> = {};
|
2022-12-09 20:28:29 +08:00
|
|
|
INCLUDE_LANGS.forEach(function (lang) {
|
2023-08-22 22:07:17 +08:00
|
|
|
const normalizedLanguage = lang.toLowerCase().replace("_", "-");
|
2022-12-09 20:28:29 +08:00
|
|
|
const languageParts = normalizedLanguage.split("-");
|
2017-05-26 23:48:21 +08:00
|
|
|
if (languageParts.length == 2 && languageParts[0] == languageParts[1]) {
|
2023-08-22 22:07:17 +08:00
|
|
|
languages[languageParts[0]] = langFileMap[lang];
|
2017-05-26 23:48:21 +08:00
|
|
|
} else {
|
2023-08-22 22:07:17 +08:00
|
|
|
languages[normalizedLanguage] = langFileMap[lang];
|
2017-05-23 21:12:53 +08:00
|
|
|
}
|
|
|
|
});
|
2022-12-09 20:28:29 +08:00
|
|
|
fs.writeFile("webapp/i18n/languages.json", JSON.stringify(languages, null, 4), function (err) {
|
2017-06-04 18:36:14 +08:00
|
|
|
if (err) {
|
2023-09-08 17:33:57 +08:00
|
|
|
console.error("Copy Error occured: " + err.message);
|
2017-06-04 18:36:14 +08:00
|
|
|
throw new Error("Failed to generate languages.json");
|
|
|
|
}
|
|
|
|
});
|
2017-05-26 23:48:21 +08:00
|
|
|
if (verbose) {
|
2017-06-04 18:36:14 +08:00
|
|
|
console.log("Generated languages.json");
|
2017-05-26 23:48:21 +08:00
|
|
|
}
|
|
|
|
}
|
2017-05-23 21:12:53 +08:00
|
|
|
|
2023-08-22 22:07:17 +08:00
|
|
|
/*
|
|
|
|
* watch the input files for a given language,
|
|
|
|
* regenerate the file, adding its content-hashed filename to langFileMap
|
|
|
|
* and regenerating languages.json with the new filename
|
|
|
|
*/
|
2023-09-08 17:33:57 +08:00
|
|
|
function watchLanguage(lang: string, dest: string, langFileMap: Record<string, string>): void {
|
2024-10-16 20:31:55 +08:00
|
|
|
const path = I18N_BASE_PATH + lang + ".json";
|
2019-02-18 23:11:41 +08:00
|
|
|
|
|
|
|
// 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
|
2023-09-08 17:43:44 +08:00
|
|
|
let makeLangDebouncer: ReturnType<typeof setTimeout>;
|
2023-09-08 17:33:57 +08:00
|
|
|
const makeLang = (): void => {
|
2019-02-18 23:11:41 +08:00
|
|
|
if (makeLangDebouncer) {
|
|
|
|
clearTimeout(makeLangDebouncer);
|
|
|
|
}
|
|
|
|
makeLangDebouncer = setTimeout(() => {
|
2023-11-16 04:43:00 +08:00
|
|
|
const [filename, json] = prepareLangFile(lang, dest);
|
|
|
|
genLangFile(dest, filename, json);
|
2022-12-09 20:28:29 +08:00
|
|
|
langFileMap[lang] = filename;
|
2019-02-18 23:11:41 +08:00
|
|
|
genLangList(langFileMap);
|
|
|
|
}, 500);
|
|
|
|
};
|
|
|
|
|
2024-10-16 20:31:55 +08:00
|
|
|
[path].forEach(function (f) {
|
2023-11-16 04:43:00 +08:00
|
|
|
chokidar
|
|
|
|
.watch(f, { ignoreInitial: true })
|
|
|
|
.on("ready", () => {
|
|
|
|
logWatch(f);
|
|
|
|
})
|
2023-11-15 21:19:44 +08:00
|
|
|
.on("add", makeLang)
|
|
|
|
.on("change", makeLang)
|
|
|
|
.on("error", errCheck);
|
2019-02-18 23:11:41 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// language resources
|
|
|
|
const I18N_DEST = "webapp/i18n/";
|
2023-09-08 17:33:57 +08:00
|
|
|
const I18N_FILENAME_MAP = INCLUDE_LANGS.reduce<Record<string, string>>((m, l) => {
|
2023-11-16 04:43:00 +08:00
|
|
|
const [filename, json] = prepareLangFile(l, I18N_DEST);
|
|
|
|
if (!watch) {
|
|
|
|
genLangFile(I18N_DEST, filename, json);
|
|
|
|
}
|
2023-08-22 22:07:17 +08:00
|
|
|
m[l] = filename;
|
2019-02-18 23:11:41 +08:00
|
|
|
return m;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
if (watch) {
|
2023-09-08 17:33:57 +08:00
|
|
|
INCLUDE_LANGS.forEach((l) => watchLanguage(l, I18N_DEST, I18N_FILENAME_MAP));
|
2023-11-16 04:43:00 +08:00
|
|
|
} else {
|
|
|
|
genLangList(I18N_FILENAME_MAP);
|
2019-02-18 23:11:41 +08:00
|
|
|
}
|