-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
82 lines (75 loc) · 2.61 KB
/
utils.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
const fs = require('fs');
const path = require('path');
const clearJSON = (value) => {
if (typeof value === 'object') {
const object = {};
for (const key in value) {
object[key] = clearJSON(value[key]);
}
return object;
}
return '';
}
const compareAndWriteFiles = (paths) => {
for (const [ index, fp ] of paths.entries()) {
let result = require(fp);
const filesToCompare = JSON.parse(JSON.stringify(paths.filter((p, i) => i !== index)));
for (const filePathToCompare of filesToCompare) {
const fileToCompare = require(filePathToCompare);
result = replaceMissingKeys(result, fileToCompare);
}
const q = JSON.stringify(result, null, 2);
// const formatted = JSON.stringify(q, null, 2).replaceAll('"",', '"", // Add Translation').replaceAll('""\n', '"" // Add Translation\n');
fs.writeFileSync(fp, q);
}
}
const getPathFromObject = (object) => {
return object.path + path.sep + object.name;
}
const getSourceFilePath = (exclude, fileName) => {
const locales = Object.keys(translations).filter(key => key !== exclude);
for (const locale of locales) {
if (translations[locale].includes(fileName)) {
return `${dirPath}${path.sep}${locale}${path.sep}${fileName}`;
}
}
return null;
};
const logger = {
success(...message) {
console.log('\x1b[32m%s\x1b[0m', message.filter(string => typeof string === 'string').join('')); // success
},
error(...message) {
console.log('\x1b[31m%s\x1b[0m', message.filter(string => typeof string === 'string').join('')); // error
},
warning(...message) {
console.log('\x1b[33m%s\x1b[0m', message.filter(string => typeof string === 'string').join('')); // warning
},
info(...message) {
console.log('\x1b[36m%s\x1b[0m', message.filter(string => typeof string === 'string').join('')); // info
},
}
const replaceMissingKeys = (a, b) => {
const newObject = JSON.parse(JSON.stringify(a));
if (typeof b === 'object') {
for (const key in b) {
if (!newObject.hasOwnProperty(key)) {
// TODO: clearJSON here maybe
newObject[key] = JSON.parse(JSON.stringify(b[key]));
} else {
if (typeof b[key] === 'object') {
newObject[key] = replaceMissingKeys(newObject[key], b[key]);
}
}
}
}
return newObject;
};
module.exports = {
clearJSON,
compareAndWriteFiles,
logger,
getSourceFilePath,
getPathFromObject,
replaceMissingKeys
}