-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
52 lines (45 loc) · 1.07 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
import { diskCache } from "./cache";
import drawTable from "./drawTable";
import { getIPData } from "./getIPData";
import { getSunData } from "./getSunData";
import isDate from "./isDate";
export interface Options {
clean: boolean;
date: string;
debug: boolean;
}
export type Log = (message: unknown) => void;
const main: ({ clean, date, debug }: Options) => Promise<string> = async ({
clean,
date,
debug,
}: Options) => {
function log(message: unknown): void {
if (debug) {
console.info(message);
}
}
log("[Options]:");
log({ clean, date, debug });
log("");
if (!isDate(date)) {
return "Date must be a valid date with format YYYY-MM-DD";
}
if (clean) {
try {
await diskCache.reset();
return "Cache is cleared";
} catch (error) {
return "Error clearing cache";
}
} else {
try {
const { lat, lon } = await getIPData(log);
const sunData = await getSunData(lat, lon, date, log);
return drawTable(sunData);
} catch (error) {
throw Error(error);
}
}
};
export default main;