2024-05-10 15:56:25 -04:00
|
|
|
import fs from 'fs';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import { processLanguageModule, processMissingKey } from './process';
|
2024-10-24 10:48:57 -04:00
|
|
|
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
2024-05-10 15:56:25 -04:00
|
|
|
|
|
|
|
|
export default async function main(baseFilePath: string, compareFilePath: string) {
|
|
|
|
|
const prompt = await processLanguageModule(path.basename(compareFilePath), compareFilePath);
|
|
|
|
|
|
|
|
|
|
if (prompt === undefined) {
|
|
|
|
|
console.error(`Prompt not found for module: ${path.basename(compareFilePath)}`);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const baseModule = await import(baseFilePath);
|
|
|
|
|
const baseKeys = Object.keys(baseModule.default);
|
|
|
|
|
|
|
|
|
|
const compareModule = await import(compareFilePath);
|
|
|
|
|
const compareKeys = Object.keys(compareModule.default);
|
|
|
|
|
|
2024-05-12 16:24:13 -04:00
|
|
|
const missingKeys = baseKeys.filter((key) => !compareKeys.includes(key));
|
2024-10-24 10:48:57 -04:00
|
|
|
const promises: Array<Promise<void>> = [];
|
2024-05-10 15:56:25 -04:00
|
|
|
if (missingKeys.length > 0) {
|
|
|
|
|
const keyTranslations = {};
|
|
|
|
|
for (const key of missingKeys) {
|
2024-05-12 16:24:13 -04:00
|
|
|
const baselineTranslation = baseModule.default[key] || 'No baseline translation available';
|
2024-10-24 10:48:57 -04:00
|
|
|
const handleMissingKey = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await processMissingKey({
|
|
|
|
|
key,
|
|
|
|
|
baselineTranslation,
|
|
|
|
|
translationPrompt: prompt,
|
|
|
|
|
moduleName: path.basename(compareFilePath),
|
|
|
|
|
});
|
|
|
|
|
keyTranslations[key] = result;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(`Error processing key: ${key}`, e);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
promises.push(handleMissingKey());
|
|
|
|
|
await sleep(700);
|
2024-05-10 15:56:25 -04:00
|
|
|
}
|
|
|
|
|
|
2024-10-24 10:48:57 -04:00
|
|
|
await Promise.all(promises);
|
2024-05-10 15:56:25 -04:00
|
|
|
const outputDir = path.dirname(compareFilePath);
|
2024-05-12 16:24:13 -04:00
|
|
|
const outputFileName = `${path.basename(
|
|
|
|
|
compareFilePath,
|
|
|
|
|
path.extname(compareFilePath),
|
|
|
|
|
)}_missing_keys.json`;
|
2024-05-10 15:56:25 -04:00
|
|
|
const outputFilePath = path.join(outputDir, outputFileName);
|
|
|
|
|
fs.writeFileSync(outputFilePath, JSON.stringify(keyTranslations, null, 2));
|
|
|
|
|
}
|
2024-05-12 16:24:13 -04:00
|
|
|
}
|