2017-01-06 11:43:13 +01:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
// copies the resources into the webapp directory.
|
|
|
|
//
|
|
|
|
|
2017-05-26 17:48:21 +02:00
|
|
|
// Languages are listed manually so we can choose when to include
|
|
|
|
// a translation in the app (because having a translation with only
|
|
|
|
// 3 strings translated is just frustrating)
|
|
|
|
// This could readily be automated, but it's nice to explicitly
|
|
|
|
// control when we languages are available.
|
|
|
|
const INCLUDE_LANGS = [
|
2017-06-01 15:38:36 +02:00
|
|
|
{'value': 'en_EN', 'label': 'English'},
|
2017-06-05 17:05:07 +02:00
|
|
|
{'value': 'en_US', 'label': 'English (US)'},
|
2017-06-01 15:38:36 +02:00
|
|
|
{'value': 'da', 'label': 'Dansk'},
|
2017-06-06 17:51:00 +02:00
|
|
|
{'value': 'el', 'label': 'Ελληνικά'},
|
2017-06-01 15:38:36 +02:00
|
|
|
{'value': 'nl', 'label': 'Nederlands'},
|
|
|
|
{'value': 'de_DE', 'label': 'Deutsch'},
|
|
|
|
{'value': 'fr', 'label': 'Français'},
|
2017-06-16 14:05:56 +02:00
|
|
|
{'value': 'ko', 'label': '한국어'},
|
2017-06-01 15:38:36 +02:00
|
|
|
{'value': 'pt', 'label': 'Português'},
|
|
|
|
{'value': 'pt_BR', 'label': 'Português do Brasil'},
|
|
|
|
{'value': 'ru', 'label': 'Русский'},
|
2017-06-02 23:05:46 +02:00
|
|
|
{'value': 'sv', 'label': 'Svenska'},
|
2017-06-01 15:38:36 +02:00
|
|
|
{'value': 'es', 'label': 'Español'},
|
2017-06-10 03:11:55 +02:00
|
|
|
{'value': 'th', 'label': 'ไทย'},
|
2017-06-06 17:51:00 +02:00
|
|
|
{'value': 'zh_Hans', 'label': '简体中文'}, // simplified chinese
|
|
|
|
{'value': 'zh_Hant', 'label': '繁體中文'}, // traditional chinese
|
2017-05-26 17:48:21 +02:00
|
|
|
];
|
|
|
|
|
2017-01-06 11:43:13 +01: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/...".
|
|
|
|
const COPY_LIST = [
|
2017-05-11 14:12:26 +02:00
|
|
|
["res/manifest.json", "webapp"],
|
2017-05-24 18:58:03 +02:00
|
|
|
["res/home.html", "webapp"],
|
2017-05-30 04:58:45 +02:00
|
|
|
["res/home/**", "webapp/home"],
|
2017-01-06 11:43:13 +01:00
|
|
|
["res/{media,vector-icons}/**", "webapp"],
|
2017-05-18 12:25:42 +02:00
|
|
|
["res/flags/*", "webapp/flags/"],
|
2017-01-06 11:43:13 +01:00
|
|
|
["src/skins/vector/{fonts,img}/**", "webapp"],
|
|
|
|
["node_modules/emojione/assets/svg/*", "webapp/emojione/svg/"],
|
2017-04-21 18:36:48 +02:00
|
|
|
["node_modules/emojione/assets/png/*", "webapp/emojione/png/"],
|
2017-05-23 15:12:53 +02:00
|
|
|
["./config.json", "webapp", { directwatch: 1 }],
|
2017-01-06 11:43:13 +01:00
|
|
|
];
|
|
|
|
|
2017-05-26 17:48:21 +02:00
|
|
|
INCLUDE_LANGS.forEach(function(l) {
|
|
|
|
COPY_LIST.push([
|
2017-06-01 15:38:36 +02:00
|
|
|
l.value, "webapp/i18n/", { lang: 1 },
|
2017-05-26 17:48:21 +02:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2017-01-06 11:43:13 +01:00
|
|
|
const parseArgs = require('minimist');
|
|
|
|
const Cpx = require('cpx');
|
|
|
|
const chokidar = require('chokidar');
|
2017-05-23 15:12:53 +02:00
|
|
|
const fs = require('fs');
|
|
|
|
const rimraf = require('rimraf');
|
|
|
|
|
2017-01-06 11:43:13 +01:00
|
|
|
const argv = parseArgs(
|
|
|
|
process.argv.slice(2), {}
|
|
|
|
);
|
|
|
|
|
|
|
|
var watch = argv.w;
|
|
|
|
var verbose = argv.v;
|
|
|
|
|
|
|
|
function errCheck(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err.message);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 17:48:21 +02: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/');
|
|
|
|
}
|
|
|
|
|
2017-01-06 11:43:13 +01:00
|
|
|
function next(i, err) {
|
|
|
|
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] || {};
|
2017-05-23 15:12:53 +02:00
|
|
|
let cpx = undefined;
|
2017-01-06 11:43:13 +01:00
|
|
|
|
2017-05-26 17:48:21 +02:00
|
|
|
if (!opts.lang) {
|
2017-05-23 15:12:53 +02:00
|
|
|
cpx = new Cpx.Cpx(source, dest);
|
|
|
|
}
|
2017-01-06 11:43:13 +01:00
|
|
|
|
2017-05-26 17:48:21 +02:00
|
|
|
if (verbose && cpx) {
|
2017-01-06 11:43:13 +01:00
|
|
|
cpx.on("copy", (event) => {
|
|
|
|
console.log(`Copied: ${event.srcPath} --> ${event.dstPath}`);
|
|
|
|
});
|
|
|
|
cpx.on("remove", (event) => {
|
|
|
|
console.log(`Removed: ${event.path}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-23 15:12:53 +02:00
|
|
|
const cb = (err) => { next(i + 1, err) };
|
2017-01-06 11:43:13 +01: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.
|
2017-05-23 15:12:53 +02:00
|
|
|
const copy = () => { cpx.copy(errCheck) };
|
2017-01-06 11:43:13 +01:00
|
|
|
chokidar.watch(source)
|
|
|
|
.on('add', copy)
|
|
|
|
.on('change', copy)
|
|
|
|
.on('ready', cb)
|
|
|
|
.on('error', errCheck);
|
2017-05-26 17:48:21 +02:00
|
|
|
} else if (opts.lang) {
|
|
|
|
const reactSdkFile = 'node_modules/matrix-react-sdk/src/i18n/strings/' + source + '.json';
|
|
|
|
const riotWebFile = 'src/i18n/strings/' + source + '.json';
|
|
|
|
|
|
|
|
const translations = {};
|
|
|
|
const makeLang = () => { genLangFile(source, dest) };
|
|
|
|
[reactSdkFile, riotWebFile].forEach(function(f) {
|
|
|
|
chokidar.watch(f)
|
|
|
|
.on('add', makeLang)
|
|
|
|
.on('change', makeLang)
|
|
|
|
//.on('ready', cb) We'd have to do this when both files are ready
|
|
|
|
.on('error', errCheck);
|
|
|
|
});
|
2017-05-23 15:12:53 +02:00
|
|
|
next(i + 1, err);
|
2017-01-06 11:43:13 +01:00
|
|
|
} else {
|
|
|
|
cpx.on('watch-ready', cb);
|
|
|
|
cpx.on("watch-error", cb);
|
|
|
|
cpx.watch();
|
|
|
|
}
|
2017-05-26 17:48:21 +02:00
|
|
|
} else if (opts.lang) {
|
|
|
|
genLangFile(source, dest);
|
2017-05-23 15:12:53 +02:00
|
|
|
next(i + 1, err);
|
2017-01-06 11:43:13 +01:00
|
|
|
} else {
|
|
|
|
cpx.copy(cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 17:48:21 +02:00
|
|
|
function genLangFile(lang, dest) {
|
|
|
|
const reactSdkFile = 'node_modules/matrix-react-sdk/src/i18n/strings/' + lang + '.json';
|
|
|
|
const riotWebFile = 'src/i18n/strings/' + lang + '.json';
|
|
|
|
|
|
|
|
const translations = {};
|
|
|
|
[reactSdkFile, riotWebFile].forEach(function(f) {
|
|
|
|
if (fs.existsSync(f)) {
|
|
|
|
Object.assign(
|
|
|
|
translations,
|
|
|
|
JSON.parse(fs.readFileSync(f).toString())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
fs.writeFileSync(dest + lang + '.json', JSON.stringify(translations, null, 4));
|
|
|
|
if (verbose) {
|
|
|
|
console.log("Generated language file: " + lang);
|
|
|
|
}
|
2017-05-23 15:12:53 +02:00
|
|
|
}
|
|
|
|
|
2017-05-26 17:48:21 +02:00
|
|
|
function genLangList() {
|
|
|
|
const languages = {};
|
|
|
|
INCLUDE_LANGS.forEach(function(lang) {
|
2017-06-01 15:38:36 +02:00
|
|
|
const normalizedLanguage = lang.value.toLowerCase().replace("_", "-");
|
2017-05-26 17:48:21 +02:00
|
|
|
const languageParts = normalizedLanguage.split('-');
|
|
|
|
if (languageParts.length == 2 && languageParts[0] == languageParts[1]) {
|
2017-06-01 15:38:36 +02:00
|
|
|
languages[languageParts[0]] = {'fileName': lang.value + '.json', 'label': lang.label};
|
2017-05-26 17:48:21 +02:00
|
|
|
} else {
|
2017-06-01 15:38:36 +02:00
|
|
|
languages[normalizedLanguage] = {'fileName': lang.value + '.json', 'label': lang.label};
|
2017-05-23 15:12:53 +02:00
|
|
|
}
|
|
|
|
});
|
2017-06-04 12:36:14 +02:00
|
|
|
fs.writeFile('webapp/i18n/languages.json', JSON.stringify(languages, null, 4), function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error("Copy Error occured: " + err);
|
|
|
|
throw new Error("Failed to generate languages.json");
|
|
|
|
}
|
|
|
|
});
|
2017-05-26 17:48:21 +02:00
|
|
|
if (verbose) {
|
2017-06-04 12:36:14 +02:00
|
|
|
console.log("Generated languages.json");
|
2017-05-26 17:48:21 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-23 15:12:53 +02:00
|
|
|
|
2017-05-26 17:48:21 +02:00
|
|
|
genLangList();
|
|
|
|
next(0);
|