-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
48 lines (44 loc) · 1.17 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
#!/usr/bin/env node
import { getArgs } from './helpers/args.js';
import {
printError,
printSuccess,
printHelp,
printForecast,
} from './services/log.service.js';
import { getKeyValue, saveKeyValue } from './services/storage.service.js';
import { getWeather, getIcon } from './services/api.service.js';
const getForecast = async () => {
try {
const city = process.env.CITY ?? (await getKeyValue('city'));
const weather = await getWeather(city);
printForecast(weather, getIcon(weather.weather[0].icon));
} catch (error) {
if (error?.response?.status == 404) {
printError('Provided city is not found');
} else if (error?.response?.status == 404) {
printError('Provided token is incorrect');
} else {
printError(error.message);
}
}
};
const saveValues = async obj => {
await saveKeyValue(obj);
Object.keys(obj).map(key =>
key == 't'
? printSuccess(`Token ${obj.t} has been successfully saved`)
: printSuccess(`City ${obj.s} has been successfully saved`)
);
};
const initCLI = () => {
const args = getArgs(process.argv);
if (args.h) {
printHelp();
return;
} else if (args.s || args.t) {
return saveValues(args);
}
getForecast();
};
initCLI();