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 nDirs; let projectDir; fs.remove(lang, () => { fs.realpath(`${__dirname}/..`, (err, realPath) => { projectDir = realPath; let len = projectDir.length + 1; glob(`${projectDir}/**/locale/`, (err, localeDirs) => { nDirs = localeDirs.length * 2; for (let localeDir of localeDirs) { localeDir = localeDir.substr(len) if (/^node_modules\//.test (localeDir)) continue; exportLocale(localeDir); } }) }); }); function exportLocale (localeDir) { let dstDir = `${lang}/${localeDir}`; fs.mkdirp(dstDir, err => { if (err) { onError(err); return; } let src, dst; src = `${projectDir}/${localeDir}/en.json`; dst = `${dstDir}/en.json`; fs.copy(src, dst, onDirEnd); src = `${projectDir}/${localeDir}/${lang}.json`; dst = `${dstDir}/${lang}.json`; fs.copy(src, dst, onDirEnd); }); } function onError (err) { console.log(err); onDirEnd(); } let output; let archive; function onDirEnd() { nDirs--; if (nDirs > 0) return; output = fs.createWriteStream(`${lang}.zip`); output.on ('close', onArchiveEnd); archive = archiver('zip', { zlib: { level: 9 } }); archive.on ('error', err => { throw err; }); archive.pipe(output); archive.directory(lang); archive.finalize(); } function onArchiveEnd() { fs.remove(lang); console.log ('Export finalized!'); }