@kununu/phraseapp-cli 3.1.0-beta.3 → 4.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/check-translations.js +115 -0
- package/package.json +6 -2
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
//import { readFileSync, writeFileSync, unlinkSync, existsSync } from 'fs';
|
|
2
|
+
//import { sync } from 'glob';
|
|
3
|
+
//import { parse } from '@babel/parser';
|
|
4
|
+
//import traverse from '@babel/traverse';
|
|
5
|
+
|
|
6
|
+
const { readFileSync, writeFileSync, unlinkSync, existsSync } = require('fs');
|
|
7
|
+
const {sync} = require('glob');
|
|
8
|
+
const {parse} = require('@babel/parser');
|
|
9
|
+
const traverse = require('@babel/traverse');
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
// const TRANSLATIONS_FILE = `${process.cwd()}/translations/de_AT.json`;
|
|
13
|
+
const DYNAMIC_KEYS_FILE = `${process.cwd()}/dynamic_keys.json`;
|
|
14
|
+
const UNUSED_KEYS_FILE = 'not_used_keys.json';
|
|
15
|
+
const MISSING_KEYS_FILE = 'missing_keys.json';
|
|
16
|
+
const POSSIBLE_DYNAMIC_KEYS_FILE = 'possible_dynamic_keys.json';
|
|
17
|
+
|
|
18
|
+
function extractTranslationKeys() {
|
|
19
|
+
const files = sync(`${process.env.SOURCE_DIR}/**/**/*.{js,jsx,ts,tsx}`);
|
|
20
|
+
const keys = new Set();
|
|
21
|
+
const possibleDynamicKeys = {};
|
|
22
|
+
|
|
23
|
+
files.forEach(file => {
|
|
24
|
+
const content = readFileSync(file, 'utf8');
|
|
25
|
+
try {
|
|
26
|
+
const ast = parse(content, { sourceType: 'module', plugins: ['jsx', 'typescript'] });
|
|
27
|
+
traverse.default(ast, {
|
|
28
|
+
JSXOpeningElement({ node }) {
|
|
29
|
+
if (node.name.name === 'FormattedMessage') {
|
|
30
|
+
const idAttr = node.attributes.find(attr => attr.name.name === 'id');
|
|
31
|
+
if (idAttr && idAttr.value && idAttr.value.type === 'StringLiteral') {
|
|
32
|
+
keys.add(idAttr.value.value);
|
|
33
|
+
} else {
|
|
34
|
+
possibleDynamicKeys[file] = possibleDynamicKeys[file] || [];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
CallExpression({ node }) {
|
|
39
|
+
if (node.callee && node.callee.property && ['formatMessage'].includes(node.callee.property.name)) {
|
|
40
|
+
const firstArg = node.arguments[0];
|
|
41
|
+
if (firstArg && firstArg.type === 'ObjectExpression') {
|
|
42
|
+
const idProperty = firstArg.properties.find(prop => prop.key.name === 'id');
|
|
43
|
+
if (idProperty && idProperty.value.type === 'StringLiteral') {
|
|
44
|
+
keys.add(idProperty.value.value);
|
|
45
|
+
} else {
|
|
46
|
+
possibleDynamicKeys[file] = possibleDynamicKeys[file] || [];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error(`Error processing ${file}:`, error);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (Object.keys(possibleDynamicKeys).length > 0) {
|
|
58
|
+
console.log(`${Object.keys(possibleDynamicKeys).length} files contain possible dynamic keys.`);
|
|
59
|
+
writeFileSync(POSSIBLE_DYNAMIC_KEYS_FILE, JSON.stringify(possibleDynamicKeys, null, 2));
|
|
60
|
+
} else if (existsSync(POSSIBLE_DYNAMIC_KEYS_FILE)) {
|
|
61
|
+
unlinkSync(POSSIBLE_DYNAMIC_KEYS_FILE);
|
|
62
|
+
console.log(`${POSSIBLE_DYNAMIC_KEYS_FILE} was deleted as there are no possible dynamic keys.`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return keys;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function compareKeys() {
|
|
69
|
+
const configPath = `${process.cwd()}/.phraseapp.json`;
|
|
70
|
+
const appKeys = extractTranslationKeys();
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
const phrase = JSON.parse(fs.readFileSync(configPath));
|
|
74
|
+
const translations = JSON.parse(readFileSync(`${phrase.path}/de_AT.json`, 'utf8'));
|
|
75
|
+
const dynamicKeys = JSON.parse(readFileSync(phrase.dynamicKeys, 'utf8'));
|
|
76
|
+
|
|
77
|
+
const allValidKeys = new Set(Object.keys(translations));
|
|
78
|
+
|
|
79
|
+
let missingKeys = [...appKeys].filter(key => !allValidKeys.has(key));
|
|
80
|
+
missingKeys = missingKeys.filter(key => key != null && key !== undefined);
|
|
81
|
+
|
|
82
|
+
// Check if there are keys in dynamicKeys that are missing from allValidKeys.
|
|
83
|
+
dynamicKeys.forEach(key => {
|
|
84
|
+
if (!allValidKeys.has(key)) {
|
|
85
|
+
missingKeys.push(key);
|
|
86
|
+
} else {
|
|
87
|
+
allValidKeys.delete(key);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
//
|
|
92
|
+
const referenceKeys = new Set([...appKeys, ...dynamicKeys]);
|
|
93
|
+
const unusedKeys = [...allValidKeys].filter(key => !referenceKeys.has(key));
|
|
94
|
+
|
|
95
|
+
if (missingKeys.length > 0) {
|
|
96
|
+
writeFileSync(MISSING_KEYS_FILE, JSON.stringify(missingKeys, null, 2));
|
|
97
|
+
console.log(`${missingKeys.length} missing keys were saved in ${MISSING_KEYS_FILE}`);
|
|
98
|
+
} else if (existsSync(MISSING_KEYS_FILE)) {
|
|
99
|
+
unlinkSync(MISSING_KEYS_FILE);
|
|
100
|
+
console.log(`${MISSING_KEYS_FILE} was deleted as there are no missing keys.`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (unusedKeys.length > 0) {
|
|
104
|
+
writeFileSync(UNUSED_KEYS_FILE, JSON.stringify(unusedKeys, null, 2));
|
|
105
|
+
console.log(`${unusedKeys.length} unused keys were saved in ${UNUSED_KEYS_FILE}`);
|
|
106
|
+
} else if (existsSync(UNUSED_KEYS_FILE)) {
|
|
107
|
+
unlinkSync(UNUSED_KEYS_FILE);
|
|
108
|
+
console.log(`${UNUSED_KEYS_FILE} was deleted as there are no missing keys.`);
|
|
109
|
+
}
|
|
110
|
+
} catch (error) {
|
|
111
|
+
console.error(`Error processing ${file}:`, error);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
compareKeys();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kununu/phraseapp-cli",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-beta.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": "kununu",
|
|
@@ -10,7 +10,11 @@
|
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"colors": "1.4.0",
|
|
13
|
-
"dotenv": "16.4.5"
|
|
13
|
+
"dotenv": "16.4.5",
|
|
14
|
+
"@babel/parser": "^7.26.9",
|
|
15
|
+
"@babel/traverse": "^7.26.9",
|
|
16
|
+
"fs": "^0.0.1-security",
|
|
17
|
+
"glob": "^11.0.1"
|
|
14
18
|
},
|
|
15
19
|
"devDependencies": {
|
|
16
20
|
"@kununu/eslint-config": "5.0.1"
|