AFFiNE/packages/frontend/i18n/build.mjs

60 lines
1.4 KiB
JavaScript
Raw Normal View History

import { readdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join, parse } from 'node:path';
import { fileURLToPath } from 'node:url';
2023-06-27 16:05:24 +03:00
import { runCli } from '@magic-works/i18n-codegen';
const isDev = process.argv.includes('--dev');
const pkgRoot = fileURLToPath(new URL('./', import.meta.url));
function calcCompletenesses() {
const resourcesDir = join(pkgRoot, 'src', 'resources');
const langs = readdirSync(resourcesDir)
.filter(file => file.endsWith('.json'))
.reduce((langs, file) => {
const filePath = `${resourcesDir}/${file}`;
const fileContent = JSON.parse(readFileSync(filePath, 'utf-8'));
langs[parse(file).name] = fileContent;
return langs;
}, {});
const base = Object.keys(langs.en).length;
const completenesses = {};
for (const key in langs) {
const [langPart, variantPart] = key.split('-');
const completeness = Object.keys(
variantPart ? { ...langs[langPart], ...langs[key] } : langs[key]
).length;
completenesses[key] = Math.min(
Math.ceil(/* avoid 0% */ (completeness / base) * 100),
100
);
}
writeFileSync(
join(pkgRoot, 'src', 'i18n-completenesses.json'),
JSON.stringify(completenesses, null, 2)
);
}
runCli(
2023-06-27 16:05:24 +03:00
{
config: fileURLToPath(new URL('./.i18n-codegen.json', import.meta.url)),
watch: isDev,
2023-06-27 16:05:24 +03:00
},
error => {
console.error(error);
if (!isDev) {
process.exit(1);
}
2023-06-27 16:05:24 +03:00
}
);
calcCompletenesses();