This repository has been archived by the owner on Feb 15, 2019. It is now read-only.
/
to-js.js
57 lines (51 loc) · 1.55 KB
/
to-js.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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);
}
});
};