-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
69 lines (64 loc) · 1.98 KB
/
weather.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
65
66
67
68
69
#!/usr/bin/env node
import { getArgs } from "./helpers/args.js";
import { printError, printSuccess, printHelp, printWeather } from "./services/log.service.js";
import { getKeyValue, saveKeyValue, TOKEN_DICTIONARY } from "./services/storage.service.js";
import { getWeather } from "./services/api.service.js";
const saveToken = async (userToken) => {
if (!userToken.length) {
printError('Please, enter the token: -t [TOKEN]');
return;
} else if (userToken === 'undefined') {
printError('Please, enter the correct token name.');
return;
}
try {
await saveKeyValue(TOKEN_DICTIONARY.token, userToken);
printSuccess(`'${userToken}' - saved as a token ;)`);
} catch(e) {
printError(e.message + '.');
}
};
const saveCity = async (userCity) => {
if (!userCity.length) {
printError('Please, enter the city: -s [CITY]');
return;
} else if (userCity === 'undefined') {
printError('Please, enter the correct name of the city.');
return;
}
try {
await saveKeyValue(TOKEN_DICTIONARY.city, userCity);
printSuccess(`'${userCity}' - saved as a city ;)`);
} catch(e) {
printError(e.message + '.');
}
};
const getForcast = async () => {
const city = process.env.CITY ?? await getKeyValue(TOKEN_DICTIONARY.city);
try {
const weatherObj = await getWeather(city);
printWeather(weatherObj);
} catch(e) {
if (e?.response?.status === 404) {
printError('Wrong name of the city.');
} else if (e?.response?.status === 401) {
printError('Wrong token.');
} else {
printError(`Something went wrong: \n ${e.message}.`);
}
}
};
const initCli = () => {
const args = getArgs(process.argv);
if (args.h) {
return printHelp();
}
if (args.s) {
return saveCity(args.s);
}
if (args.t) {
return saveToken(args.t);
}
return getForcast();
};
initCli();