-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
64 lines (59 loc) · 1.48 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
#!/usr/bin/env node
import {getArgs} from './helpers/args.js';
import {getWeather, getIcon} from './services/api.service.js';
import {printHelp, printSuccess, printError, printWeather} from './services/log.service.js';
import {saveKeyValue, TOKEN_DICTIONARY, getKeyValue} from "./services/storage.service.js";
const saveToken = async (token) => {
if (!token.length) {
printError('There is no token');
return;
}
try {
await saveKeyValue(TOKEN_DICTIONARY.token, token);
printSuccess('Token was saved!');
} catch (e) {
printError(e.message);
}
}
const saveCity = async (city) => {
if (!city.length) {
printError('City was not written');
return;
}
try {
await saveKeyValue(TOKEN_DICTIONARY.city, city);
printSuccess('City is saved');
} catch (e) {
printError(e.message);
}
};
const getForcast = async () => {
try {
const city = process.env.CITY ?? await getKeyValue(TOKEN_DICTIONARY.city);
const weather = await getWeather(city);
printWeather(weather, getIcon(weather.weather[0].icon));
} catch (e) {
if (e?.response?.status == 404) {
printError('This city does not exist');
} else if (e?.response?.status == 401) {
printError('This token was not correct');
} else {
printError(e.message);
}
}
}
const initCLI = () => {
const args = getArgs(process.argv);
// console.log(process.env);
if (args.h) {
return printHelp();
}
if (args.s) {
return saveCity(args.s);
}
if (args.t) {
return saveToken(args.t);
}
return getForcast();
};
initCLI();