-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
409 lines (347 loc) · 12.1 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
* @author Yiğit Can YILMAZ
* @date 12.08.2022
*/
/* EXISTENCE TRUTH TABLE
remote local xlsx result (0: not exist, 1: exist)
-------------------------------
0 0 0 NoChange
0 0 1 XlsxAdded
0 1 0 Removed
0 1 1 Removed
1 0 0 RemoteAdded
1 0 1 XlsxAdded
1 1 0 Removed
1 1 1 NoChange
################################################
CHANGE TRUTH TABLE
remote local xlsx result (0: not changed, 1 changed)
-------------------------------
0 0 0 NoChange
0 0 1 XlsxChanged
1 0 0 RemoteChanged
1 0 1 XlsxChanged
1 1 1 XlsxChanged
*/
const { Console } = require('console');
const fs = require('fs');
const XLSX = require("xlsx");
var RCU = require("./rcu.js");
const TABLES_FOLDER_PATH = 'tables'
const xlsx_file_path = 'tables/Localizable.xlsm'
const MAX_COL_WIDTH = 80
const KEY_TEMPLATE_VERSION = "templateVersion"
const FILEPATH_CACHE = 'cache.json'
const FILEPATH_PREFIX_LOCALIZABLE = 'localizable/Localizable_'
const KEY_PREFIX_LOCALIZABLE = 'localizable_'
const KEY_OF_KEYS = "Key"
const KEY_OF_VALUES = "Value"
const KEY_OF_SUPPORTED_LANGUAGES = "supportedLanguages"
const LOCALIZABLE_FOLDER_PATH = 'localizable'
var languageCodes = [];
const KeyStatus = {
Removed: 0,
RemoteAdded: 1,
XlsxAdded: 2,
RemoteChanged: 3,
XlsxChanged: 4,
NoChange: 5
}
if(isFileExist(FILEPATH_CACHE)){
languageCodes = getCache()[KEY_OF_SUPPORTED_LANGUAGES];
}
function init(){
ensureFilesCreated();
initFirebase()
RCU.getTemplateWithoutSaving().then(data => {
if (isNewVersionExist(data)){
updateXlsx(data)
console.log("Sync Successful. Workbook Up to Date. ✅");
} else {
console.log("No New Version. Workbook Up to Date ✅");
}
});
}
function initFirebase(){
try {
RCU.initFirebaseApp("main");
} catch (err) {
console.log("Already initialized: FirebaseApp ✅");
}
}
function publishTemplate(){
initFirebase();
ensureFilesCreated();
RCU.getTemplateWithoutSaving().then(data => {
if (isNewVersionExist(data)){
console.log("New Version Exist. 👀");
console.log("Changes Merged with New Version. ✅");
}
var mergedTemplate = getMergedLocalizable(data);
var updatedTemplate = data;
for(i in languageCodes){
var languageCode = languageCodes[i];
updatedTemplate.parameters[KEY_PREFIX_LOCALIZABLE+languageCode].defaultValue.value = JSON.stringify(mergedTemplate.parameters[KEY_PREFIX_LOCALIZABLE+languageCode].defaultValue.value);
};
RCU.publishEditedTemplate(updatedTemplate).then(result => {
console.log("Published Successfully 🚀");
init();
});
});
}
function getCache(){
return JSON.parse(fs.readFileSync(FILEPATH_CACHE, 'utf8'));
}
function updateCache(data){
fs.writeFileSync(FILEPATH_CACHE, beautifyJson(data), 'utf8');
}
function updateXlsx(template){
ensureFilesCreated();
var workbook = XLSX.readFile(xlsx_file_path);
for (var i in languageCodes){
var languageCode = languageCodes[i];
var parameter = template.parameters[KEY_PREFIX_LOCALIZABLE+languageCode];
if (parameter !== undefined){
updateWorkbookPage(workbook, languageCode, parameter);
updateTemplateVersionCache(template.version.versionNumber);
}
}
}
function updateWorkbookPage(workbook, languageCode, parameter) {
var localizable = parameter.defaultValue.value;
fs.writeFileSync(FILEPATH_PREFIX_LOCALIZABLE + languageCode + '.json', beautifyJson(JSON.parse(localizable)), 'utf8');
var localizableData = jsonToArray(escape(JSON.parse(localizable)));
if (workbook.SheetNames.includes(languageCode)){
var nonRelevantData = XLSX.utils.sheet_to_json(workbook.Sheets[languageCode]).filter(it => it[KEY_OF_KEYS] === undefined);
nonRelevantData.forEach(element => {
localizableData.push(element)
});
var sheet = XLSX.utils.json_to_sheet(localizableData);
sheet["!cols"] = [ { wch: MAX_COL_WIDTH } ];
workbook.Sheets[languageCode] = sheet;
} else {
var sheet = XLSX.utils.json_to_sheet(jsonToArray(localizable));
sheet["!cols"] = [ { wch: MAX_COL_WIDTH } ];
XLSX.utils.book_append_sheet(workbook, sheet, languageCode, { origin: "A1" });
}
XLSX.writeFile(workbook, xlsx_file_path);
}
function updateTemplateVersionCache(newVersion){
var cache = getCache();
cache[KEY_TEMPLATE_VERSION] = newVersion;
updateCache(cache);
}
function beautifyJson(json){
return JSON.stringify(json, null, 2)
}
function isJSON(str) {
try {
return (JSON.parse(str) && !!str);
} catch (e) {
return false;
}
}
function jsonToArray(json){
var result = [];
for(const [key, value] of Object.entries(json)){
var obj = {};
obj[KEY_OF_KEYS] = key;
obj[KEY_OF_VALUES] = value;
result.push(obj);
};
return result;
}
function escape(json){
var result = {};
for(const [key, value] of Object.entries(json)){
result[key] = value.replace("\n", "\\n").replace("\"", "\\\"");
};
return result;
}
function ensureFilesCreated(){
if(!isFileExist(FILEPATH_CACHE)){
var initialCache ={ "templateVersion": "0", "supportedLanguages": ["tr"]}
updateCache(initialCache);
languageCodes = getCache()[KEY_OF_SUPPORTED_LANGUAGES];
}
fs.mkdirSync(LOCALIZABLE_FOLDER_PATH, { recursive: true })
languageCodes.forEach(languageCode => {
var localizationFilePath = FILEPATH_PREFIX_LOCALIZABLE + languageCode + ".json"
if(!isFileExist(localizationFilePath)){
fs.writeFileSync(localizationFilePath, "{}", 'utf8');
}
})
fs.mkdirSync(TABLES_FOLDER_PATH, { recursive: true })
if (!isFileExist(xlsx_file_path)){
var workbook = XLSX.utils.book_new();
} else {
var workbook = XLSX.readFile(xlsx_file_path);
}
for (var i in languageCodes){
var languageCode = languageCodes[i];
if (!workbook.SheetNames.includes(languageCode)){
XLSX.utils.book_append_sheet(workbook, [[KEY_OF_KEYS, KEY_OF_VALUES]], languageCode, { origin: "A1" })
}
}
XLSX.writeFile(workbook, xlsx_file_path);
}
function isFileExist(filePath){
try {
var fd = fs.openSync(filePath);
var stat = fs.fstatSync(fd);
fs.close(fd, (err) => {});
if (stat.isFile){
return true;
}
return false;
} catch (error) {
return false
}
}
function getLocalJson(languageCode) {
return JSON.parse(fs.readFileSync(FILEPATH_PREFIX_LOCALIZABLE + languageCode + '.json', 'utf8'));
}
function getRemoteJson(template, languageCode) {
return JSON.parse(template.parameters[KEY_PREFIX_LOCALIZABLE+languageCode].defaultValue.value);
}
function getXlsxJson(languageCode) {
var xlsxFile = XLSX.readFile(xlsx_file_path);
return sheetJsonToLocalizableJson(XLSX.utils.sheet_to_json(xlsxFile.Sheets[languageCode]).filter(it => it[KEY_OF_KEYS] !== undefined));
}
function getMergedLocalizable(template){
var updatedTemplate = template;
//ensureFilesCreated();
for (var l in languageCodes){
var languageCode = languageCodes[l];
var localJson = getLocalJson(languageCode);
var remoteJson = getRemoteJson(template, languageCode);
var xlsxJson = getXlsxJson(languageCode);
var allKeys = [];
var mergedObject = {};
for(const [key, value] of Object.entries(localJson)){
//languageListLocal.push([i, localJson[i]]);
if (!allKeys.includes(key)){
allKeys.push(key);
}
};
for(const [key, value] of Object.entries(remoteJson)){
//languageListRemote.push([i, remoteJson[i]]);
if (!allKeys.includes(key)){
allKeys.push(key);
}
};
for(const [key, value] of Object.entries(xlsxJson)){
//languageListXlsx.push([i, xlsxJson[i]]);
if (!allKeys.includes(key)){
allKeys.push(key);
}
};
allKeys.forEach(i => {
var difference = getDifferenceWithKey(i, remoteJson, localJson, xlsxJson);
switch (difference) {
case KeyStatus.XlsxAdded:
mergedObject[i] = xlsxJson[i];
break;
case KeyStatus.RemoteAdded:
mergedObject[i] = remoteJson[i];
break;
case KeyStatus.Removed:
//will not add to mergedObject
break;
case KeyStatus.RemoteChanged:
mergedObject[i] = remoteJson[i];
break;
case KeyStatus.XlsxChanged:
mergedObject[i] = xlsxJson[i];
break;
case KeyStatus.NoChange:
mergedObject[i] = xlsxJson [i];
break;
default:
break;
};
});
updatedTemplate.parameters[KEY_PREFIX_LOCALIZABLE+languageCode].defaultValue.value = mergedObject;
};
return updatedTemplate;
}
function getDifferenceWithKey(key, remoteJson, localJson, xlsxJson){
var remoteValue = remoteJson[key]
var localValue = localJson[key]
var xlsxValue = xlsxJson[key]
// this statement checks 1-0-0 and 1-0-1 (see: existence truth table)
if (remoteValue !== undefined && localValue === undefined){
if (xlsxValue === undefined){
return KeyStatus.RemoteAdded;
} else {
return KeyStatus.XlsxAdded;
}
}
// this statement checks 0-0-1 and 0-1-1 (see: existence truth table)
if (remoteValue === undefined && xlsxValue !== undefined){
if (localValue === undefined){
return KeyStatus.XlsxAdded;
} else {
return KeyStatus.Removed;
}
}
// this statement checks 1-1-0 and 0-1-0 (see: existence truth table)
if (localValue !== undefined && xlsxValue === undefined){
if(remoteValue === undefined){
return KeyStatus.Removed;
} else {
return KeyStatus.Removed;
}
}
//this statement checks 1-0-0 and 1-0-1 (see: change truth table)
if (remoteValue !== localValue){
if(localValue === xlsxValue){
return KeyStatus.RemoteChanged;
} else {
return KeyStatus.XlsxChanged;
}
}
//this statement checks 0-0-1 (see: change truth table)
if (remoteValue === localValue && xlsxValue !== localValue){
return KeyStatus.XlsxChanged;
}
return KeyStatus.NoChange;
}
function unescape(json){
return json.replace("\\n", "\n").replace("\\\"", "\"")
}
function sheetJsonToLocalizableJson(json){
var result = {};
for(const [key, value] of Object.entries(json)){
result[value[KEY_OF_KEYS]] = unescape(value[KEY_OF_VALUES]);
};
return result;
}
function isNewVersionExist(template){
var cache = getCache();
var cachedTemplateVersion = cache.templateVersion;
if (cachedTemplateVersion === undefined){
cache[KEY_TEMPLATE_VERSION] = 0;
cachedTemplateVersion = 0;
}
var remoteTemplateVersion = Number(template.version.versionNumber);
return remoteTemplateVersion>cachedTemplateVersion;
}
const action = process.argv[2];
switch (action) {
case "fetch":
console.log("data has been fetching...⏳");
init();
break;
case "publish":
console.log("publish started. ⏳");
publishTemplate();
break;
default:
console.log(`Invalid argument: ${action}. Please try "fetch" or "publish".`);
//init();
break;
};
module.exports ={
init, publishTemplate
}