diff --git a/README.md b/README.md index 4d06019..8a4f427 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,5 @@ Web app which returns the iconic Hello, World in various locales - [x] Print hello world in a random locale - [x] Select the locale by id +- [x] Use with CLI - [ ] Serve via internet diff --git a/cli.ts b/cli.ts new file mode 100644 index 0000000..d7741b7 --- /dev/null +++ b/cli.ts @@ -0,0 +1,33 @@ +import { readLines } from "https://deno.land/std@0.87.0/io/bufio.ts"; +import { ID, sayHello, sayRandomHello, ValidationError } from "./src"; + +const start = async () => { + console.log("Hello, World! (International version)\n"); + console.log( + "🌍 Type a number (between 1 and 78), or leave blank for random language. Type 'quit' to leave.", + ); + console.log("👉 "); + + for await (const line of readLines(Deno.stdin)) { + const value = line.trim(); + + if (value === "") { + console.log(sayRandomHello()); + } else if (value === "quit") { + return; + } else { + try { + const id = new ID(parseInt(value, 10)); + console.log(sayHello(id)); + } catch (error) { + if (error instanceof ValidationError) { + console.error(error.message); + } else { + throw error; + } + } + } + } +}; + +await start(); diff --git a/src/index.ts b/src/index.ts index 97c15b2..7bef041 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,35 +1,5 @@ -import { readLines } from "https://deno.land/std@0.87.0/io/bufio.ts"; import { ValidationError } from "./errors/exceptions.ts"; import { ID } from "./types/id.ts"; import { sayHello, sayRandomHello } from "./usecases/hello.ts"; -const start = async () => { - console.log("Hello, World! (International version)\n"); - console.log( - "🌍 Type a number (between 1 and 78), or leave blank for random language. Type 'quit' to leave.", - ); - console.log("👉 "); - - for await (const line of readLines(Deno.stdin)) { - const value = line.trim(); - - if (value === "") { - console.log(sayRandomHello()); - } else if (value === "quit") { - return; - } else { - try { - const id = new ID(parseInt(value, 10)); - console.log(sayHello(id)); - } catch (error) { - if (error instanceof ValidationError) { - console.error(error.message); - } else { - throw error; - } - } - } - } -}; - -await start(); +export { ID, sayHello, sayRandomHello, ValidationError };