-
-
Notifications
You must be signed in to change notification settings - Fork 349
/
csv-sync.js
145 lines (134 loc) · 3.65 KB
/
csv-sync.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
const fs = require('fs');
const args = process.argv.slice(2);
const { PATH_CSV } = require('./env');
const referenceFile = 'en.csv';
const files = fs
.readdirSync(PATH_CSV)
.filter(f => f !== referenceFile && f.endsWith('csv'));
const stringify = require('csv-stringify');
const parse = require('csv-parse/lib/sync');
function aggregateRecords(records) {
const common = records.filter(r => {
return r.Key.split(',')[0].indexOf(':') === -1;
});
const others = records.filter(r => {
return r.Key.split(',')[0].indexOf(':') !== -1;
});
return common.sort(sortKeys).concat(others.sort(sortKeys));
}
function sortKeys(a, b) {
if (a.Key < b.Key) {
return -1;
}
if (a.Key > b.Key) {
return 1;
}
return 0;
}
function readReferenceFile() {
return readTranslation(referenceFile);
}
function readTranslation(filename) {
const source = fs.readFileSync(`${PATH_CSV}${filename}`, 'utf8');
const records = parse(source, {
columns: true,
skip_empty_lines: true
});
records.sort(sortKeys);
return records;
}
function updateMissingTranslation(
referenceTranslation,
dialectTranslationArray
) {
const sK = dialectTranslationArray.find(
s => referenceTranslation.Key === s.Key
);
if (!sK) {
dialectTranslationArray.push({
Key: referenceTranslation.Key,
Translation: ''
});
}
}
function deleteDeprecatedTranslations(dialectArray, referenceArray) {
const referenceKeys = referenceArray.map(x => x.Key);
dialectArray = dialectArray.filter(x => referenceKeys.includes(x.Key));
return dialectArray;
}
function syncTranslationFiles(referenceTranslations) {
files.forEach(f => {
let records = readTranslation(f)
referenceTranslations.forEach(t => {
updateMissingTranslation(t, records);
});
records = deleteDeprecatedTranslations(records, referenceTranslations);
stringify(
[{ Key: 'Key', Translation: 'Translation' }].concat(
aggregateRecords(records)
),
(err, csvOutput) => {
fs.writeFileSync(`${PATH_CSV}${f}`, csvOutput);
}
);
});
// Reordering the reference Translation
stringify(
[{ Key: 'Key', Translation: 'Translation' }].concat(
aggregateRecords(referenceTranslations)
),
(err, csvOutput) => {
fs.writeFileSync(`${PATH_CSV}en.csv`, csvOutput);
}
);
}
function checkTranslationFiles(referenceTranslations) {
let errors = {};
files.forEach(f => {
const dialectTranslations = readTranslation(f);
const dialectKeys = dialectTranslations.map(s => s.Key);
const missingTranslations = referenceTranslations.filter(
t => !dialectKeys.includes(t.Key)
);
if (missingTranslations.length > 0) {
errors[f] = missingTranslations.map(s => s.Key);
}
});
if (Object.keys(errors).length > 0) {
console.log('Missing translations found');
console.log('');
Object.keys(errors).forEach(e => {
console.log(`Missing Keys in ${e}`);
console.log('');
console.log(errors[e].join('\n'));
console.log('');
});
console.log('Translation report:');
Object.keys(errors).forEach(e => {
console.log(`${e} : ${errors[e].length} entries missing`);
});
return false;
} else {
console.log('Everything is synchronized');
return true;
}
}
function main() {
const reference = readReferenceFile();
switch (args[0]) {
case 'sync':
syncTranslationFiles(reference);
break;
case 'chk':
if (checkTranslationFiles(reference)) {
process.exit(0);
} else {
process.exit(1);
}
break;
default:
console.log('Invalid argument. sync / ci are only available');
process.exit(1);
}
}
main();