hedera-web/utils/extract-locale.js

88 lines
1.6 KiB
JavaScript

var glob = require('glob');
var fs = require('fs-extra');
var path = require('path');
var archiver = require('archiver');
let lang = process.argv[2];
if (lang == null)
{
let baseName = path.basename(process.argv[1]);
console.log(`Usage: ${baseName} language_code`);
process.exit(1);
}
let nTasks;
let projectDir;
fs.remove(lang, () => {
fs.realpath(`${__dirname}/..`, (err, realPath) => {
projectDir = realPath;
let len = projectDir.length + 1;
glob(`${projectDir}/**/locale/`, (err, localeDirs) => {
for (let localeDir of localeDirs) {
localeDir = localeDir.substr(len)
if (/^node_modules\//.test (localeDir))
continue;
exportLocale(localeDir);
}
nTasks = localeDirs.length;
});
});
});
function exportLocale (localeDir) {
let dstDir = `${lang}/${localeDir}`;
fs.mkdirp(dstDir, err => {
if (err) {
console.log(err);
onTaskEnd();
return;
}
let src, dst;
src = `${projectDir}/${localeDir}/en.json`;
dst = `${dstDir}/en.json`;
fs.copy(src, dst, onTaskEnd);
src = `${projectDir}/${localeDir}/${lang}.json`;
dst = `${dstDir}/${lang}.json`;
fs.copy(src, dst, onTaskEnd);
nTasks += 2;
onTaskEnd();
});
}
let output;
let archive;
function onTaskEnd() {
nTasks--;
if (nTasks > 0) return;
output = fs.createWriteStream(`${lang}.zip`);
output.on ('close', onArchiveClose);
archive = archiver('zip', {
zlib: { level: 9 }
});
archive.on ('error', err => {
throw err;
});
archive.pipe(output);
archive.directory(lang);
archive.finalize();
}
function onArchiveClose() {
fs.remove(lang);
console.log ('Export finalized!');
}