Repository navigation
Expand file tree
/
Copy pathcheck-i18n.js
More file actions
113 lines (84 loc) · 2.63 KB
/
Copy pathcheck-i18n.js
File metadata and controls
113 lines (84 loc) · 2.63 KB
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
const fs = require('fs');
const path = require('path');
const fg = require('fast-glob');
const regexVar = /__[a-zA-Z_]+__/g;
const validateKeys = (json, usedKeys) =>
usedKeys
.filter(({ key }) => typeof json[key] !== 'undefined')
.reduce((prev, cur) => {
const { key, replaces } = cur;
const miss = replaces.filter((replace) => json[key] && json[key].indexOf(replace) === -1);
if (miss.length > 0) {
prev.push({ key, miss });
}
return prev;
}, []);
const removeMissingKeys = (i18nFiles, usedKeys) => {
i18nFiles.forEach((file) => {
const json = JSON.parse(fs.readFileSync(file, 'utf8'));
if (Object.keys(json).length === 0) {
return;
}
validateKeys(json, usedKeys)
.forEach(({ key }) => {
json[key] = null;
});
fs.writeFileSync(file, JSON.stringify(json, null, 2));
});
};
const checkUniqueKeys = (content, json, filename) => {
const matchKeys = content.matchAll(/^\s+"([^"]+)"/mg);
const allKeys = [...matchKeys];
if (allKeys.length !== Object.keys(json).length) {
throw new Error(`Duplicated keys found on file ${ filename }`);
}
};
const validate = (i18nFiles, usedKeys) => {
const totalErrors = i18nFiles
.reduce((errors, file) => {
const content = fs.readFileSync(file, 'utf8');
const json = JSON.parse(content);
checkUniqueKeys(content, json, file);
// console.log('json, usedKeys2', json, usedKeys);
const result = validateKeys(json, usedKeys);
if (result.length === 0) {
return errors;
}
console.log('\n## File', file, `(${ result.length } errors)`);
result.forEach(({ key, miss }) => {
console.log('\n- Key:', key, '\n Missing variables:', miss.join(', '));
});
return errors + result.length;
}, 0);
if (totalErrors > 0) {
throw new Error(`\n${ totalErrors } errors found`);
}
};
const checkFiles = async (sourcePath, sourceFile, fix = false) => {
const content = fs.readFileSync(path.join(sourcePath, sourceFile), 'utf8');
const sourceContent = JSON.parse(content);
checkUniqueKeys(content, sourceContent, sourceFile);
const usedKeys = Object.entries(sourceContent)
.map(([key, value]) => {
const replaces = value.match(regexVar);
return {
key,
replaces,
};
});
const keysWithInterpolation = usedKeys
.filter(({ replaces }) => !!replaces);
const i18nFiles = await fg([`${ sourcePath }/**/*.i18n.json`]);
if (fix) {
return removeMissingKeys(i18nFiles, keysWithInterpolation);
}
validate(i18nFiles, keysWithInterpolation);
};
(async () => {
try {
await checkFiles('./packages/rocketchat-i18n/i18n', 'en.i18n.json', process.argv[2] === '--fix');
} catch (e) {
console.error(e);
process.exit(1);
}
})();