UNPKG

aahook

Version:

A CLI tool that displays ASCII art when commands succeed or fail

180 lines 6.04 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.GitHubFetcher = void 0; const https = __importStar(require("https")); const fs = __importStar(require("fs")); const path = __importStar(require("path")); /** * Fetches ASCII arts from GitHub repository */ class GitHubFetcher { constructor() { this.baseUrl = 'https://raw.githubusercontent.com/MidoriTakahashi77/aahook/main/arts/'; this.cacheDir = path.join(process.env.HOME || '', '.aahook', 'cache'); this.ensureCacheDir(); } /** * Fetch ASCII art content from GitHub */ async fetchArt(category, name) { const url = `${this.baseUrl}${category}/${name}.txt`; return this.fetchText(url); } /** * Fetch category metadata */ async fetchCategory(name) { const url = `${this.baseUrl}${name}/META.json`; const content = await this.fetchText(url); return JSON.parse(content); } /** * Fetch index of all available arts */ async fetchIndex() { const url = `${this.baseUrl}index.json`; // Try cache first const cached = await this.getCached('index'); if (cached) { return cached; } try { const content = await this.fetchText(url); const index = JSON.parse(content); await this.setCached('index', index, 3600000); // Cache for 1 hour return index; } catch (error) { // If index.json doesn't exist, generate from known structure return this.generateDefaultIndex(); } } /** * Fetch text content from URL */ fetchText(url) { return new Promise((resolve, reject) => { const timeout = setTimeout(() => { reject(new Error('Request timeout')); }, 10000); // 10 second timeout https.get(url, (res) => { clearTimeout(timeout); if (res.statusCode === 404) { reject(new Error(`Not found: ${url}`)); return; } if (res.statusCode !== 200) { reject(new Error(`HTTP ${res.statusCode}: ${url}`)); return; } let data = ''; res.on('data', (chunk) => data += chunk); res.on('end', () => resolve(data)); }).on('error', (err) => { clearTimeout(timeout); reject(err); }); }); } /** * Get cached data */ async getCached(key) { const cachePath = path.join(this.cacheDir, `${key}.json`); if (!fs.existsSync(cachePath)) { return null; } try { const stat = fs.statSync(cachePath); const content = fs.readFileSync(cachePath, 'utf8'); const cache = JSON.parse(content); // Check expiration if (Date.now() - stat.mtimeMs > (cache.ttl || 3600000)) { fs.unlinkSync(cachePath); return null; } return cache.data; } catch { return null; } } /** * Set cached data */ async setCached(key, data, ttl = 3600000) { const cachePath = path.join(this.cacheDir, `${key}.json`); const cache = { data, ttl }; fs.writeFileSync(cachePath, JSON.stringify(cache, null, 2)); } /** * Clear all cache */ async clearCache() { if (fs.existsSync(this.cacheDir)) { const files = fs.readdirSync(this.cacheDir); files.forEach(file => { fs.unlinkSync(path.join(this.cacheDir, file)); }); } } /** * Ensure cache directory exists */ ensureCacheDir() { if (!fs.existsSync(this.cacheDir)) { fs.mkdirSync(this.cacheDir, { recursive: true }); } } /** * Generate default index from known structure */ generateDefaultIndex() { return { version: '1.0.0', updated: new Date().toISOString(), categories: [ { name: 'animals', displayName: 'Animals 🐾', count: 2 }, { name: 'celebrations', displayName: 'Celebrations 🎉', count: 2 }, { name: 'developer', displayName: 'Developer 💻', count: 2 }, { name: 'emotions', displayName: 'Emotions 😊', count: 3 } ], total: 9 }; } } exports.GitHubFetcher = GitHubFetcher; //# sourceMappingURL=github-fetcher.js.map