UNPKG

scrabble-cheater

Version:

A simple Scrabble cheating tool.

49 lines (48 loc) 1.95 kB
#!/usr/bin/env node import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { program as commander } from 'commander'; import { ScrabbleCheater } from './index.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const packageJsonPath = path.join(__dirname, '../package.json'); const { bin, description, version } = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); commander .name(Object.keys(bin)[0]) .version(version) .description(description) .option('-w, --wordlist <file>', 'Specify a wordlist file (mandatory)') .option('-l, --letters <letters>', 'Specify letters') .option('-q, --quiet', 'Quiet mode: displays only the letters') .option('-m, --maximum <number>', 'Specify a maximum of results') .option('-s, --single', 'Single word mode: displays each word and copies it to the clipboard') .parse(process.argv); const commanderOptions = commander.opts(); if (!commanderOptions.wordlist) { console.error(' Error: no wordlist file specified.'); commander.help(); } if (commanderOptions.maximum && !parseInt(commanderOptions.maximum, 10)) { console.error(' Error: invalid maximum number specified.'); commander.help(); } const options = { ...(commanderOptions.letters && { letters: commanderOptions.letters }), ...(commanderOptions.maximum && { maximum: commanderOptions.maximum }), ...(commanderOptions.quiet && { quiet: commanderOptions.quiet }), ...(commanderOptions.single && { single: commanderOptions.single }), }; void (async () => { try { const matches = await new ScrabbleCheater(commanderOptions.wordlist, options).start(); if (matches.length && !commanderOptions.single) { console.info(matches.join('\n')); } process.exit(); } catch (error) { console.error(error); process.exit(1); } })();