-
Notifications
You must be signed in to change notification settings - Fork 1
/
formatFiles.js
242 lines (212 loc) · 6.8 KB
/
formatFiles.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
Copyright 2017 Rentalia
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict';
var MagicCSV = require("magic-csv");
var csvNonPlural = new MagicCSV({trim: true});
var csvPlurals = new MagicCSV({trim: true});
var fs = require("fs");
var path = require("path");
var del = require('delete');
var _fileName = "";
var moduleClassAndroid = require('./android-format');
var moduleClassiOS = require('./ios-format');
var CALLBACK_MESSAGE = {
ERROR: {message:'Error generating files!', type: 'error'},
SUCCESS: 'success'
};
function formatCSV(fileName, path, pathPlurals, callback) {
_fileName = fileName.replace('.csv','');
csvNonPlural.readFile(path, function(err, stats) {
if (err){
console.log('There was an error: ' + err);
callback(err);
}else{
if (pathPlurals != ''){
csvPlurals.readFile(pathPlurals, function(err, stats) {
if (err){
console.log('There was an error: ' + err);
callback(err);
}else{
pluralsFileCreator(csvNonPlural, csvPlurals, function(result){
console.log('result plurals ' + result);
callback(result);
});
}
});
}else{
nonPluralFileCreator(csvNonPlural,function(result){
callback(result);
});
}
}
});
}
//Generates langs files
function pluralsFileCreator(nonPluralCSV, pluralsCSV, callback){
var dictPls = pluralsDictionaryData(pluralsCSV);
var dictNonPls = nonPluralsDictionaryData(nonPluralCSV);
console.log('DICT PLURALS: '+ JSON.stringify(dictPls));
var htmlAndroid = "";
var htmliOS = "";
for (var lang in dictNonPls){
htmliOS += wrapperModuleiOSHeaderHTML;
for (var identifier in dictPls){
for(var value in dictPls[identifier]){
if (value == lang){
htmlAndroid += wrapperModuleAndroidBodyName(identifier);
htmliOS += wrapperModuleiOSBodyName(identifier);
for (var val in dictPls[identifier][value]){
var valIdentifier = dictPls[identifier][value][val];
if(valIdentifier != ''){
htmliOS += wrapperModuleiOSBodyHTML(val,valIdentifier);
htmlAndroid += wrapperModuleAndroidBodyHTML(val,valIdentifier);
}
}
htmlAndroid += moduleClassAndroid.footerHTMLAndroid();
htmliOS += moduleClassiOS.footerHTMLiOS();
}
}
}
var data = dictNonPls[lang];
htmliOS += moduleClassiOS.footerCloseHTMLiOS();
goToModules(data, lang, htmlAndroid,htmliOS,_fileName)
htmlAndroid = "";
htmliOS = "";
}
callback(CALLBACK_MESSAGE.SUCCESS);
}
function wrapperModuleiOSHeaderHTML(){
return moduleClassiOS.headerHTMLiOS();
}
function wrapperModuleAndroidBodyName(identifier){
return moduleClassAndroid.bodyNameForPlurals(identifier);
}
function wrapperModuleiOSBodyName(identifier){
return moduleClassiOS.bodyWithIDHTMLiOS(identifier);
}
function wrapperModuleiOSBodyHTML(val, identifier){
return moduleClassiOS.bodyHTMLiOS(val,identifier);
}
function wrapperModuleAndroidBodyHTML(val, identifier){
return moduleClassAndroid.bodyHTMLAndroid(val,identifier);
}
function wrapperModuleiOSFooterHTML(){
return moduleClassiOS.footerHTMLiOS();
}
function wrapperModuleAndroidFooterHMTL(){
return moduleClassAndroid.footerHTMLAndroid();
}
function wrapperModuleiOSFooterCloseHTML(){
return moduleClassiOS.footerCloseHTMLiOS();
}
function goToModules(data, lang, htmlAndroid,htmliOS, fileName){
moduleClassAndroid.toAndroid(data, lang, htmlAndroid,fileName);
moduleClassiOS.toiOS(data,lang,fileName);
moduleClassiOS.toiOSPlurals(htmliOS,lang,fileName);
}
function nonPluralFileCreator(csv, callback){
var dictLangs = nonPluralsDictionaryData(csv);
//There are at least a key column and one or more language columns.
if (dictLangs){
for (var lang in dictLangs){
moduleClassiOS.toiOS(dictLangs[lang],lang,_fileName);
moduleClassAndroid.toAndroid(dictLangs[lang],lang,{},_fileName);
}
callback(CALLBACK_MESSAGE.SUCCESS);
}else {
callback(CALLBACK_MESSAGE.ERROR);
}
}
//Get Dictionary for non plural file
function nonPluralsDictionaryData(csv){
var columns = csv.getCols();
var dictLangs = {};
if (Object.keys(columns).length > 1){
var objects = csv.getObjects();
dictLangs = iterateThroughLangs(objects);
}
return dictLangs;
}
//Get dictionary for plurals file
function pluralsDictionaryData(csv){
csv.drop_empty_rows = true;
var dictPlurals = iterateThroughPlurals(csv.getObjects());
return dictPlurals;
}
function iterateThroughPlurals(rows){
var langValues = {};
var dict = {};
var k = ''
var pls = '';
if(rows){
for (var i = 0;i < Object.keys(rows).length; i++){
var entry = rows[i];
for (var key in entry){
if (key == 'KEY' || key == 'key' || key == 'Key'){
k = key
}else if (key == 'PLS' || key == 'pls' || key == 'Pls'){
if (entry[key] != ''){
pls = entry[key];
}
}else{
if (dict[pls] == undefined || dict[pls] == 'undefined'){
langValues = {};
}else{
dict[pls] = dict[pls] || {};
}
langValues[key] = langValues[key] || {};
langValues[key][entry[k]] = entry[key];
dict[pls] = langValues;
}
}
}
}
return dict;
}
function iterateThroughLangs(rows){
var langValues = {};
var k = ''
if(rows){
for (var i = 0;i < Object.keys(rows).length; i++){
var entry = rows[i];
for (var key in entry){
if (key == 'KEY' || key == 'key' || key == 'Key'){
k = key
}
if (key != 'key' && key != 'KEY' && key != 'Key') {
langValues[key] = langValues[key] || {};
langValues[key][entry[k]] = entry[key];
}
}
}
}
return langValues;
}
function deleteFiles(){
var filePath = './download/*.xml';
del.sync(['foo/*.js']);
}
function saveInDisk(path,data){
console.log("PATH TO SAVE DISK: " + path);
fs.writeFile(path, data, function(error) {
if (error) {
console.error("write error: " + error.message);
} else {
console.log("Successful Write to " + path);
}
});
}
exports = module.exports = {
formatCSV: formatCSV,
saveInDisk: saveInDisk,
};