UNPKG

windio

Version:

A cli tool to get weather data.

41 lines (35 loc) • 1.14 kB
import axios from "axios"; import chalk from "chalk"; import spinner from "../helpers/spinner.js"; export async function getWeatherToday(city) { if (!process.env.API_KEY) { console.log( "āŒ API Key not found. Use --apiKey <your-openWeatherApi-key> to save it.", ); return; } if (!city) { console.log( "āŒ City not provided. Use --setCity <your-city-name> to specify a city.", ); return; } try { spinner.start(); const apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${process.env.API_KEY}`; const response = await axios.get(apiUrl); const data = response.data; spinner.stop(); console.log( chalk.blueBright(`\nšŸ“ Location: ${data.name}, ${data.sys.country}`), ); console.log(`šŸŒ”ļø Temperature: ${data.main.temp}°C`); console.log(`šŸŒ„ļø Weather: ${data.weather[0].description}`); console.log(`šŸ’§ Humidity: ${data.main.humidity}%\n`); } catch (err) { console.log( "āŒ Failed to fetch weather. Please check the city name and API key.", ); console.error(err.message); } }