perf: half clean code
gitea/salix-front/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Javier Segarra 2024-12-27 14:34:20 +01:00
parent 023492652f
commit 7c183964c9
1 changed files with 22 additions and 16 deletions

View File

@ -4,25 +4,34 @@ import path from 'path';
import { glob } from 'glob';
import yaml from 'js-yaml';
import assert from 'assert';
const YML = '.yml';
const UTF8 = 'utf8';
const LOCALES_FILES = [`src/i18n/locale/**${YML}`, `src/pages/**/locale/*${YML}`];
const VUE_FILES = ['src/pages/*.vue', 'src/pages/**/*.vue', 'src/components/**/*.vue'];
const CURRENT_LOCALES = ['es'];
let locales = {};
let i18n = {};
async function init() {
const files = await glob(['src/i18n/locale/**.yml', 'src/pages/**/locale/*.yml']);
async function init() {
const files = await glob(LOCALES_FILES);
locales = files.reduce((acc, file) => {
const locale = path.basename(file, '.yml');
acc[locale] = { ...acc[locale], ...yaml.load(fs.readFileSync(file, 'utf8')) };
const locale = path.basename(file, YML);
acc[locale] = { ...acc[locale], ...yaml.load(fs.readFileSync(file, UTF8)) };
return acc;
}, {});
}
function validateKeys(keys, locale) {
const missingKeys = validateLocale(keys, locales[locale]);
const missingKeys2 = validateLocale(missingKeys, i18n[locale]);
return missingKeys2;
}
function validateLocale(keys, translations) {
const missingKeys = [];
let missingKeys = [];
if (translations === undefined) return missingKeys;
keys.forEach((key) => {
const parts = key.split('.');
@ -44,17 +53,13 @@ function validateLocale(keys, translations) {
describe('🔍 Translation Keys Validation', async () => {
await init();
const vueFiles = await glob([
'src/pages/*.vue',
'src/pages/**/*.vue',
'src/components/**/*.vue',
]);
const vueFiles = await glob(VUE_FILES);
const regex = /="\$t\(['"`]([\w.]+)['"`]\)|="t\(['"`]([\w.]+)['"`]\)/g;
vueFiles.forEach(async (file) => {
const keys = new Set();
const content = fs.readFileSync(file, 'utf8');
const content = fs.readFileSync(file, UTF8);
let match;
while ((match = regex.exec(content)) !== null) {
keys.add(match[1] || match[2]);
@ -62,11 +67,13 @@ describe('🔍 Translation Keys Validation', async () => {
const parts = file.split(path.sep);
const cardIndex = parts.indexOf('Card');
let previousElement = '';
if (cardIndex > -1) {
previousElement = parts[cardIndex - 1];
previousElement =
previousElement.charAt(0).toLowerCase() + previousElement.slice(1);
}
const i18nMatch = content.match(/<i18n[^>]*>([\s\S]*?)<\/i18n>/);
if (i18nMatch) {
@ -82,17 +89,14 @@ describe('🔍 Translation Keys Validation', async () => {
});
}
i18n = i18nContent;
// Object.entries(i18nContent).forEach(([local, value]) => {
// Object.entries(value).forEach(([k, v]) => (i18n[local] = { [k]: v }));
// });
} catch (err) {
console.warn(`⚠️ Error parsing <i18n> block in ${file}:`, err.message);
}
}
['es'].forEach((locale) => {
CURRENT_LOCALES.forEach((locale) => {
let missingKeys = validateKeys(keys, locale);
if (missingKeys.length > 0) {
console.log(`Elemento anterior a 'Card': ${previousElement}`);
const updatedKeys = new Set();
missingKeys.forEach((key) => {
if (!key.startsWith(`${previousElement}.`))
@ -100,10 +104,12 @@ describe('🔍 Translation Keys Validation', async () => {
});
missingKeys = validateKeys(updatedKeys, locale);
}
assert(
missingKeys.length === 0,
`Missing keys in ${locale}.${file}:\n${missingKeys.join('\n')}`
);
it(`should have all translation keys in ${locale}.${file}`, () => {
expect(missingKeys, `Missing keys in ${file}`).toHaveLength(0);
});