create locales dir if it doesnt exist

This commit is contained in:
Alex Grintsvayg 2017-06-21 08:23:21 -04:00
parent 741a88a7b4
commit 024ff3cb50

View file

@ -1,48 +1,48 @@
var extract = require('i18n-extract'); var extract = require("i18n-extract");
const fs = require('fs'); const fs = require("fs");
var path = '../app/dist/locales/en.json'; var dir = __dirname + "/../app/dist/locales";
var path = dir + "/en.json";
fs.writeFile(path, '{}', 'utf8', function(err) { if (!fs.existsSync(dir)) {
if(err) { fs.mkdirSync(dir);
return console.log(err); }
}
var enLocale = require(path);
const keys = extract.extractFromFiles(['js/**/*.{js,jsx}'], { fs.writeFile(path, "{}", "utf8", function(err) {
marker: '__', if (err) {
}); return console.log(err);
}
var enLocale = require(path);
let reports = []; const keys = extract.extractFromFiles(["js/**/*.{js,jsx}"], {
reports = reports.concat(extract.findMissing(enLocale, keys)); marker: "__",
});
if (reports.length > 0) { let reports = [];
fs.readFile(path, 'utf8', function readFileCallback(err, data){ reports = reports.concat(extract.findMissing(enLocale, keys));
if (err){
console.log(err);
} else {
localeObj = JSON.parse(data);
for (var i = 0; i < reports.length; i++) { if (reports.length > 0) {
// no need to care for other types than MISSING because starting file will always be empty fs.readFile(path, "utf8", function readFileCallback(err, data) {
if (reports[i].type === 'MISSING') { if (err) {
localeObj[reports[i].key] = reports[i].key; console.log(err);
} } else {
} localeObj = JSON.parse(data);
var json = JSON.stringify(localeObj, null, '\t'); //convert it back to json-string for (var i = 0; i < reports.length; i++) {
fs.writeFile(path, json, 'utf8', function callback(err) { // no need to care for other types than MISSING because starting file will always be empty
if (err) throw err; if (reports[i].type === "MISSING") {
console.log('Extracted all strings!'); localeObj[reports[i].key] = reports[i].key;
}); }
} }
var json = JSON.stringify(localeObj, null, "\t"); //convert it back to json-string
fs.writeFile(path, json, "utf8", function callback(err) {
if (err) {
throw err;
}
console.log("Extracted all strings!");
}); });
} }
});
}
}); });