Automate generating the list of available languages for the badge

Note that getMessage in the badge depends on having a complete set of
translations, unlike the webextension, which will fallback to the
default for a string.
This commit is contained in:
Arlo Breault 2019-08-22 12:28:26 -04:00
parent 1e33ae830f
commit 1c550599b8
2 changed files with 23 additions and 9 deletions

View file

@ -2,6 +2,7 @@
/* global require, process */
var { writeFileSync, readdirSync, statSync } = require('fs');
var { execSync, spawn } = require('child_process');
// All files required.
@ -36,9 +37,11 @@ var SHARED_FILES = [
'_locales',
];
var concatJS = function(outDir, init, outFile) {
var concatJS = function(outDir, init, outFile, pre) {
var files = FILES.concat(`init-${init}.js`);
execSync(`cat ${files.join(' ')} > ${outDir}/${outFile}`);
var outPath = `${outDir}/${outFile}`;
writeFileSync(outPath, pre, 'utf8');
execSync(`cat ${files.join(' ')} >> ${outPath}`);
};
var copyTranslations = function(outDir) {
@ -46,6 +49,20 @@ var copyTranslations = function(outDir) {
execSync(`cp -rf translation/* ${outDir}/_locales/`);
};
var availableLangs = function() {
let out = "const availableLangs = new Set([\n";
let dirs = readdirSync('translation').filter((f) => {
const s = statSync(`translation/${f}`);
return s.isDirectory();
});
dirs.push('en_US');
dirs.sort();
dirs = dirs.map(d => ` '${d}',`);
out += dirs.join("\n");
out += "\n]);\n\n";
return out;
};
var tasks = new Map();
var task = function(key, msg, func) {
@ -76,7 +93,7 @@ task('build', 'build the snowflake proxy', function() {
execSync(`rm -rf ${outDir}`);
execSync(`cp -r ${STATIC}/ ${outDir}/`);
copyTranslations(outDir);
concatJS(outDir, 'badge', 'embed.js');
concatJS(outDir, 'badge', 'embed.js', availableLangs());
console.log('Snowflake prepared.');
});
@ -85,13 +102,13 @@ task('webext', 'build the webextension', function() {
execSync(`git clean -f -x -d ${outDir}/`);
execSync(`cp -r ${STATIC}/{${SHARED_FILES.join(',')}} ${outDir}/`, { shell: '/bin/bash' });
copyTranslations(outDir);
concatJS(outDir, 'webext', 'snowflake.js');
concatJS(outDir, 'webext', 'snowflake.js', '');
console.log('Webextension prepared.');
});
task('node', 'build the node binary', function() {
execSync('mkdir -p build');
concatJS('build', 'node', 'snowflake.js');
concatJS('build', 'node', 'snowflake.js', '');
console.log('Node prepared.');
});