word-waltz / files / scripts / generate-guess-list.js
generate-guess-list.js
| 1 | #!/usr/bin/env node |
| 2 | /** |
| 3 | * scripts/generate-guess-list.js |
| 4 | * |
| 5 | * Generates src/utils/validGuesses.ts from the `word-list` npm package. |
| 6 | * Filters to lowercase 5-letter alphabetic words only. |
| 7 | * |
| 8 | * Run with: node scripts/generate-guess-list.js |
| 9 | * Automatically run before `dev` and `build` via package.json scripts. |
| 10 | */ |
| 11 | |
| 12 | import wordListPath from 'word-list'; |
| 13 | import { readFileSync, writeFileSync } from 'fs'; |
| 14 | import { resolve, dirname } from 'path'; |
| 15 | import { fileURLToPath } from 'url'; |
| 16 | |
| 17 | const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 18 | const OUT = resolve(__dirname, '../src/utils/validGuesses.ts'); |
| 19 | |
| 20 | const words = readFileSync(wordListPath, 'utf8') |
| 21 | .split('\n') |
| 22 | .filter(w => /^[a-z]{5}$/.test(w)); |
| 23 | |
| 24 | const ts = `// cspell:disable |
| 25 | // AUTO-GENERATED — do not edit by hand. Run: node scripts/generate-guess-list.js |
| 26 | export const VALID_GUESSES = new Set<string>(${JSON.stringify(words)}); |
| 27 | `; |
| 28 | |
| 29 | writeFileSync(OUT, ts, 'utf8'); |
| 30 | console.log(`Generated ${OUT} with ${words.length} words.`); |