()
| 44 | }; |
| 45 | |
| 46 | async function main() { |
| 47 | const entries = await fs.promises.readdir(dataDir, { withFileTypes: true }); |
| 48 | |
| 49 | const sortedEntries = entries.sort((a, b) => { |
| 50 | const aIdx = PRIORITY_LANGS.indexOf(a.name.toLowerCase()); |
| 51 | const bIdx = PRIORITY_LANGS.indexOf(b.name.toLowerCase()); |
| 52 | if (aIdx !== -1 && bIdx !== -1) return aIdx - bIdx; |
| 53 | if (aIdx !== -1) return -1; |
| 54 | if (bIdx !== -1) return 1; |
| 55 | return a.name.localeCompare(b.name); |
| 56 | }); |
| 57 | |
| 58 | for (const entry of sortedEntries) { |
| 59 | if (!entry.isDirectory()) continue; |
| 60 | const langCode = entry.name; |
| 61 | const langName = LANG_NAMES[langCode] || langCode.charAt(0).toUpperCase() + langCode.slice(1); |
| 62 | const ext = LANG_EXT[langCode] || "txt"; |
| 63 | |
| 64 | try { |
| 65 | const langDir = path.join(dataDir, langCode); |
| 66 | const files = await fs.promises.readdir(langDir); |
| 67 | |
| 68 | const chapters = files |
| 69 | .filter((f) => !f.startsWith(".")) |
| 70 | .map((f) => path.basename(f, path.extname(f))) |
| 71 | .sort((a, b) => { |
| 72 | const numA = parseInt(a.split("_")[0], 10); |
| 73 | const numB = parseInt(b.split("_")[0], 10); |
| 74 | if (!isNaN(numA) && !isNaN(numB)) return numA - numB; |
| 75 | return a.localeCompare(b); |
| 76 | }); |
| 77 | |
| 78 | if (chapters.length > 0) { |
| 79 | manifest[langCode] = { code: langCode, name: langName, ext, chapters }; |
| 80 | |
| 81 | for (const chap of chapters) { |
| 82 | const filePath = path.join(langDir, `${chap}.${ext}`); |
| 83 | const content = await fs.promises.readFile(filePath, "utf-8"); |
| 84 | codeFiles[`${langCode}/${chap}`] = content; |
| 85 | } |
| 86 | } |
| 87 | } catch (err) { |
| 88 | console.error(`Failed to read ${langCode}`, err); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | const output = { manifest, codeFiles }; |
| 93 | await fs.promises.writeFile(outputPath, JSON.stringify(output)); |
| 94 | console.log("Generated lib/code/data.json"); |
| 95 | } |
| 96 | |
| 97 | main().catch(console.error); |
no test coverage detected