hedera-web/utils/extract-locale.js

84 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2018-02-05 12:58:17 +00:00
#!/usr/bin/node
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;
2018-02-05 12:58:17 +00:00
(async () => {
await fs.remove(lang);
projectDir = await fs.realpath(`${__dirname}/..`);
projectDir = realPath;
let len = projectDir.length + 1;
glob(`${projectDir}/**/locale/`, async (err, localeDirs) => {
if (err) throw err;
2017-12-18 13:35:16 +00:00
2018-02-05 12:58:17 +00:00
for (let localeDir of localeDirs) {
localeDir = localeDir.substr(len)
2017-12-18 13:35:16 +00:00
2018-02-05 12:58:17 +00:00
if (/^node_modules\//.test (localeDir))
continue;
2017-12-20 11:34:04 +00:00
2018-02-05 12:58:17 +00:00
await exportLocale(localeDir);
}
nTasks = localeDirs.length;
2017-12-18 13:35:16 +00:00
});
2018-02-05 12:58:17 +00:00
})();
2017-12-18 13:35:16 +00:00
2018-02-05 12:58:17 +00:00
async function exportLocale (localeDir) {
2017-12-18 13:35:16 +00:00
let dstDir = `${lang}/${localeDir}`;
2018-02-05 12:58:17 +00:00
try {
await fs.mkdirp(dstDir);
2017-12-18 13:35:16 +00:00
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;
2018-02-05 12:58:17 +00:00
}
catch (e) {
console.log(err);
}
2017-12-18 13:35:16 +00:00
2017-12-20 11:34:04 +00:00
nTasks--;
if (nTasks > 0) return;
2017-12-18 13:35:16 +00:00
2018-02-05 12:58:17 +00:00
let output = fs.createWriteStream(`${lang}.zip`);
output.on ('close', () =>{
fs.remove(lang);
console.log ('Export finalized!');
});
2017-12-18 13:35:16 +00:00
2018-02-05 12:58:17 +00:00
let archive = archiver('zip', {
2017-12-18 13:35:16 +00:00
zlib: { level: 9 }
});
archive.on ('error', err => {
throw err;
});
archive.pipe(output);
archive.directory(lang);
archive.finalize();
}