#!/usr/bin/env node import { readFileSync, writeFileSync } from 'node:fs' import { dirname, join } from 'node:path' import { fileURLToPath, pathToFileURL } from 'node:url' const root = join(dirname(fileURLToPath(import.meta.url)), '..') const sourcePath = join(root, 'scripts', 'instruction-sources', 'pt-BR.json') const outputPath = join(root, 'frontend', 'src', 'instr', 'pt-BR.js') const exercisesPath = join(root, 'frontend', 'src', 'lib', 'exercises-data.js') const translations = JSON.parse(readFileSync(sourcePath, 'utf8')) const { EXDB } = await import(pathToFileURL(exercisesPath)) const exercises = new Map(EXDB.map(exercise => [exercise.id, exercise])) for (const [id, steps] of Object.entries(translations)) { const exercise = exercises.get(id) if (!exercise) throw new Error(`Unknown exercise ID: ${id}`) if (!Array.isArray(steps) || steps.length === 0) throw new Error(`${id}: translation must contain steps`) if (steps.length !== exercise.st.length) { throw new Error(`${id}: expected ${exercise.st.length} steps, received ${steps.length}`) } steps.forEach((step, index) => { if (typeof step !== 'string' || !step.trim()) throw new Error(`${id}: empty step ${index + 1}`) if (step === exercise.st[index]) throw new Error(`${id}: step ${index + 1} is still English`) }) } const ordered = Object.fromEntries(EXDB.filter(exercise => translations[exercise.id]).map(exercise => [exercise.id, translations[exercise.id]])) writeFileSync(outputPath, '// generated by scripts/build-pt-br-instructions.mjs — edit scripts/instruction-sources/pt-BR.json\nexport default ' + JSON.stringify(ordered) + '\n') const stepCount = Object.values(ordered).reduce((total, steps) => total + steps.length, 0) console.log(`${outputPath}: ${Object.keys(ordered).length}/${EXDB.length} exercises, ${stepCount} steps`)