-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.js
executable file
·90 lines (85 loc) · 3.07 KB
/
app.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env node
const chalk = require("chalk");
const ora = require("ora");
const pck = require("./package.json");
const geocode = require("./utils/geocode");
const forecast = require("./utils/forecast");
const autolocate = require("./utils/autolocate");
const menu = require('./utils/menu');
const emojis = require('./utils/emojis.json');
const spinner = ora();
const args = process.argv;
let units;
let location;
(function () {
if (!process.env.WEATHERSTACK || !process.env.MAPBOX) {
spinner.fail("Failed to get API keys");
return;
}
if (args[2] === "-h" || args[2] === "--help") {
menu();
return;
}
if (args[2] === "-v" || args[2] === "--version") {
spinner.succeed(pck.version);
return;
}
for (let i = 2; i < args.length; i++) {
if (args[i].startsWith("--u")) {
parseunit = args[i].split("=")[1];
if (['m', 's', 'f'].includes(parseunit)) {
units = parseunit;
}
}
else if (!location) {
location = args[i];
}
}
if (!location) {
autolocate((err, { latitude, longitude, location } = {}) => {
if (err) {
return spinner.fail(err);
}
forecast(
latitude,
longitude,
units,
(err, { description, temp, feelsLike, tempScale, windSpeed, windDir, windScale, precipitation, precipitationUnits, humidity, cloudCover } = {}) => {
if (err) {
return spinner.fail(err);
}
spinner.succeed(chalk.underline(location));
console.log(
emojis.hasOwnProperty(description.toLowerCase().replace(' ', '_')) ? emojis[`${description.toLowerCase().replace(' ', '_')}`] : emojis.question,
chalk.cyanBright(
`${description}. It is currently ${temp}${tempScale}, it feels like ${feelsLike}${tempScale}.\nWind speed of ${windSpeed} ${windScale} with direction ${windDir}. \nHumidity of ${humidity}% with a Cloud Coverage of ${cloudCover}%.\n${precipitation} ${precipitationUnits} of precipitation.`
)
);
}
);
});
} else {
geocode(location, (err, { latitude, longitude, location } = {}) => {
if (err) {
return spinner.fail(err);
}
forecast(
latitude,
longitude,
units,
(err, { description, temp, feelsLike, tempScale, windSpeed, windDir, windScale, precipitation, precipitationUnits, humidity, cloudCover, emoji } = {}) => {
if (err) {
return spinner.fail(err);
}
spinner.succeed(chalk.underline(location));
console.log(
emojis.hasOwnProperty(description.toLowerCase().replace(' ', '_')) ? emojis[`${description.toLowerCase().replace(' ', '_')}`] : emojis.question,
chalk.cyanBright(
`${description}. It is currently ${temp}${tempScale}, it feels like ${feelsLike}${tempScale}.\nWind speed of ${windSpeed} ${windScale} with direction ${windDir}. \nHumidity of ${humidity}% with a Cloud Coverage of ${cloudCover}%.\n${precipitation} ${precipitationUnits} of precipitation.`
)
);
}
);
});
}
})();