UNPKG

moshai-cli

Version:

A modern, fast Node.js CLI powered by arasadrahman

48 lines (39 loc) 1.93 kB
const axios = require('axios'); const chalk = require('chalk').default; module.exports = class WeatherCommand { static signature = 'weather'; static description = 'Show current weather for Gazipur'; async handle() { if (!this.startSpinner) this.startSpinner = () => { }; if (!this.stopSpinner) this.stopSpinner = () => { }; if (!this.info) this.info = console.log; if (!this.error) this.error = console.error; this.startSpinner('Fetching weather for Gazipur...'); try { const res = await axios.get('https://wttr.in/Gazipur+Sadar?format=j1'); this.stopSpinner(); const data = res.data; const current = data.current_condition[0]; const area = data.nearest_area[0]; const tempC = current.temp_C; const feelsLikeC = current.FeelsLikeC; const weatherDesc = current.weatherDesc[0].value; const humidity = current.humidity; const windSpeedKmph = current.windspeedKmph; const windDir = current.winddir16Point; // Build a colorful weather info block const output = [ chalk.bold.cyan(`\n🌤 Weather for ${area.areaName[0].value}, ${area.region[0].value}, ${area.country[0].value}\n`), chalk.yellow(`Condition: `) + chalk.white(weatherDesc), chalk.yellow(`Temperature: `) + chalk.white(`${tempC}°C (Feels like ${feelsLikeC}°C)`), chalk.yellow(`Humidity: `) + chalk.white(`${humidity}%`), chalk.yellow(`Wind: `) + chalk.white(`${windSpeedKmph} km/h ${windDir}`), '', ].join('\n'); this.info(output); } catch (err) { this.stopSpinner(); this.error(chalk.red('❌ Weather API failed: ') + err.message); } } };