diff --git a/README.md b/README.md index b87cb2c..dc94def 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,22 @@ -lottery-cli -================= +# lottery-cli Command line tool to generate global lottery numbers - [![oclif](https://img.shields.io/badge/cli-oclif-brightgreen.svg)](https://oclif.io) -[![Version](https://img.shields.io/npm/v/lottery-cli.svg)](https://npmjs.org/package/lottery-cli) -[![Downloads/week](https://img.shields.io/npm/dw/lottery-cli.svg)](https://npmjs.org/package/lottery-cli) + -* [Usage](#usage) -* [Commands](#commands) + +- [Usage](#usage) +- [Commands](#commands) + # Usage + + ```sh-session $ npm install -g lottery-cli $ lottery COMMAND @@ -26,22 +28,26 @@ USAGE $ lottery COMMAND ... ``` + + # Commands + -* [`lottery hello PERSON`](#lottery-hello-person) -* [`lottery hello world`](#lottery-hello-world) -* [`lottery help [COMMAND]`](#lottery-help-command) -* [`lottery plugins`](#lottery-plugins) -* [`lottery plugins add PLUGIN`](#lottery-plugins-add-plugin) -* [`lottery plugins:inspect PLUGIN...`](#lottery-pluginsinspect-plugin) -* [`lottery plugins install PLUGIN`](#lottery-plugins-install-plugin) -* [`lottery plugins link PATH`](#lottery-plugins-link-path) -* [`lottery plugins remove [PLUGIN]`](#lottery-plugins-remove-plugin) -* [`lottery plugins reset`](#lottery-plugins-reset) -* [`lottery plugins uninstall [PLUGIN]`](#lottery-plugins-uninstall-plugin) -* [`lottery plugins unlink [PLUGIN]`](#lottery-plugins-unlink-plugin) -* [`lottery plugins update`](#lottery-plugins-update) + +- [`lottery hello PERSON`](#lottery-hello-person) +- [`lottery hello world`](#lottery-hello-world) +- [`lottery help [COMMAND]`](#lottery-help-command) +- [`lottery plugins`](#lottery-plugins) +- [`lottery plugins add PLUGIN`](#lottery-plugins-add-plugin) +- [`lottery plugins:inspect PLUGIN...`](#lottery-pluginsinspect-plugin) +- [`lottery plugins install PLUGIN`](#lottery-plugins-install-plugin) +- [`lottery plugins link PATH`](#lottery-plugins-link-path) +- [`lottery plugins remove [PLUGIN]`](#lottery-plugins-remove-plugin) +- [`lottery plugins reset`](#lottery-plugins-reset) +- [`lottery plugins uninstall [PLUGIN]`](#lottery-plugins-uninstall-plugin) +- [`lottery plugins unlink [PLUGIN]`](#lottery-plugins-unlink-plugin) +- [`lottery plugins update`](#lottery-plugins-update) ## `lottery hello PERSON` @@ -393,4 +399,5 @@ DESCRIPTION ``` _See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.1.3/src/commands/plugins/update.ts)_ + diff --git a/src/commands/hello/index.ts b/src/commands/hello/index.ts deleted file mode 100644 index 6fe09e1..0000000 --- a/src/commands/hello/index.ts +++ /dev/null @@ -1,25 +0,0 @@ -import {Args, Command, Flags} from '@oclif/core' - -export default class Hello extends Command { - static args = { - person: Args.string({description: 'Person to say hello to', required: true}), - } - - static description = 'Say hello' - - static examples = [ - `<%= config.bin %> <%= command.id %> friend --from oclif -hello friend from oclif! (./src/commands/hello/index.ts) -`, - ] - - static flags = { - from: Flags.string({char: 'f', description: 'Who is saying hello', required: true}), - } - - async run(): Promise { - const {args, flags} = await this.parse(Hello) - - this.log(`hello ${args.person} from ${flags.from}! (./src/commands/hello/index.ts)`) - } -} diff --git a/src/commands/hello/world.ts b/src/commands/hello/world.ts deleted file mode 100644 index 8111043..0000000 --- a/src/commands/hello/world.ts +++ /dev/null @@ -1,19 +0,0 @@ -import {Command} from '@oclif/core' - -export default class World extends Command { - static args = {} - - static description = 'Say hello world' - - static examples = [ - `<%= config.bin %> <%= command.id %> -hello world! (./src/commands/hello/world.ts) -`, - ] - - static flags = {} - - async run(): Promise { - this.log('hello world! (./src/commands/hello/world.ts)') - } -} diff --git a/src/commands/lottery.ts b/src/commands/lottery.ts new file mode 100644 index 0000000..488d00d --- /dev/null +++ b/src/commands/lottery.ts @@ -0,0 +1,76 @@ +import {Args, Command, Flags} from '@oclif/core' + +const generateUniqueNumbers = (count: number, min: number, max: number) => { + const numbers = new Set() + while (numbers.size < count) { + const num = Math.floor(Math.random() * (max - min + 1) + min) + numbers.add(num) + } + + return [...numbers] +} + +const generateLotteryNumbers = (lotteryType: string) => { + switch (lotteryType) { + case 'powerball': { + const mainNumbers = generateUniqueNumbers(5, 1, 69) + const powerballNumbers = generateUniqueNumbers(1, 1, 26) + return [...mainNumbers, ...powerballNumbers] + } + + case 'megamillions': { + const mainNumbers = generateUniqueNumbers(5, 1, 70) + const megaBallNumbers = generateUniqueNumbers(1, 1, 26) + return [...mainNumbers, megaBallNumbers] + } + + /* TODO: add all global lotteries here */ + default: { + throw new Error('Unsupported lottery type') + } + } +} + +export default class Lottery extends Command { + static override args = { + file: Args.string({description: 'file to read'}), + } + + static override description = 'Generate lottery numbers for major global lotteries' + + static override examples = [ + '<%= config.bin %> <%= command.id %> --type powerball', + '<%= config.bin %> <%= command.id %> --type megamillions', + ] + + static override flags = { + type: Flags.string({char: 't', description: 'type of lottery', required: true}), + } + + public async run(): Promise { + const {flags} = await this.parse(Lottery) + + const lotteryType = flags.type + try { + const numbers = generateLotteryNumbers(lotteryType) + const numString = numbers.join(', ') + + const art = ` + ______ + /\\ o o o\\ + /o \\ o o o\\_______ +< >------> o /| + \\ o/ o /_____/o| + \\/______/ |oo| + | o |o/ + |_______|/ + +Here are your ${lotteryType} numbers: ${numString} + ` + + this.log(art) + } catch (error: any) { + this.error(error.message) + } + } +} diff --git a/test/commands/hello/index.test.ts b/test/commands/hello/index.test.ts deleted file mode 100644 index dad0ac3..0000000 --- a/test/commands/hello/index.test.ts +++ /dev/null @@ -1,9 +0,0 @@ -import {runCommand} from '@oclif/test' -import {expect} from 'chai' - -describe('hello', () => { - it('runs hello', async () => { - const {stdout} = await runCommand('hello friend --from oclif') - expect(stdout).to.contain('hello friend from oclif!') - }) -}) diff --git a/test/commands/hello/world.test.ts b/test/commands/hello/world.test.ts deleted file mode 100644 index 0f5e90f..0000000 --- a/test/commands/hello/world.test.ts +++ /dev/null @@ -1,9 +0,0 @@ -import {runCommand} from '@oclif/test' -import {expect} from 'chai' - -describe('hello world', () => { - it('runs hello world cmd', async () => { - const {stdout} = await runCommand('hello world') - expect(stdout).to.contain('hello world!') - }) -}) diff --git a/test/commands/lottery.ts b/test/commands/lottery.ts new file mode 100644 index 0000000..5f22394 --- /dev/null +++ b/test/commands/lottery.ts @@ -0,0 +1,30 @@ +import {Args, Command, Flags} from '@oclif/core' + +export default class Lottery extends Command { + static override args = { + file: Args.string({description: 'file to read'}), + } + + static override description = 'describe the command here' + + static override examples = [ + '<%= config.bin %> <%= command.id %>', + ] + + static override flags = { + // flag with no value (-f, --force) + force: Flags.boolean({char: 'f'}), + // flag with a value (-n, --name=VALUE) + name: Flags.string({char: 'n', description: 'name to print'}), + } + + public async run(): Promise { + const {args, flags} = await this.parse(Lottery) + + const name = flags.name ?? 'world' + this.log(`hello ${name} from /Users/bittricky/Projects/lottery-cli/src/commands/lottery.ts`) + if (args.file && flags.force) { + this.log(`you input --force and --file: ${args.file}`) + } + } +}