UNPKG

eol-check

Version:

CLI tool to check End-of-Life (EOL) status of Node.js, package managers, operating systems, dependencies, and databases. Supports HTML reports and GitHub Actions.

78 lines (77 loc) 2.58 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Cache = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const os_1 = __importDefault(require("os")); class Cache { constructor(ttl = 24 * 60 * 60 * 1000) { // Default: 24 hours this.cacheDir = path_1.default.join(os_1.default.homedir(), '.eol-check', 'cache'); this.ttl = ttl; this.ensureCacheDir(); } ensureCacheDir() { if (!fs_1.default.existsSync(this.cacheDir)) { fs_1.default.mkdirSync(this.cacheDir, { recursive: true }); } } getCacheFilePath(product) { return path_1.default.join(this.cacheDir, `${product}.json`); } get(product) { const filePath = this.getCacheFilePath(product); if (!fs_1.default.existsSync(filePath)) { return null; } try { const content = fs_1.default.readFileSync(filePath, 'utf-8'); const entry = JSON.parse(content); // Check if cache is still valid const now = Date.now(); if (now - entry.timestamp > this.ttl) { // Cache expired return null; } return entry.data; } catch (error) { // Corrupted cache file, return null to trigger refresh console.warn(`Failed to read cache for ${product}:`, error); return null; } } set(product, data) { const filePath = this.getCacheFilePath(product); const entry = { product, timestamp: Date.now(), data, }; try { fs_1.default.writeFileSync(filePath, JSON.stringify(entry, null, 2), 'utf-8'); } catch (error) { console.warn(`Failed to write cache for ${product}:`, error); } } invalidate(product) { const filePath = this.getCacheFilePath(product); if (fs_1.default.existsSync(filePath)) { fs_1.default.unlinkSync(filePath); } } clear() { if (!fs_1.default.existsSync(this.cacheDir)) { return; } const files = fs_1.default.readdirSync(this.cacheDir); for (const file of files) { fs_1.default.unlinkSync(path_1.default.join(this.cacheDir, file)); } } } exports.Cache = Cache;