| 1 | /* |
| 2 | make-flags.mjs: the language screen's flags, from flag-icons (MIT, github.com/lipis/flag-icons). |
| 3 |
|
| 4 | node tools/make-flags.mjs "<flag-icons folder>" |
| 5 |
|
| 6 | data/flags/<code>.png 120x90 (4:3), for the flags src/lang.c names |
| 7 | data/licenses/flag-icons.txt |
| 8 |
|
| 9 | Uses sharp from generator/node_modules (cd generator && npm install first). |
| 10 | */ |
| 11 | import fs from 'node:fs'; |
| 12 | import path from 'node:path'; |
| 13 | import { createRequire } from 'node:module'; |
| 14 | import { fileURLToPath } from 'node:url'; |
| 15 |
|
| 16 | const ROOT = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); |
| 17 | const sharp = createRequire(path.join(ROOT, 'generator/'))('sharp'); |
| 18 | const src = process.argv[2]; |
| 19 | if (!src) { console.error('usage: node tools/make-flags.mjs "<flag-icons folder>"'); process.exit(1); } |
| 20 |
|
| 21 | // lang.c's flags[], in its order |
| 22 | const lang = fs.readFileSync(path.join(ROOT, 'src/lang.c'), 'utf8'); |
| 23 | const codes = [...lang.match(/flags\[LANG_COUNT\] = \{([^}]*)\}/)[1].matchAll(/"([a-z]+)"/g)].map(m => m[1]); |
| 24 | fs.mkdirSync(path.join(ROOT, 'data/flags'), { recursive: true }); |
| 25 | for (const c of codes) { |
| 26 | const svg = path.join(src, 'flags/4x3', `${c}.svg`); |
| 27 | await sharp(svg, { density: 300 }).resize(120, 90, { fit: 'fill' }).png().toFile(path.join(ROOT, 'data/flags', `${c}.png`)); |
| 28 | } |
| 29 | fs.copyFileSync(path.join(src, 'LICENSE'), path.join(ROOT, 'data/licenses/flag-icons.txt')); |
| 30 | console.log(`data/flags: ${codes.join(' ')}`); |