diff --git a/lib/to-csv.js b/lib/to-csv.js new file mode 100644 index 0000000..c6760a2 --- /dev/null +++ b/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); + }); +}; diff --git a/lib/to-js.js b/lib/to-js.js new file mode 100644 index 0000000..6ac00a7 --- /dev/null +++ b/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); + } + }); +}; diff --git a/to-csv.js b/to-csv.js index b5992ca..557d54a 100644 --- a/to-csv.js +++ b/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); diff --git a/to-js.js b/to-js.js index e4836cc..0829c0f 100644 --- a/to-js.js +++ b/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);