This repository has been archived by the owner on Apr 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
480 lines (401 loc) · 14.8 KB
/
gulpfile.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
'use strict';
const fs = require('fs');
const path = require('path');
const fetch = require('node-fetch');
const admin = require('firebase-admin');
const onesky = require('@brainly/onesky-utils');
// OneSky auth
const SKYAPP_PROJECT_ID = '359388';
const SKYAPP_PUBLIC_KEY = 'e0DfHgNmzrc67zt3RabZRWcYpkSISL1W';
const SKYAPP_SECRET_KEY = process.env.SKYAPP_SECRET_KEY;
// Firebase auth
const CREDENTIALS_FILE = process.env.GOOGLE_APPLICATION_CREDENTIALS || 'creds.json';
const getServiceAccount = () => require(path.resolve(CREDENTIALS_FILE));
// important constants
const TRANSLATION_SOURCE_FILE = 'rc.json';
const WRAPPING_KEY = 'rc';
const RC_PREFIX = 'v2_';
const DEFAULT_LANGUAGE = 'cs';
const DEFAULT_FALLBACK = 'en';
const TRANSLATED_LANGUAGES = ['en', 'sk'];
const FALLBACK_LANGUAGE = {
'en': DEFAULT_LANGUAGE,
'sk': DEFAULT_LANGUAGE
};
const LANGUAGE_TO_SKYAPP = {
'en': 'en-GB'
};
const DEFAULT_RC_LANGUAGE_VALUE = 'DEFAULT';
const LANGUAGE_TO_RC = {
'cs': 'Cz value',
'sk': 'Sk value',
'en': DEFAULT_RC_LANGUAGE_VALUE,
'Base': 'iOS base'
};
const GET_DEFAULTS_SUPPORTED = ['cs', 'sk'];
var currentVueKey = '';
async function uploadStrings(keepStrings = true) {
console.log(`Sending ${TRANSLATION_SOURCE_FILE} for translation`);
const options = {
secret: SKYAPP_SECRET_KEY,
apiKey: SKYAPP_PUBLIC_KEY,
projectId: SKYAPP_PROJECT_ID,
language: DEFAULT_LANGUAGE,
fileName: TRANSLATION_SOURCE_FILE,
format: 'HIERARCHICAL_JSON',
content: fs.readFileSync(TRANSLATION_SOURCE_FILE).toString(),
keepStrings // avoid deleting all translations with an erroneous upload
};
try {
await onesky.postFile(options);
} catch (e) {
console.error(`Failed to upload translation: ${JSON.stringify(e)}`);
}
}
async function forceUploadStrings() {
await uploadStrings(false);
}
async function downloadStrings() {
console.log(`Fetching translation of ${TRANSLATION_SOURCE_FILE}`);
const options = {
secret: SKYAPP_SECRET_KEY,
apiKey: SKYAPP_PUBLIC_KEY,
projectId: SKYAPP_PROJECT_ID,
fileName: TRANSLATION_SOURCE_FILE,
format: 'I18NEXT_MULTILINGUAL_JSON'
};
try {
return await onesky.getMultilingualFile(options);
} catch (e) {
console.error(`Failed to download translation: ${JSON.stringify(e)}`);
return '';
}
}
async function processAndUploadDownloadedStrings() {
const translationFile = await downloadStrings();
if (translationFile === '') {
return;
}
const content = JSON.parse(translationFile);
var translation = {};
const allLanguages = [...TRANSLATED_LANGUAGES, DEFAULT_LANGUAGE]
for (const language of allLanguages) {
const key = LANGUAGE_TO_SKYAPP[language] || language;
let data = {};
if (content.hasOwnProperty(key)) {
data = content[key]['translation'];
} else {
console.warn(`Language ${language} not found in OneSky`);
}
translation[language] = data;
}
const processedTranslation = buildI18n(translation);
await updateRemoteConfig(processedTranslation);
}
function buildI18n(content) {
var resultingTranslation = {};
for (const key of Object.keys(content)) {
let currentTranslation = content[key];
normalizeTranslations(content, key);
currentVueKey = key;
currentTranslation = processByRegex(currentTranslation);
resultingTranslation[key] = currentTranslation;
}
return resultingTranslation;
}
function normalizeTranslations(translations, language) {
const fallback = getFallback(language);
const data = translations[language];
if (language !== DEFAULT_LANGUAGE) {
for (const key of Object.keys(translations[DEFAULT_LANGUAGE])) {
if (!data.hasOwnProperty(key)) {
data[key] = translate(translations, fallback, key);
}
}
}
}
function getFallback(language) {
if (FALLBACK_LANGUAGE.hasOwnProperty(language)) {
return FALLBACK_LANGUAGE[language];
}
return DEFAULT_FALLBACK;
}
function translate(translation, language, key) {
const strings = translation[language] || {};
let result;
if (strings.hasOwnProperty(key)) {
result = strings[key];
}
if (result === undefined) {
if (language === DEFAULT_LANGUAGE) {
throw Error(`${key} not found for default language`);
}
const fallback = getFallback(language);
console.warn(`${key} not found for ${language}, using ${fallback}`);
return translate(translation, fallback, key);
}
return result;
}
function processByRegex(data) {
if (Array.isArray(data)) {
return data.map(processByRegex);
}
else if (typeof data === 'string') {
// non-breaking space (nbsp)
if (currentVueKey === 'cs' || currentVueKey === 'sk') {
data = data.replace(/(?<=\s)([kvszaiou])\s/gi, '$1\u00A0');
} else if (currentVueKey === 'en') {
data = data.replace(/(?<=\s)(a|an|the)\s/gi, '$1\u00A0');
}
return data;
}
else if (typeof data === 'object' && data !== null) {
const modified = {};
for (const key of Object.keys(data)) {
modified[key] = processByRegex(data[key]);
}
return modified;
}
throw Error(`Wrong type supplied to processByRegex: ${typeof data}, ${data}`);
}
async function updateRemoteConfig(translation) {
if (CREDENTIALS_FILE === undefined) {
console.log('GOOGLE_APPLICATION_CREDENTIALS not set, skipping remote config upload');
return;
}
const values = {
...stringsToRemoteConfigFormat(translation)
};
await updateRemoteConfigValues(values);
}
function stringsToRemoteConfigFormat(translation) {
var values = {};
for (const language of Object.keys(translation)) {
translation[language] = translation[language][WRAPPING_KEY];
for (const key of Object.keys(translation[language])) {
const rcKey = RC_PREFIX + key;
const string = translation[language][key];
const value = typeof string === 'object' ? JSON.stringify(string) : string;
if (values[rcKey] === undefined) {
values[rcKey] = {
defaultValue: {},
conditionalValues: {}
}
}
if (LANGUAGE_TO_RC[language] === DEFAULT_RC_LANGUAGE_VALUE) {
values[rcKey].defaultValue = { value };
} else if (LANGUAGE_TO_RC[language]) {
values[rcKey].conditionalValues[LANGUAGE_TO_RC[language]] = { value };
}
}
}
return values;
}
async function updateRemoteConfigValues(values) {
try {
const account = getServiceAccount();
const firebaseProject = account.project_id;
console.log(`Updating remote config of ${firebaseProject}`);
const credential = admin.credential.cert(account);
const token = (await credential.getAccessToken()).access_token;
const config = await fetch(`https://firebaseremoteconfig.googleapis.com/v1/projects/${firebaseProject}/remoteConfig`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`,
'Accept-Encoding': 'gzip',
}
});
if (config.status !== 200) {
console.log(`Remote config fetch failed: ${config.status}: ${config.statusText}`);
return;
}
const etag = config.headers.raw().etag[0];
const body = await config.json();
const parameters = body['parameters'] || {};
const conditions = body['conditions'] || [];
if (!isRemoteConfigDirty(body['parameters'], values)) {
console.log('Values not changed, skipping');
return;
}
for (const key of Object.keys(values)) {
const value = values[key];
if (value === '') {
console.warn(`Skipping remote config key ${key} because it's empty`);
continue;
}
let object;
if (value.hasOwnProperty('defaultValue')) {
object = value;
if (parameters[key] !== undefined) {
object = parameters[key];
}
object = value;
} else {
object = {
defaultValue: {
value
}
};
if (parameters[key] !== undefined) {
object = parameters[key];
}
object.defaultValue.value = value;
}
parameters[key] = object;
}
const data = {
parameters,
conditions
};
const dataJson = JSON.stringify(data);
const result = await fetch(`https://firebaseremoteconfig.googleapis.com/v1/projects/${firebaseProject}/remoteConfig`, {
method: 'PUT',
headers: {
'Content-Length': dataJson.length,
'Content-Type': 'application/json; UTF8',
'Authorization': `Bearer ${token}`,
'Accept-Encoding': 'gzip',
'If-Match': etag
},
body: dataJson
});
const status = result.status;
if (status === 200) {
console.log('Remote config uploaded');
} else {
console.error(`Remote config upload failed: ${status}: ${result.statusText}`);
}
} catch (e) {
console.error(e);
}
}
function isRemoteConfigDirty(data, values) {
if (data === undefined) {
return true;
}
for (const key of Object.keys(values)) {
if (data[key] === undefined || data[key]['defaultValue']['value'] !== values[key]) {
console.log(`Key ${key} is dirty`);
return true;
}
}
return false;
}
async function getRemoteConfigValues(isiOS) {
try {
const account = getServiceAccount();
const firebaseProject = account.project_id;
console.log(`Fetching remote config of ${firebaseProject}`);
const credential = admin.credential.cert(account);
const token = (await credential.getAccessToken()).access_token;
const config = await fetch(`https://firebaseremoteconfig.googleapis.com/v1/projects/${firebaseProject}/remoteConfig`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`,
'Accept-Encoding': 'gzip',
}
});
if (config.status !== 200) {
console.log(`Remote config fetch failed: ${config.status}: ${config.statusText}`);
return;
}
const body = await config.json();
const parameters = body['parameters'] || {};
const xmlContent = {};
for (const key in parameters) {
if (parameters.hasOwnProperty(key) && key.substr(0, 3) == RC_PREFIX && (!isiOS || key != 'v2_verificationServerApiKey')) {
const element = parameters[key];
xmlContent[DEFAULT_RC_LANGUAGE_VALUE] = xmlContent[DEFAULT_RC_LANGUAGE_VALUE] ? xmlContent[DEFAULT_RC_LANGUAGE_VALUE] : '';
xmlContent[DEFAULT_RC_LANGUAGE_VALUE] += getTemplateForKeyValue(key, element.defaultValue.value, isiOS);
GET_DEFAULTS_SUPPORTED.forEach(defKey => {
const condKey = LANGUAGE_TO_RC[defKey];
xmlContent[condKey] = xmlContent[condKey] ? xmlContent[condKey] : '';
if (element.hasOwnProperty('conditionalValues') && element.conditionalValues.hasOwnProperty(condKey)) {
const { value } = element.conditionalValues[condKey];
xmlContent[condKey] += getTemplateForKeyValue(key, value, isiOS);
} else {
xmlContent[condKey] += getTemplateForKeyValue(key, element.defaultValue.value, isiOS);
}
});
}
}
const directory = isiOS ? 'resios' : 'res';
const innerText = isiOS ? '.lproj' : 'xml';
const fileName = [isiOS ? 'RemoteConfig.strings' : 'remote_config_defaults.xml', 'Help.strings', 'HelpJson.strings'];
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
if (isiOS) {
xmlContent['iOS base'] = xmlContent[LANGUAGE_TO_RC['cs']];
}
for (const lang in xmlContent) {
if (xmlContent.hasOwnProperty(lang)) {
const langContent = xmlContent[lang];
const dirName = `${directory}/${
isiOS
? `${getKeyByValue(LANGUAGE_TO_RC, lang)}${innerText}`
: (
lang == DEFAULT_RC_LANGUAGE_VALUE
? innerText
: `${innerText}-${getKeyByValue(LANGUAGE_TO_RC, lang)}`
)
}`;
if (!fs.existsSync(dirName)) {
fs.mkdirSync(dirName);
}
if (isiOS) {
fs.writeFileSync(`${dirName}/${fileName[1]}`, processXmlByRegex(getTemplateWrapper(langContent, isiOS), isiOS, 1));
fs.writeFileSync(`${dirName}/${fileName[2]}`, processXmlByRegex(getTemplateWrapper(langContent, isiOS), isiOS, 2));
}
fs.writeFileSync(`${dirName}/${fileName[0]}`, processXmlByRegex(getTemplateWrapper(langContent, isiOS), isiOS, 0));
}
}
} catch (e) {
console.error(e);
}
}
async function getRemoteConfigValuesAndroid() {
return await getRemoteConfigValues(false);
}
async function getRemoteConfigValuesIos() {
return await getRemoteConfigValues(true);
}
function getKeyByValue(obj, value) {
return Object.keys(obj).find(key => obj[key] === value);
}
function getTemplateForKeyValue(key, value, isiOS) {
return isiOS
? `"${key.replace(RC_PREFIX, '')}" = "${value.replace(/(["\\])/g, '\\$1')}";
`
: `
<entry>
<key>${key}</key>
<value>${value}</value>
</entry>`;
}
function processXmlByRegex(value, isiOS, helpMode) {
return (
isiOS ? (
helpMode == 0
? value.replace(/"help((Markdown)|(Json))".*\n/g, '')
: (
helpMode == 1
? value.match(/"helpMarkdown".*\n/)[0]
: value.match(/"helpJson".*\n/)[0]
)
) : value.replace(/&/g, '&')
);
}
function getTemplateWrapper(value, isiOS) {
return isiOS
? `${value}`
: `<?xml version="1.0" encoding="utf-8"?>
<defaultsMap>${value}
</defaultsMap>
`;
}
exports.up = uploadStrings;
exports.uploadF = forceUploadStrings;
exports.get = getRemoteConfigValuesAndroid;
exports.getios = getRemoteConfigValuesIos;
exports.default = processAndUploadDownloadedStrings;