UNPKG

doclyft

Version:

CLI for DocLyft - Interactive documentation generator with hosted documentation support

150 lines (149 loc) 5.18 kB
"use strict"; /** * Version update checker for DocLyft CLI * Checks for newer versions and displays update notifications */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkForUpdates = checkForUpdates; exports.forceUpdateCheck = forceUpdateCheck; const axios_1 = __importDefault(require("axios")); const chalk_1 = __importDefault(require("chalk")); const PACKAGE_NAME = 'doclyft'; const CURRENT_VERSION = require('../../package.json').version; const CHECK_INTERVAL = 24 * 60 * 60 * 1000; // 24 hours const NPM_REGISTRY_URL = `https://registry.npmjs.org/${PACKAGE_NAME}/latest`; /** * Check for updates and display notification if available */ async function checkForUpdates(force = false) { try { const cache = getVersionCache(); const now = Date.now(); // Skip check if recently checked and not forced if (!force && cache && (now - cache.lastChecked) < CHECK_INTERVAL) { if (cache.updateAvailable) { displayUpdateBanner(cache.latestVersion); } return; } // Fetch latest version from npm registry const response = await axios_1.default.get(NPM_REGISTRY_URL, { timeout: 5000, headers: { 'User-Agent': `${PACKAGE_NAME}/${CURRENT_VERSION}` } }); const latestVersion = response.data.version; const updateAvailable = isNewerVersion(latestVersion, CURRENT_VERSION); // Cache the result saveVersionCache({ lastChecked: now, latestVersion, updateAvailable }); // Display update banner if update is available if (updateAvailable) { displayUpdateBanner(latestVersion); } } catch (error) { // Silently fail - don't interrupt user workflow for version checks // Only log in debug mode if (process.env.DEBUG) { console.error('Version check failed:', error); } } } /** * Display update notification banner */ function displayUpdateBanner(latestVersion) { console.log(''); console.log(chalk_1.default.bgYellow.black(' UPDATE AVAILABLE ')); console.log(''); console.log(chalk_1.default.yellow(`📦 DocLyft CLI ${latestVersion} is available (current: ${CURRENT_VERSION})`)); console.log(chalk_1.default.cyan('🔒 This update includes important security improvements')); console.log(''); console.log(chalk_1.default.white('To update, run:')); console.log(chalk_1.default.green(` npm install -g ${PACKAGE_NAME}@latest`)); console.log(''); console.log(chalk_1.default.gray('━'.repeat(60))); console.log(''); } /** * Compare version strings to determine if newer version is available */ function isNewerVersion(latest, current) { const latestParts = latest.split('.').map(Number); const currentParts = current.split('.').map(Number); for (let i = 0; i < Math.max(latestParts.length, currentParts.length); i++) { const latestPart = latestParts[i] || 0; const currentPart = currentParts[i] || 0; if (latestPart > currentPart) return true; if (latestPart < currentPart) return false; } return false; } /** * Get cached version check data */ function getVersionCache() { try { const os = require('os'); const path = require('path'); const fs = require('fs'); const cacheFile = path.join(os.homedir(), '.doclyft', 'version-cache.json'); if (!fs.existsSync(cacheFile)) { return null; } const cacheData = fs.readFileSync(cacheFile, 'utf-8'); return JSON.parse(cacheData); } catch { return null; } } /** * Save version check data to cache */ function saveVersionCache(data) { try { const os = require('os'); const path = require('path'); const fs = require('fs'); const cacheDir = path.join(os.homedir(), '.doclyft'); const cacheFile = path.join(cacheDir, 'version-cache.json'); // Ensure directory exists if (!fs.existsSync(cacheDir)) { fs.mkdirSync(cacheDir, { recursive: true }); } fs.writeFileSync(cacheFile, JSON.stringify(data, null, 2)); } catch { // Silently fail if we can't write cache } } /** * Force check for updates (used by update command) */ async function forceUpdateCheck() { try { const response = await axios_1.default.get(NPM_REGISTRY_URL, { timeout: 10000, headers: { 'User-Agent': `${PACKAGE_NAME}/${CURRENT_VERSION}` } }); const latestVersion = response.data.version; const updateAvailable = isNewerVersion(latestVersion, CURRENT_VERSION); return { updateAvailable, latestVersion }; } catch (error) { throw new Error(`Failed to check for updates: ${error instanceof Error ? error.message : 'Unknown error'}`); } }