From bba991384c3334b55e54b16a5d1769ca352f712b Mon Sep 17 00:00:00 2001 From: Mama Naomi Date: Wed, 24 Jan 2024 00:38:17 -0800 Subject: [PATCH] feat: confirm id values are correct in i18n sync (#53331) Co-authored-by: Oliver Eyton-Williams --- tools/scripts/sync-i18n.ts | 41 +++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/tools/scripts/sync-i18n.ts b/tools/scripts/sync-i18n.ts index 1adac1c9983..649e29bbb92 100644 --- a/tools/scripts/sync-i18n.ts +++ b/tools/scripts/sync-i18n.ts @@ -1,5 +1,5 @@ import { exec } from 'child_process'; -import { readdir, stat } from 'fs/promises'; +import { readFile, readdir, stat, writeFile } from 'fs/promises'; import { join, sep } from 'path'; import { promisify } from 'util'; @@ -27,19 +27,32 @@ const syncChallenges = async () => { // these will be paths const english = await loadDirectory(join(basePath, 'english')); for (const path of english) { - for (const lang of filtered) { - const targetPath = path.replace('english', lang); - // we swallow the error here to detect if the file doesn't exist - const status = await stat(targetPath).catch(() => null); - if (!status) { - const targetDir = targetPath.split(sep); - targetDir.pop(); - console.log(`Syncing ${path.split('/english/')[1]}`); - await asyncExec( - `mkdir -p ${targetDir.join(sep)} && cp ${path} ${targetPath}` - ); - } - } + await Promise.all( + filtered.map(async lang => { + const targetPath = path.replace('english', lang); + // we swallow the error here to detect if the file doesn't exist + const status = await stat(targetPath).catch(() => null); + if (!status) { + const targetDir = targetPath.split(sep); + targetDir.pop(); + console.log(`Syncing ${path.split('/english/')[1]}`); + await asyncExec( + `mkdir -p ${targetDir.join(sep)} && cp ${path} ${targetPath}` + ); + } + const englishContent = await readFile(path, 'utf-8'); + const langContent = await readFile(targetPath, 'utf-8'); + const engLines = englishContent.split('\n'); + const engId = engLines.find(l => l.startsWith('id')); + const langLines = langContent.split('\n'); + const langId = langLines.find(l => l.startsWith('id')); + if (engId && langId && engId !== langId) { + langLines.splice(langLines.indexOf(langId), 1, engId); + console.log(`Updating ID for ${targetPath}`); + await writeFile(targetPath, langLines.join('\n'), 'utf-8'); + } + }) + ); } for (const lang of filtered) { const langPath = join(process.cwd(), 'curriculum', 'challenges', lang);