UNPKG

weather-cli-nk_sm

Version:
63 lines (62 loc) 2.66 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { promises } from 'fs'; import { URL } from 'url'; import { DB_NAME } from '../constants/db.constants.js'; export var DBKeys; (function (DBKeys) { DBKeys["API_KEY"] = "API_KEY"; DBKeys["CITY"] = "CITY"; })(DBKeys || (DBKeys = {})); export default class StorageService { static isExist(filePath) { return __awaiter(this, void 0, void 0, function* () { try { yield promises.stat(filePath); return true; } catch (_) { return false; } }); } static get(key) { return __awaiter(this, void 0, void 0, function* () { this.fileIsReady = false; if (yield this.isExist(this.filePath)) { const data = JSON.parse((yield promises.readFile(this.filePath)).toString()); if (data[key]) { this.fileIsReady = true; return data[key]; } } this.fileIsReady = true; return undefined; }); } static post({ key, value }) { return __awaiter(this, void 0, void 0, function* () { const i = setInterval(() => __awaiter(this, void 0, void 0, function* () { if (this.fileIsReady) { clearInterval(i); this.fileIsReady = false; let data = {}; if (yield this.isExist(this.filePath)) data = JSON.parse((yield promises.readFile(this.filePath)).toString()); data[key] = value; yield promises.writeFile(this.filePath, JSON.stringify(data)); this.fileIsReady = true; } }), 5); }); } } StorageService.filePath = new URL(`../../${DB_NAME}`, import.meta.url).pathname.slice(1); StorageService.fileIsReady = true;