dianom-weather 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ export function getArgs(args){
2
+ let res = {};
3
+ let [executer, file, ...inputArgs] = args;
4
+
5
+ inputArgs.forEach((value, index, array) => {
6
+ if(value.charAt(0) == '-'){
7
+ if(index == array.length - 1){
8
+ res[value.substring(1)] = true;
9
+ }
10
+ else if(array[index + 1].charAt(0) != '-'){
11
+ res[value.substring(1)] = array[index + 1];
12
+ }
13
+ else{
14
+ res[value.substring(1)] = true;
15
+ }
16
+ }
17
+ });
18
+
19
+ return res;
20
+ }
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "dianom-weather",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "license": "ISC",
6
+ "author": "",
7
+ "type": "module",
8
+ "main": "weather.js",
9
+ "scripts": {
10
+ "dev": "node weather.js"
11
+ },
12
+ "bin":{
13
+ "weather": "weather.js"
14
+ },
15
+ "keywords": [
16
+ "cli",
17
+ "weather"
18
+ ],
19
+ "dependencies": {
20
+ "axios": "^1.11.0",
21
+ "chalk": "^5.4.1",
22
+ "dedent-js": "^1.0.1"
23
+ }
24
+ }
@@ -0,0 +1,45 @@
1
+ import axios from "axios";
2
+ import { getKeyValue, KEY_DICTIONARY } from "./storage.service.js";
3
+
4
+ async function getWeather(city) {
5
+ const token = await getKeyValue(KEY_DICTIONARY.token);
6
+
7
+ if (!token) {
8
+ throw new Error('Не задан API ключ. Задай его через команду -t [API_KEY]');
9
+ }
10
+
11
+ const response = await axios.get('https://api.openweathermap.org/data/2.5/weather', {
12
+ params: {
13
+ q: city,
14
+ appid: token,
15
+ lang: 'ru',
16
+ units: 'metric'
17
+ }
18
+ });
19
+ return response.data;
20
+ }
21
+
22
+ function getIcon(iconCode){
23
+ switch (iconCode.slice(0, -1)){
24
+ case "01": // Clear sky
25
+ return "☀️";
26
+ case "02": // Few clouds
27
+ return "⛅️";
28
+ case "03": // Scattered clouds
29
+ return "☁️";
30
+ case "04": // Broken clouds
31
+ return "🌫️";
32
+ case "09": // Shower rain
33
+ return "🌂️";
34
+ case "10": // Rain
35
+ return "🌨️";
36
+ case "11": // Thunderstorm
37
+ return "⛈️";
38
+ case "13": // Snow
39
+ return "❄️";
40
+ case "50": // Mist
41
+ return "💨";
42
+ }
43
+ }
44
+
45
+ export { getWeather, getIcon }
@@ -0,0 +1,36 @@
1
+ import chalk from "chalk";
2
+ import dedent from "dedent-js";
3
+ import { getIcon } from "./api.service.js";
4
+
5
+ function printError(error) {
6
+ console.log(chalk.bgRed(' ERROR ') + error);
7
+ }
8
+
9
+ function printSuccess(message) {
10
+ console.log(chalk.bgGreen(' SUCCESS ') + message);
11
+ }
12
+
13
+ function printHelp() {
14
+ console.log(
15
+ dedent`${chalk.bgYellow(' HELP ')}
16
+ Без параметров - вывод погоды
17
+ -s [CITY] для установки города
18
+ -h для вывода помощи
19
+ -t [API_KEY] для сохранение токена`
20
+ )
21
+ }
22
+
23
+ function printWeather(data) {
24
+ console.log(
25
+ dedent`${chalk.bgYellow(' Погода ')}
26
+ Сейчас в городе ${data.name}
27
+ ${getIcon(data.weather[0].icon)} ${data.weather[0].description}
28
+ Температура: ${data.main.temp}° (ощущается как ${data.main.feels_like}°)
29
+ Влажность: ${data.main.humidity}%
30
+ Скорость ветра: ${data.wind.speed}м/c`
31
+ );
32
+
33
+ }
34
+
35
+
36
+ export { printError, printSuccess, printHelp, printWeather };
@@ -0,0 +1,48 @@
1
+ import { homedir } from 'os';
2
+ import { join } from 'path';
3
+ import fs from 'fs';
4
+
5
+ const KEY_DICTIONARY = {
6
+ token: 'token',
7
+ city: 'city'
8
+ }
9
+
10
+ const filePath = join(homedir(), 'weather-data.json');
11
+
12
+ async function saveKeyValue(key, value){
13
+ let data = {};
14
+
15
+ if(await isExist(filePath)){
16
+ const file = await fs.promises.readFile(filePath);
17
+ data = JSON.parse(file);
18
+
19
+ }
20
+
21
+ data[key] = value;
22
+
23
+ await fs.promises.writeFile(filePath, JSON.stringify(data));
24
+ }
25
+
26
+ async function getKeyValue(key){
27
+ if(await isExist(filePath)){
28
+ const file = await fs.promises.readFile(filePath);
29
+ const data = JSON.parse(file);
30
+ return data[key];
31
+ }
32
+
33
+ return undefined;
34
+ }
35
+
36
+ async function isExist(path){
37
+ try{
38
+ await fs.promises.stat(path);
39
+ return true;
40
+ }
41
+ catch(e){
42
+ return false;
43
+ }
44
+ }
45
+
46
+
47
+
48
+ export {saveKeyValue, getKeyValue, KEY_DICTIONARY};
package/weather.js ADDED
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { getArgs } from "./helpers/args.js";
4
+ import { getWeather } from "./services/api.service.js";
5
+ import { printError, printHelp, printSuccess, printWeather } from "./services/log.service.js";
6
+ import { getKeyValue, KEY_DICTIONARY, saveKeyValue } from "./services/storage.service.js";
7
+
8
+ async function saveToken(token){
9
+ try {
10
+ await saveKeyValue(KEY_DICTIONARY.token, token);
11
+ printSuccess('Токен сохранён');
12
+ } catch (e) {
13
+ printError(e.message);
14
+ }
15
+ }
16
+
17
+ async function saveCity(city){
18
+ try {
19
+ await saveKeyValue(KEY_DICTIONARY.city, city);
20
+ printSuccess('Город сохранён');
21
+ } catch (e) {
22
+ printError(e.message);
23
+ }
24
+ }
25
+
26
+ async function getForcast(){
27
+ try {
28
+ const city = await getKeyValue(KEY_DICTIONARY.city);
29
+ const weather = await getWeather(city);
30
+ printWeather(weather);
31
+ } catch (error) {
32
+ if(error?.response?.status == 404){
33
+ printError('Неверно указан город');
34
+ }
35
+ else if(error?.response?.status == 401){
36
+ printError('Неверно указан токен');
37
+ }
38
+ else{
39
+ printError(error.message);
40
+ }
41
+ }
42
+ }
43
+
44
+ function initCLI(){
45
+ const args = getArgs(process.argv);
46
+
47
+ if(args.h){
48
+ return printHelp();
49
+ }
50
+ if(args.s){
51
+ return saveCity(args.s);
52
+ }
53
+ if(args.t){
54
+ return saveToken(args.t);
55
+ }
56
+
57
+ return getForcast();
58
+ }
59
+
60
+ initCLI()
61
+