-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
81 lines (76 loc) · 2.27 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const { args } = Deno;
import { parse } from "https://deno.land/std@0.153.0/flags/mod.ts";
import {
bold,
brightBlue,
brightGreen,
brightMagenta,
brightYellow,
} from "https://deno.land/std@0.153.0/fmt/colors.ts";
import { wait } from "https://deno.land/x/wait@0.1.12/mod.ts";
const spinner = wait("Generating Words").start();
// deno-lint-ignore no-inferrable-types
const BASE_URL: string = "https://san-random-words.vercel.app/";
const parsedArgs = parse(args);
async function getRandomwords() {
const res = await fetch(`${BASE_URL}`);
const data = await res.json();
const word = brightBlue(`${bold("Word:")} ${data[0].word}`);
const definition = brightGreen(
`${bold("Definition:")} ${data[0].definition}`,
);
const pronunciation = brightYellow(
`${bold("Pronunciation:")} ${data[0].pronunciation}`,
);
spinner.stop();
console.log(word + "\n" + definition + "\n" + pronunciation);
}
async function getPronunciation(Pronunciation: string) {
const cleanPronounce = Pronunciation;
if (cleanPronounce) {
const res = await fetch(`${BASE_URL}api/${cleanPronounce}`);
const data = await res.json();
const getPronounce = data;
if (data === "true") {
spinner.stop();
console.log("Enter a Any Word to Get Pronunciation -", "Hello World");
} else {
spinner.stop();
console.log(brightMagenta(bold(getPronounce)));
}
} else {
spinner.stop();
console.log("API Error");
}
}
function displayHelpMsg() {
spinner.stop();
return "flags:\n-h, --help: display help message\n-r, --random: Get Random Words with with pronunciation\n-p, --pronunciation: Get pronunciation for a Word";
}
async function main() {
switch (Object.keys(parsedArgs)[1]) {
case "help":
case "h":
console.log(displayHelpMsg());
break;
case "random":
case "r":
await getRandomwords();
break;
// deno-lint-ignore no-fallthrough
case "pronunciation":
// deno-lint-ignore no-case-declarations
case "p":
const Pronunciation = parsedArgs.p || parsedArgs.Pronunciation ||
"automation";
await getPronunciation(Pronunciation);
break;
case null:
case "":
console.log("Enter a Valid Command");
break;
default:
console.log(displayHelpMsg());
}
}
main();