-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
executable file
·64 lines (53 loc) · 1.59 KB
/
app.js
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
#!/usr/bin/env node
// Project's dependencies
const chalk = require("chalk");
const args = process.argv;
const {
getCityId,
getCityName,
displayResult,
getData,
parsePrayerTimesFromResponse,
} = require("./utils.js");
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
// Logging functioin
const green = (msg) => console.log(chalk.green(msg));
// Project's data
const { BANNER, LOCAL_STORAGE_PATH } = require("./constants");
const cities = require("./data/cities.json");
// Setting up localStorage
const { LocalStorage } = require("node-localstorage");
localStorage = new LocalStorage(LOCAL_STORAGE_PATH);
const cityName = getCityName(args[2], cities);
const cityId = getCityId(cityName, cities);
const main = async () => {
// Prinitng a banner ('cause I'm cool and I can do it XD)
green(BANNER);
const storageKey = `${cityName.toLowerCase()}_${new Date().toLocaleDateString()}`;
let item = localStorage.getItem(storageKey);
// Disable localStorage for local development
if (process.env.NODE_ENV === "development") {
console.log("development mode: localStorage is disabled");
item = null;
}
let prayers;
if (item) {
prayers = JSON.parse(item);
} else {
try {
prayers = parsePrayerTimesFromResponse(await getData(cityId));
localStorage.setItem(storageKey, JSON.stringify(prayers));
} catch (ex) {
//TODO: Use a more descriptif error message
console.error("Something went wrong!");
console.log(ex);
// console.log(ex);
return;
}
}
console.clear();
displayResult(prayers, cityName);
};
(async () => {
await main();
})();