-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·135 lines (116 loc) · 2.95 KB
/
index.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// i.e
// csv2jsonDirectoryConverter({
// input: "config/i18next/csv",
// output: "config/i18next/locales"
// }).start()
const path = require('path');
const fs = require('fs');
const csvParse = require('csv-parse');
function csv2jsonDirectoryConverter(cfg) {
const csvDir = cfg.input || 'input_csv';
const jsonDir = cfg.output || 'output_json';
let inputFiles = [];
let resource = {};
let languages = [];
function processCsvRecord(recordArr, index) {
// ignor empty
if (!recordArr[0]) {
return;
}
if (!languages.length) {
languages = recordArr.slice(1);
for(var i=0; i<languages.length; i++) {
if (!resource[languages[i]] || !resource[languages[i]]['translation']){
resource[languages[i]] = {
translation : {}
};
}
}
return;
}
var keysArr = recordArr[0].split('.');
var currentValue;
var prevValue;
var lastKey;
for(var i=0; i<languages.length; i++) {
if (!recordArr[i+1]) {
continue;
}
currentValue = resource[languages[i]]['translation'];
for(var j=0; j<keysArr.length; j++) {
prevValue = currentValue;
lastKey = keysArr[j];
if (! prevValue[lastKey] ) {
prevValue[lastKey] = {};
}
currentValue = prevValue[lastKey];
}
prevValue[lastKey] = recordArr[i+1];
}
}
function saveFile(dir, fileName, data) {
fs.writeFile(path.join(dir, fileName), data, function(err) {
if(err) {
console.log(err);
} else {
console.log(fileName+" file was saved!");
}
});
}
function saveLanguage(langName, langObj) {
saveFile(jsonDir, langName+'.json', JSON.stringify(langObj['translation'], null, " "));
// var tmpObj = {};
// tmpObj[langName] = langObj['translation'];
// saveFile(ymlDir, langName+'.yml', '---\n'+yamljs.stringify(tmpObj, 4));
}
function saveResults() {
var lang = {};
for(lang in resource) {
saveLanguage(lang, resource[lang]);
}
}
function processNextFile() {
var fileName = "";
if (!inputFiles.length) {
//console.log(JSON.stringify(resource, null, " "));
console.log("all converted");
saveResults();
return;
}
fileName = inputFiles.pop();
loadFile(path.join(csvDir, fileName));
}
function loadFile(fileName) {
// reset languages for each file
languages = [];
fs.createReadStream(fileName)
.pipe(csvParse())
.on('data', processCsvRecord)
.on('end', function(count){
processNextFile();
})
.on('error', function(error){
console.log(error.message);
});
}
function processFiles(err, files) {
if (err) {
throw err;
}
inputFiles = files;
processNextFile();
}
function readDir() {
fs.readdir(csvDir, processFiles);
}
return {
start: function() {
if (!csvDir || !jsonDir) {
console.log("Error: directories not specified");
return;
}
readDir();
}
};
}
module.exports = csv2jsonDirectoryConverter;