Skip to content
This repository has been archived by the owner on Feb 15, 2019. It is now read-only.

Commit

Permalink
move to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelly Selden committed Jan 5, 2016
1 parent 80f7bf1 commit b0bc31d
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 117 deletions.
63 changes: 63 additions & 0 deletions lib/to-csv.js
@@ -0,0 +1,63 @@
var fs = require('fs');
var path = require('path');
var stringify = require('csv-stringify');
var eol = require('eol');
require('6to5/register');

module.exports = function(localesPath, csvPath) {
var locales = fs.readdirSync(localesPath);

var keys = [];
var rows = [];

function recurse(json, columnIndex, rowIndex, currentKey) {
for (var key in json) {
var value = json[key];
var newKey = currentKey;
if (newKey) {
newKey += '.';
}
newKey += key;
if (typeof value === 'object') {
rowIndex = recurse(value, columnIndex, rowIndex, newKey);
} else {
var row;
var index = keys.indexOf(newKey);
if (index !== -1) {
row = rows[index];
} else {
row = [];
rows.splice(rowIndex, 0, row);
keys.splice(rowIndex, 0, newKey);
}
for (var i in locales) {
if (i == columnIndex) {
row[i] = value;
} else if (!row[i]) {
row[i] = '';
}
}
rowIndex++;
}
}
return rowIndex;
}

for (var columnIndex in locales) {
var locale = locales[columnIndex];
var filePath = path.resolve(localesPath, locale, 'translations.js');
var json = require(filePath);
recurse(json, columnIndex, 0, '');
}

var lines = [];
lines.push(['key'].concat(locales.map(function(locale) { return locale.replace('.js', ''); })));
for (var i in keys) {
lines.push([keys[i]].concat(rows[i]));
}

stringify(lines, function(err, csv) {
var normalized = eol.auto(csv);
fs.writeFileSync(csvPath, normalized);
});
};
57 changes: 57 additions & 0 deletions lib/to-js.js
@@ -0,0 +1,57 @@
var fs = require('fs');
var path = require('path');
var parse = require('csv-parse');
var eol = require('eol');

module.exports = function(csvPath, localesPath, ignoreJshint) {
var csv = fs.readFileSync(csvPath, 'utf8');

parse(csv, function(err, lines) {
var i, columnIndex;
var locales = lines.shift().slice(1);

var objs = [];
for (i in locales) {
objs.push({});
}

function recurse(keySections, obj, value) {
var key = keySections[0];
if (keySections.length > 1) {
if (!obj[key]) {
obj[key] = {};
}
recurse(keySections.slice(1), obj[key], value);
} else {
obj[key] = value;
}
}

for (i in lines) {
var line = lines[i];
var key = line.shift();
var keySections = key.split('.');
for (columnIndex in line) {
var obj = objs[columnIndex];
var value = line[columnIndex];
recurse(keySections, obj, value);
}
}

for (columnIndex in locales) {
var locale = locales[columnIndex];
var localePath = path.join(localesPath, locale);
if (!fs.existsSync(localePath)) {
fs.mkdirSync(localePath);
}
var filePath = path.join(localePath, 'translations.js');
var jsonString = JSON.stringify(objs[columnIndex], null, 2);
var string = 'export default ' + jsonString + ';\n';
if (ignoreJshint) {
string = '/* jshint ignore:start */\n\n' + string + '\n/* jshint ignore:end */\n';
}
string = eol.auto(string);
fs.writeFileSync(filePath, string);
}
});
};
65 changes: 2 additions & 63 deletions to-csv.js
@@ -1,68 +1,7 @@
var fs = require('fs');
var path = require('path');
var stringify = require('csv-stringify');
var eol = require('eol');
var argv = require('yargs').argv;
require('6to5/register');
var toCsv = require('./lib/to-csv');

var localesPath = argv._[0];
var csvPath = argv._[1];

var locales = fs.readdirSync(localesPath);

var keys = [];
var rows = [];

function recurse(json, columnIndex, rowIndex, currentKey) {
for (var key in json) {
var value = json[key];
var newKey = currentKey;
if (newKey) {
newKey += '.';
}
newKey += key;
if (typeof value === 'object') {
rowIndex = recurse(value, columnIndex, rowIndex, newKey);
} else {
var row;
var index = keys.indexOf(newKey);
if (index !== -1) {
row = rows[index];
} else {
row = [];
rows.splice(rowIndex, 0, row);
keys.splice(rowIndex, 0, newKey);
}
for (var i in locales) {
if (i == columnIndex) {
row[i] = value;
} else if (!row[i]) {
row[i] = '';
}
}
rowIndex++;
}
}
return rowIndex;
}

for (var columnIndex in locales) {
var locale = locales[columnIndex];
var filePath = path.join(localesPath, locale, 'translations.js');
if (!path.isAbsolute(filePath)) {
filePath = './' + filePath;
}
var json = require(filePath);
recurse(json, columnIndex, 0, '');
}

var lines = [];
lines.push(['key'].concat(locales.map(function(locale) { return locale.replace('.js', ''); })));
for (var i in keys) {
lines.push([keys[i]].concat(rows[i]));
}

stringify(lines, function(err, csv) {
var normalized = eol.auto(csv);
fs.writeFileSync(csvPath, normalized);
});
toCsv(localesPath, csvPath);
56 changes: 2 additions & 54 deletions to-js.js
@@ -1,60 +1,8 @@
var fs = require('fs');
var path = require('path');
var parse = require('csv-parse');
var eol = require('eol');
var argv = require('yargs').argv;
var toJs = require('./lib/to-js');

var csvPath = argv._[0];
var localesPath = argv._[1];
var ignoreJshint = argv['jshint-ignore'];

var csv = fs.readFileSync(csvPath, 'utf8');

parse(csv, function(err, lines) {
var i, columnIndex;
var locales = lines.shift().slice(1);

var objs = [];
for (i in locales) {
objs.push({});
}

function recurse(keySections, obj, value) {
var key = keySections[0];
if (keySections.length > 1) {
if (!obj[key]) {
obj[key] = {};
}
recurse(keySections.slice(1), obj[key], value);
} else {
obj[key] = value;
}
}

for (i in lines) {
var line = lines[i];
var key = line.shift();
var keySections = key.split('.');
for (columnIndex in line) {
var obj = objs[columnIndex];
var value = line[columnIndex];
recurse(keySections, obj, value);
}
}

for (columnIndex in locales) {
var locale = locales[columnIndex];
var localePath = path.join(localesPath, locale);
if (!fs.existsSync(localePath)) {
fs.mkdirSync(localePath);
}
var filePath = path.join(localePath, 'translations.js');
var jsonString = JSON.stringify(objs[columnIndex], null, 2);
var string = 'export default ' + jsonString + ';\n';
if (ignoreJshint) {
string = '/* jshint ignore:start */\n\n' + string + '\n/* jshint ignore:end */\n';
}
string = eol.auto(string);
fs.writeFileSync(filePath, string);
}
});
toJs(csvPath, localesPath, ignoreJshint);

0 comments on commit b0bc31d

Please sign in to comment.