-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (49 loc) · 1.68 KB
/
index.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
const commander = require('commander');
const time = require('./time');
const weather = require('./weather');
let apiKey = {
commands: ['wak', 'tak'],
wak: ['weather'],
tak: ['time'],
};
commander
.version('0.1.0', '-v, --version')
.option(`--${apiKey.commands[0]}`, 'provide weather api key')
.option(`--${apiKey.commands[1]}`, 'provide time api key')
.parse(process.argv);
const args = process.argv.slice(2);
const removableValuesFromArgs = apiKey.commands.map(command => `--${command}`);
const storeApiKey = command => {
if(commander[command]) {
apiKey[command].push(args[args.indexOf(`--${command}`) + 1]);
removableValuesFromArgs.push(apiKey[command][1]);
}
};
const showMissingArgMessage = command => {
if(!commander[command]) {
console.log(`Please provide api key for ${apiKey[command][0]} using --${command}`,'\nSee here -> https://github.com/ksharifbd/weathertime#how-to-get-the-api-keys');
process.exit(1);
}
}
apiKey.commands.forEach(command => {
storeApiKey(command);
showMissingArgMessage(command);
});
args
.filter(arg => !removableValuesFromArgs.includes(arg))
.forEach(async argument => {
try {
const weatherData = await weather(apiKey.wak[1], argument);
const {lat, lon} = weatherData.coord;
const timeData = await time(apiKey.tak[1], lat, lon);
const output = {
name: weatherData.name,
weather: weatherData.weather[0].main,
description: weatherData.weather[0].description,
time: timeData.formatted,
};
console.log('\n', output);
} catch(err) {
console.error(err);
}
});