2017-01-06 05:43:13 -05:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
// copies the resources into the webapp directory.
|
2023-09-08 05:14:31 -04:00
|
|
|
|
2023-09-08 05:33:57 -04:00
|
|
|
import parseArgs from "minimist";
|
|
|
|
import * as chokidar from "chokidar";
|
|
|
|
import * as fs from "node:fs";
|
2023-09-08 05:49:41 -04:00
|
|
|
import _ from "lodash";
|
|
|
|
import { Cpx } from "cpx";
|
2023-09-08 05:33:57 -04:00
|
|
|
import * as loaderUtils from "loader-utils";
|
2023-09-08 06:24:22 -04:00
|
|
|
import { Translations } from "matrix-web-i18n";
|
2023-09-08 05:33:57 -04:00
|
|
|
|
2023-09-08 06:24:22 -04:00
|
|
|
const REACT_I18N_BASE_PATH = "node_modules/matrix-react-sdk/src/i18n/strings/";
|
2023-09-08 05:14:31 -04:00
|
|
|
const I18N_BASE_PATH = "src/i18n/strings/";
|
2023-09-08 06:24:22 -04:00
|
|
|
const INCLUDE_LANGS = [...new Set([...fs.readdirSync(I18N_BASE_PATH), ...fs.readdirSync(REACT_I18N_BASE_PATH)])]
|
|
|
|
.filter((fn) => fn.endsWith(".json"))
|
|
|
|
.map((f) => f.slice(0, -5));
|
2017-05-26 11:48:21 -04:00
|
|
|
|
2017-01-06 05:43:13 -05:00
|
|
|
// 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/...".
|
2023-09-08 05:33:57 -04:00
|
|
|
const COPY_LIST: [
|
|
|
|
sourceGlob: string,
|
|
|
|
outputPath: string,
|
|
|
|
opts?: {
|
|
|
|
directwatch?: 1;
|
|
|
|
},
|
|
|
|
][] = [
|
2022-11-08 08:27:20 -05:00
|
|
|
["res/apple-app-site-association", "webapp"],
|
2017-05-11 08:12:26 -04:00
|
|
|
["res/manifest.json", "webapp"],
|
2020-10-01 09:05:07 -04:00
|
|
|
["res/sw.js", "webapp"],
|
2019-02-07 12:00:01 -05:00
|
|
|
["res/welcome.html", "webapp"],
|
|
|
|
["res/welcome/**", "webapp/welcome"],
|
2018-04-12 20:49:52 -04:00
|
|
|
["res/themes/**", "webapp/themes"],
|
2018-12-31 18:22:47 -05:00
|
|
|
["res/vector-icons/**", "webapp/vector-icons"],
|
2020-04-30 08:51:50 -04:00
|
|
|
["res/decoder-ring/**", "webapp/decoder-ring"],
|
2018-12-31 18:22:47 -05:00
|
|
|
["node_modules/matrix-react-sdk/res/media/**", "webapp/media"],
|
2021-05-30 00:48:11 -04:00
|
|
|
["node_modules/@matrix-org/olm/olm_legacy.js", "webapp", { directwatch: 1 }],
|
2017-05-23 09:12:53 -04:00
|
|
|
["./config.json", "webapp", { directwatch: 1 }],
|
2020-02-05 09:28:44 -05:00
|
|
|
["contribute.json", "webapp"],
|
2017-01-06 05:43:13 -05:00
|
|
|
];
|
|
|
|
const argv = parseArgs(process.argv.slice(2), {});
|
|
|
|
|
2019-02-18 10:11:41 -05:00
|
|
|
const watch = argv.w;
|
|
|
|
const verbose = argv.v;
|
2017-01-06 05:43:13 -05:00
|
|
|
|
2023-09-08 05:33:57 -04:00
|
|
|
function errCheck(err?: Error): void {
|
2017-01-06 05:43:13 -05:00
|
|
|
if (err) {
|
|
|
|
console.error(err.message);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 11:48:21 -04:00
|
|
|
// Check if webapp exists
|
|
|
|
if (!fs.existsSync("webapp")) {
|
|
|
|
fs.mkdirSync("webapp");
|
|
|
|
}
|
|
|
|
// Check if i18n exists
|
|
|
|
if (!fs.existsSync("webapp/i18n/")) {
|
|
|
|
fs.mkdirSync("webapp/i18n/");
|
|
|
|
}
|
|
|
|
|
2023-09-08 05:33:57 -04:00
|
|
|
function next(i: number, err?: Error): void {
|
2017-01-06 05:43:13 -05:00
|
|
|
errCheck(err);
|
|
|
|
|
|
|
|
if (i >= COPY_LIST.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ent = COPY_LIST[i];
|
|
|
|
const source = ent[0];
|
|
|
|
const dest = ent[1];
|
|
|
|
const opts = ent[2] || {};
|
2023-09-08 05:49:41 -04:00
|
|
|
const cpx = new Cpx(source, dest);
|
2017-01-06 05:43:13 -05:00
|
|
|
|
2023-09-08 05:33:57 -04:00
|
|
|
if (verbose) {
|
2017-01-06 05:43:13 -05:00
|
|
|
cpx.on("copy", (event) => {
|
|
|
|
console.log(`Copied: ${event.srcPath} --> ${event.dstPath}`);
|
|
|
|
});
|
|
|
|
cpx.on("remove", (event) => {
|
|
|
|
console.log(`Removed: ${event.path}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-08 05:33:57 -04:00
|
|
|
const cb = (err?: Error): void => {
|
2017-05-23 09:12:53 -04:00
|
|
|
next(i + 1, err);
|
|
|
|
};
|
2017-01-06 05:43:13 -05:00
|
|
|
|
|
|
|
if (watch) {
|
|
|
|
if (opts.directwatch) {
|
|
|
|
// cpx -w creates a watcher for the parent of any files specified,
|
|
|
|
// which in the case of config.json is '.', which inevitably takes
|
|
|
|
// ages to crawl. So we create our own watcher on the files
|
|
|
|
// instead.
|
2023-09-08 05:33:57 -04:00
|
|
|
const copy = (): void => {
|
2017-05-23 09:12:53 -04:00
|
|
|
cpx.copy(errCheck);
|
|
|
|
};
|
2017-01-06 05:43:13 -05:00
|
|
|
chokidar.watch(source).on("add", copy).on("change", copy).on("ready", cb).on("error", errCheck);
|
|
|
|
} else {
|
|
|
|
cpx.on("watch-ready", cb);
|
|
|
|
cpx.on("watch-error", cb);
|
|
|
|
cpx.watch();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cpx.copy(cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 05:33:57 -04:00
|
|
|
function genLangFile(lang: string, dest: string): string {
|
2023-09-08 06:24:22 -04:00
|
|
|
const reactSdkFile = REACT_I18N_BASE_PATH + lang + ".json";
|
2023-09-08 05:14:31 -04:00
|
|
|
const riotWebFile = I18N_BASE_PATH + lang + ".json";
|
2017-05-26 11:48:21 -04:00
|
|
|
|
2023-09-08 06:24:22 -04:00
|
|
|
let translations: Translations = {};
|
2017-05-26 11:48:21 -04:00
|
|
|
[reactSdkFile, riotWebFile].forEach(function (f) {
|
|
|
|
if (fs.existsSync(f)) {
|
2017-10-11 04:56:38 -04:00
|
|
|
try {
|
2023-09-05 12:17:25 -04:00
|
|
|
translations = _.merge(translations, JSON.parse(fs.readFileSync(f).toString()));
|
2017-10-11 04:56:38 -04:00
|
|
|
} catch (e) {
|
2018-03-02 10:30:06 -05:00
|
|
|
console.error("Failed: " + f, e);
|
2017-10-11 04:56:38 -04:00
|
|
|
throw e;
|
|
|
|
}
|
2017-05-26 11:48:21 -04:00
|
|
|
}
|
|
|
|
});
|
2017-09-04 12:12:13 -04:00
|
|
|
|
2019-02-18 10:11:41 -05:00
|
|
|
const json = JSON.stringify(translations, null, 4);
|
|
|
|
const jsonBuffer = Buffer.from(json);
|
2023-09-08 05:33:57 -04:00
|
|
|
const digest = loaderUtils.getHashDigest(jsonBuffer, null, "hex", 7);
|
2019-02-18 10:11:41 -05:00
|
|
|
const filename = `${lang}.${digest}.json`;
|
|
|
|
|
|
|
|
fs.writeFileSync(dest + filename, json);
|
2017-05-26 11:48:21 -04:00
|
|
|
if (verbose) {
|
2019-02-18 10:11:41 -05:00
|
|
|
console.log("Generated language file: " + filename);
|
2017-05-26 11:48:21 -04:00
|
|
|
}
|
2019-02-18 10:11:41 -05:00
|
|
|
|
|
|
|
return filename;
|
2017-05-23 09:12:53 -04:00
|
|
|
}
|
|
|
|
|
2023-09-08 05:33:57 -04:00
|
|
|
function genLangList(langFileMap: Record<string, string>): void {
|
|
|
|
const languages: Record<string, string> = {};
|
2017-05-26 11:48:21 -04:00
|
|
|
INCLUDE_LANGS.forEach(function (lang) {
|
2023-08-22 10:07:17 -04:00
|
|
|
const normalizedLanguage = lang.toLowerCase().replace("_", "-");
|
2017-05-26 11:48:21 -04:00
|
|
|
const languageParts = normalizedLanguage.split("-");
|
|
|
|
if (languageParts.length == 2 && languageParts[0] == languageParts[1]) {
|
2023-08-22 10:07:17 -04:00
|
|
|
languages[languageParts[0]] = langFileMap[lang];
|
2017-05-26 11:48:21 -04:00
|
|
|
} else {
|
2023-08-22 10:07:17 -04:00
|
|
|
languages[normalizedLanguage] = langFileMap[lang];
|
2017-05-23 09:12:53 -04:00
|
|
|
}
|
|
|
|
});
|
2017-06-04 06:36:14 -04:00
|
|
|
fs.writeFile("webapp/i18n/languages.json", JSON.stringify(languages, null, 4), function (err) {
|
|
|
|
if (err) {
|
2023-09-08 05:33:57 -04:00
|
|
|
console.error("Copy Error occured: " + err.message);
|
2017-06-04 06:36:14 -04:00
|
|
|
throw new Error("Failed to generate languages.json");
|
|
|
|
}
|
|
|
|
});
|
2017-05-26 11:48:21 -04:00
|
|
|
if (verbose) {
|
2017-06-04 06:36:14 -04:00
|
|
|
console.log("Generated languages.json");
|
2017-05-26 11:48:21 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-23 09:12:53 -04:00
|
|
|
|
2023-08-22 10:07:17 -04: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 05:33:57 -04:00
|
|
|
function watchLanguage(lang: string, dest: string, langFileMap: Record<string, string>): void {
|
2023-09-08 06:24:22 -04:00
|
|
|
const reactSdkFile = REACT_I18N_BASE_PATH + lang + ".json";
|
2023-09-08 05:14:31 -04:00
|
|
|
const riotWebFile = I18N_BASE_PATH + lang + ".json";
|
2019-02-18 10:11:41 -05: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 05:43:44 -04:00
|
|
|
let makeLangDebouncer: ReturnType<typeof setTimeout>;
|
2023-09-08 05:33:57 -04:00
|
|
|
const makeLang = (): void => {
|
2019-02-18 10:11:41 -05:00
|
|
|
if (makeLangDebouncer) {
|
|
|
|
clearTimeout(makeLangDebouncer);
|
|
|
|
}
|
|
|
|
makeLangDebouncer = setTimeout(() => {
|
|
|
|
const filename = genLangFile(lang, dest);
|
|
|
|
langFileMap[lang] = filename;
|
|
|
|
genLangList(langFileMap);
|
|
|
|
}, 500);
|
|
|
|
};
|
|
|
|
|
|
|
|
[reactSdkFile, riotWebFile].forEach(function (f) {
|
|
|
|
chokidar.watch(f).on("add", makeLang).on("change", makeLang).on("error", errCheck);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// language resources
|
|
|
|
const I18N_DEST = "webapp/i18n/";
|
2023-09-08 05:33:57 -04:00
|
|
|
const I18N_FILENAME_MAP = INCLUDE_LANGS.reduce<Record<string, string>>((m, l) => {
|
2023-08-22 10:07:17 -04:00
|
|
|
const filename = genLangFile(l, I18N_DEST);
|
|
|
|
m[l] = filename;
|
2019-02-18 10:11:41 -05:00
|
|
|
return m;
|
|
|
|
}, {});
|
|
|
|
genLangList(I18N_FILENAME_MAP);
|
|
|
|
|
|
|
|
if (watch) {
|
2023-09-08 05:33:57 -04:00
|
|
|
INCLUDE_LANGS.forEach((l) => watchLanguage(l, I18N_DEST, I18N_FILENAME_MAP));
|
2019-02-18 10:11:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// non-language resources
|
2017-05-26 11:48:21 -04:00
|
|
|
next(0);
|