-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
54 lines (47 loc) · 1.24 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
#!/usr/bin/env node
import { getArgs } from './helpers/args.js';
import { getWeather } from './services/api.service.js';
import { printError, printHelp, printSucces, printWeather } from './services/log.service.js';
import { getKeyValue, saveKeyValue } from './services/storage.service.js';
const saver = async(key, key_api) => {
if (!key_api.length) {
printError('Instead of token you give me nothing')
return;
}
try {
await saveKeyValue(key, key_api)
printSucces('Token has been saved')
} catch (error) {
printError(error.message)
}
}
const getForecast = async(city) => {
try {
const weather = await getWeather(city);
printWeather(weather);
} catch (error) {
printError(error.message)
}
}
const getCity = async() => {
const city = await getKeyValue('city') || process.env.CITY;
return city;
}
const showingWeather = async() => {
const city = await getCity();
getForecast(city);
}
const initCLI = () => {
const args = getArgs(process.argv);
if (args.t) {
return saver("token", args.t);
}
if (args.s) {
return saver("city", args.s)
}
if (args.h) {
printHelp()
}
showingWeather();
}
initCLI();