29 lines
1.4 KiB
JavaScript
29 lines
1.4 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
import { mkdirSync, 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', 'exercise-name-sources', 'pt-BR.json')
|
|
const outputPath = join(root, 'frontend', 'src', 'exercise-names', 'pt-BR.js')
|
|
const exercisesPath = join(root, 'frontend', 'src', 'lib', 'exercises-data.js')
|
|
|
|
const source = JSON.parse(readFileSync(sourcePath, 'utf8'))
|
|
const { EXDB } = await import(pathToFileURL(exercisesPath))
|
|
const known = new Set(EXDB.map(exercise => exercise.id))
|
|
|
|
for (const id of Object.keys(source)) {
|
|
if (!known.has(id)) throw new Error(`Unknown exercise ID: ${id}`)
|
|
if (typeof source[id] !== 'string' || !source[id].trim()) throw new Error(`${id}: name must be a non-empty string`)
|
|
}
|
|
if (Object.keys(source).length !== EXDB.length) {
|
|
throw new Error(`Incomplete pt-BR exercise names: ${Object.keys(source).length}/${EXDB.length}`)
|
|
}
|
|
|
|
const ordered = {}
|
|
for (const exercise of EXDB) ordered[exercise.id] = source[exercise.id].trim()
|
|
mkdirSync(dirname(outputPath), { recursive: true })
|
|
writeFileSync(outputPath, '// generated by scripts/build-pt-br-exercise-names.mjs — edit scripts/exercise-name-sources/pt-BR.json\nexport default ' + JSON.stringify(ordered) + '\n')
|
|
console.log(`Generated ${EXDB.length}/${EXDB.length} pt-BR exercise names.`)
|