hedera-web/utils/extract-locale.js

88 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-12-18 13:35:16 +00:00
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);
}
2017-12-20 11:34:04 +00:00
let nTasks;
2017-12-18 13:35:16 +00:00
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);
}
2017-12-20 11:34:04 +00:00
nTasks = localeDirs.length;
});
2017-12-18 13:35:16 +00:00
});
});
function exportLocale (localeDir) {
let dstDir = `${lang}/${localeDir}`;
fs.mkdirp(dstDir, err => {
if (err) {
2017-12-20 11:34:04 +00:00
console.log(err);
onTaskEnd();
2017-12-18 13:35:16 +00:00
return;
}
let src, dst;
src = `${projectDir}/${localeDir}/en.json`;
dst = `${dstDir}/en.json`;
2017-12-20 11:34:04 +00:00
fs.copy(src, dst, onTaskEnd);
2017-12-18 13:35:16 +00:00
src = `${projectDir}/${localeDir}/${lang}.json`;
dst = `${dstDir}/${lang}.json`;
2017-12-20 11:34:04 +00:00
fs.copy(src, dst, onTaskEnd);
2017-12-18 13:35:16 +00:00
2017-12-20 11:34:04 +00:00
nTasks += 2;
onTaskEnd();
});
2017-12-18 13:35:16 +00:00
}
let output;
let archive;
2017-12-20 11:34:04 +00:00
function onTaskEnd() {
nTasks--;
if (nTasks > 0) return;
2017-12-18 13:35:16 +00:00
output = fs.createWriteStream(`${lang}.zip`);
2017-12-20 11:34:04 +00:00
output.on ('close', onArchiveClose);
2017-12-18 13:35:16 +00:00
archive = archiver('zip', {
zlib: { level: 9 }
});
archive.on ('error', err => {
throw err;
});
archive.pipe(output);
archive.directory(lang);
archive.finalize();
}
2017-12-20 11:34:04 +00:00
function onArchiveClose() {
2017-12-18 13:35:16 +00:00
fs.remove(lang);
console.log ('Export finalized!');
}